-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import { Envelope } from '@xmtp/proto/ts/dist/types/message_api/v1/message_api.pb' | ||
import Client from './Client' | ||
import { | ||
// eslint-disable-next-line camelcase | ||
ecies_decrypt_k256_sha3_256, | ||
// eslint-disable-next-line camelcase | ||
ecies_encrypt_k256_sha3_256, | ||
// eslint-disable-next-line camelcase | ||
generate_private_preferences_topic, | ||
} from '@xmtp/ecies-bindings-wasm' | ||
import { privatePreferences } from '@xmtp/proto' | ||
import { buildUserPrivatePreferencesTopic } from './utils' | ||
import { PublishParams } from './ApiClient' | ||
|
||
export type AllowListPermissionType = 'allow' | 'block' | 'unknown' | ||
|
||
export type AllowListEntryType = 'address' | ||
|
||
export class AllowListEntry { | ||
value: string | ||
entryType: AllowListEntryType | ||
permissionType: AllowListPermissionType | ||
|
||
constructor( | ||
value: string, | ||
entryType: AllowListEntryType, | ||
permissionType: AllowListPermissionType | ||
) { | ||
this.value = value | ||
this.entryType = entryType | ||
this.permissionType = permissionType | ||
} | ||
|
||
get key(): string { | ||
return `${this.entryType}-${this.value}` | ||
} | ||
|
||
static fromAddress( | ||
address: string, | ||
permissionType: AllowListPermissionType = 'unknown' | ||
): AllowListEntry { | ||
return new AllowListEntry(address, 'address', permissionType) | ||
} | ||
} | ||
|
||
export class AllowList { | ||
entries: Map<string, AllowListPermissionType> | ||
|
||
constructor() { | ||
this.entries = new Map<string, AllowListPermissionType>() | ||
} | ||
|
||
allow(address: string) { | ||
const entry = AllowListEntry.fromAddress(address, 'allow') | ||
this.entries.set(address, 'allow') | ||
return entry | ||
} | ||
|
||
block(address: string) { | ||
const entry = AllowListEntry.fromAddress(address, 'block') | ||
this.entries.set(address, 'block') | ||
return entry | ||
} | ||
|
||
state(address: string) { | ||
return this.entries.get(address) ?? 'unknown' | ||
} | ||
|
||
static async load(client: Client): Promise<AllowList> { | ||
const allowList = new AllowList() | ||
const privateKeyBundle = await client.keystore.getPrivateKeyBundle() | ||
const publicKey = | ||
privateKeyBundle.identityKey?.publicKey?.secp256k1Uncompressed?.bytes | ||
const privateKey = privateKeyBundle.identityKey?.secp256k1?.bytes | ||
if (publicKey && privateKey) { | ||
const identifier = | ||
generate_private_preferences_topic(privateKey).toString() | ||
const envelopes = ( | ||
await client.listEnvelopes( | ||
buildUserPrivatePreferencesTopic(identifier), | ||
async ({ message }: Envelope) => { | ||
if (message) { | ||
const payload = ecies_decrypt_k256_sha3_256( | ||
publicKey, | ||
privateKey, | ||
message | ||
) | ||
return privatePreferences.PrivatePreferencesAction.decode(payload) | ||
} | ||
return undefined | ||
} | ||
) | ||
).filter( | ||
(envelope) => envelope !== undefined | ||
) as privatePreferences.PrivatePreferencesAction[] | ||
|
||
envelopes.forEach((envelope) => { | ||
envelope.allow?.walletAddresses.forEach((address) => { | ||
allowList.allow(address) | ||
}) | ||
envelope.block?.walletAddresses.forEach((address) => { | ||
allowList.block(address) | ||
}) | ||
}) | ||
} | ||
|
||
return allowList | ||
} | ||
|
||
static async publish(entries: AllowListEntry[], client: Client) { | ||
const privateKeyBundle = await client.keystore.getPrivateKeyBundle() | ||
const publicKey = | ||
privateKeyBundle.identityKey?.publicKey?.secp256k1Uncompressed?.bytes | ||
const privateKey = privateKeyBundle.identityKey?.secp256k1?.bytes | ||
|
||
if (publicKey && privateKey) { | ||
const identifier = | ||
generate_private_preferences_topic(privateKey).toString() | ||
|
||
const envelopes = entries | ||
.map((entry) => { | ||
if (entry.entryType === 'address') { | ||
const action: privatePreferences.PrivatePreferencesAction = { | ||
allow: | ||
entry.permissionType === 'allow' | ||
? { | ||
walletAddresses: [entry.value], | ||
} | ||
: undefined, | ||
block: | ||
entry.permissionType === 'block' | ||
? { | ||
walletAddresses: [entry.value], | ||
} | ||
: undefined, | ||
} | ||
const payload = | ||
privatePreferences.PrivatePreferencesAction.encode( | ||
action | ||
).finish() | ||
const message = ecies_encrypt_k256_sha3_256( | ||
publicKey, | ||
privateKey, | ||
payload | ||
) | ||
return { | ||
contentTopic: buildUserPrivatePreferencesTopic(identifier), | ||
message, | ||
timestamp: new Date(), | ||
} | ||
} | ||
return undefined | ||
}) | ||
.filter((envelope) => envelope !== undefined) as PublishParams[] | ||
await client.publishEnvelopes(envelopes) | ||
} | ||
} | ||
} | ||
|
||
export class Contacts { | ||
/** | ||
* Addresses that the client has connected to | ||
*/ | ||
addresses: Set<string> | ||
allowList: AllowList | ||
client: Client | ||
|
||
constructor(client: Client) { | ||
this.addresses = new Set<string>() | ||
this.allowList = new AllowList() | ||
this.client = client | ||
} | ||
|
||
async refreshAllowList() { | ||
this.allowList = await AllowList.load(this.client) | ||
} | ||
|
||
isAllowed(address: string) { | ||
return this.allowList.state(address) === 'allow' | ||
} | ||
|
||
isBlocked(address: string) { | ||
return this.allowList.state(address) === 'block' | ||
} | ||
|
||
async allow(addresses: string[]) { | ||
await AllowList.publish( | ||
addresses.map((address) => this.allowList.allow(address)), | ||
this.client | ||
) | ||
} | ||
|
||
async block(addresses: string[]) { | ||
await AllowList.publish( | ||
addresses.map((address) => this.allowList.block(address)), | ||
this.client | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters