Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PPPP support #477

Merged
merged 9 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
},
"dependencies": {
"@noble/secp256k1": "^1.5.2",
"@xmtp/ecies-bindings-wasm": "^0.1.5",
"@xmtp/proto": "^3.28.0-beta.1",
"@xmtp/ecies-bindings-wasm": "^0.1.7",
"@xmtp/proto": "^3.32.0",
"async-mutex": "^0.4.0",
"elliptic": "^6.5.4",
"ethers": "^5.5.3",
Expand Down
26 changes: 19 additions & 7 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
buildUserInviteTopic,
isBrowser,
getSigner,
EnvelopeMapperWithMessage,
EnvelopeWithMessage,
} from './utils'
import { utils } from 'ethers'
import { Signer } from './types/Signer'
Expand Down Expand Up @@ -44,6 +46,7 @@ import { hasMetamaskWithSnaps } from './keystore/snapHelpers'
import { version as snapVersion, package as snapPackage } from './snapInfo.json'
import { ExtractDecodedType } from './types/client'
import type { WalletClient } from 'viem'
import { Contacts } from './Contacts'
const { Compression } = proto

// eslint-disable @typescript-eslint/explicit-module-boundary-types
Expand Down Expand Up @@ -194,6 +197,10 @@ export type PreEventCallbackOptions = {
preEnableIdentityCallback?: PreEventCallback
}

export type AllowListOptions = {
enableAllowList: boolean
}

/**
* Aggregate type for client options. Optional properties are used when the default value is calculated on invocation, and are computed
* as needed by each function. All other defaults are specified in defaultOptions.
Expand All @@ -203,7 +210,8 @@ export type ClientOptions = Flatten<
KeyStoreOptions &
ContentOptions &
LegacyOptions &
PreEventCallbackOptions
PreEventCallbackOptions &
AllowListOptions
>

/**
Expand All @@ -226,6 +234,7 @@ export function defaultOptions(opts?: Partial<ClientOptions>): ClientOptions {
disablePersistenceEncryption: false,
keystoreProviders: defaultKeystoreProviders(),
apiClientFactory: createHttpApiClientFromOptions,
enableAllowList: false,
}

if (opts?.codecs) {
Expand All @@ -251,7 +260,7 @@ export default class Client<ContentTypes = any> {
address: string
keystore: Keystore
apiClient: ApiClient
contacts: Set<string> // address which we have connected to
contacts: Contacts
publicKeyBundle: PublicKeyBundle
private knownPublicKeyBundles: Map<
string,
Expand All @@ -263,14 +272,16 @@ export default class Client<ContentTypes = any> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _codecs: Map<string, ContentCodec<any>>
private _maxContentSize: number
readonly _enableAllowList: boolean

constructor(
publicKeyBundle: PublicKeyBundle,
apiClient: ApiClient,
backupClient: BackupClient,
keystore: Keystore
keystore: Keystore,
enableAllowList: boolean = false
) {
this.contacts = new Set<string>()
this.contacts = new Contacts(this)
this.knownPublicKeyBundles = new Map<
string,
PublicKeyBundle | SignedPublicKeyBundle
Expand All @@ -284,6 +295,7 @@ export default class Client<ContentTypes = any> {
this._maxContentSize = MaxContentSize
this.apiClient = apiClient
this._backupClient = backupClient
this._enableAllowList = enableAllowList
}

/**
Expand Down Expand Up @@ -328,7 +340,7 @@ export default class Client<ContentTypes = any> {
const backupClient = await Client.setupBackupClient(address, options.env)
const client = new Client<
ExtractDecodedType<[...ContentCodecs, TextCodec][number]> | undefined
>(publicKeyBundle, apiClient, backupClient, keystore)
>(publicKeyBundle, apiClient, backupClient, keystore, opts?.enableAllowList)
await client.init(options)
return client
}
Expand Down Expand Up @@ -701,7 +713,7 @@ export default class Client<ContentTypes = any> {
*/
async listEnvelopes<Out>(
topic: string,
mapper: EnvelopeMapper<Out>,
mapper: EnvelopeMapperWithMessage<Out>,
opts?: ListMessagesOptions
): Promise<Out[]> {
if (!opts) {
Expand All @@ -721,7 +733,7 @@ export default class Client<ContentTypes = any> {
for (const env of envelopes) {
if (!env.message) continue
try {
const res = await mapper(env)
const res = await mapper(env as EnvelopeWithMessage)
rygine marked this conversation as resolved.
Show resolved Hide resolved
results.push(res)
} catch (e) {
console.warn('Error in listEnvelopes mapper', e)
Expand Down
211 changes: 211 additions & 0 deletions src/Contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import Client from './Client'
import { privatePreferences } from '@xmtp/proto'
import { EnvelopeWithMessage, buildUserPrivatePreferencesTopic } from './utils'

export type AllowListPermissionType = 'allow' | 'block' | 'unknown'
rygine marked this conversation as resolved.
Show resolved Hide resolved

export type AllowListEntryType = 'address'

export class AllowListEntry {
rygine marked this conversation as resolved.
Show resolved Hide resolved
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>
static _identifier: string

constructor() {
this.entries = new Map<string, AllowListPermissionType>()
}

allow(address: string) {
const entry = AllowListEntry.fromAddress(address, 'allow')
this.entries.set(entry.key, 'allow')
return entry
}

block(address: string) {
const entry = AllowListEntry.fromAddress(address, 'block')
this.entries.set(entry.key, 'block')
return entry
}

state(address: string) {
const entry = AllowListEntry.fromAddress(address)
return this.entries.get(entry.key) ?? 'unknown'
}

static async getIdentifier(client: Client): Promise<string> {
if (!this._identifier) {
const { identifier } =
await client.keystore.getPrivatePreferencesTopicIdentifier()
this._identifier = identifier
}
return this._identifier
}

static async load(client: Client): Promise<AllowList> {
const allowList = new AllowList()
const identifier = await this.getIdentifier(client)
const contentTopic = buildUserPrivatePreferencesTopic(identifier)

const messages = await client.listEnvelopes(
contentTopic,
async ({ message }: EnvelopeWithMessage) => message
)

// decrypt messages
const { responses } = await client.keystore.selfDecrypt({
requests: messages.map((message) => ({ payload: message })),
})

// decoded actions
const actions = responses.reduce((result, response) => {
return response.result?.decrypted
? result.concat(
privatePreferences.PrivatePreferencesAction.decode(
response.result.decrypted
)
)
: result
}, [] as privatePreferences.PrivatePreferencesAction[])

actions.forEach((action) => {
action.allow?.walletAddresses.forEach((address) => {
allowList.allow(address)
})
action.block?.walletAddresses.forEach((address) => {
allowList.block(address)
})
})

return allowList
}

static async publish(entries: AllowListEntry[], client: Client) {
const identifier = await this.getIdentifier(client)

// encoded actions
const actions = entries.reduce((result, 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,
}
return result.concat(
privatePreferences.PrivatePreferencesAction.encode(action).finish()
)
}
return result
}, [] as Uint8Array[])

const payloads = actions.map((action) => ({ payload: action }))

const { responses } = await client.keystore.selfEncrypt({
requests: payloads,
})

// encrypted messages
const messages = responses.reduce((result, response) => {
return response.result?.encrypted
? result.concat(response.result?.encrypted)
: result
}, [] as Uint8Array[])

const contentTopic = buildUserPrivatePreferencesTopic(identifier)
const timestamp = new Date()

// envelopes to publish
const envelopes = messages.map((message) => ({
contentTopic,
message,
timestamp,
}))

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() {
if (this.client._enableAllowList) {
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'
}

allowState(address: string) {
return this.allowList.state(address)
}

async allow(addresses: string[]) {
if (this.client._enableAllowList) {
await AllowList.publish(
addresses.map((address) => this.allowList.allow(address)),
this.client
)
}
}

async block(addresses: string[]) {
if (this.client._enableAllowList) {
await AllowList.publish(
addresses.map((address) => this.allowList.block(address)),
this.client
)
}
}
}
Loading
Loading