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 all 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
32 changes: 25 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 ConsentListOptions = {
enableConsentList: 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 &
ConsentListOptions
>

/**
Expand All @@ -226,6 +234,7 @@ export function defaultOptions(opts?: Partial<ClientOptions>): ClientOptions {
disablePersistenceEncryption: false,
keystoreProviders: defaultKeystoreProviders(),
apiClientFactory: createHttpApiClientFromOptions,
enableConsentList: 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 _enableConsentList: boolean

constructor(
publicKeyBundle: PublicKeyBundle,
apiClient: ApiClient,
backupClient: BackupClient,
keystore: Keystore
keystore: Keystore,
enableConsentList: 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._enableConsentList = enableConsentList
}

/**
Expand Down Expand Up @@ -328,7 +340,13 @@ 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?.enableConsentList
)
await client.init(options)
return client
}
Expand Down Expand Up @@ -701,7 +719,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 +739,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
209 changes: 209 additions & 0 deletions src/Contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import Client from './Client'
import { privatePreferences } from '@xmtp/proto'
import { EnvelopeWithMessage, buildUserPrivatePreferencesTopic } from './utils'

export type ConsentState = 'allowed' | 'blocked' | 'unknown'

export type ConsentListEntryType = 'address'

export class ConsentListEntry {
value: string
entryType: ConsentListEntryType
permissionType: ConsentState

constructor(
value: string,
entryType: ConsentListEntryType,
permissionType: ConsentState
) {
this.value = value
this.entryType = entryType
this.permissionType = permissionType
}

get key(): string {
return `${this.entryType}-${this.value}`
}

static fromAddress(
address: string,
permissionType: ConsentState = 'unknown'
): ConsentListEntry {
return new ConsentListEntry(address, 'address', permissionType)
}
}

export class ConsentList {
entries: Map<string, ConsentState>
static _identifier: string

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

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

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

state(address: string) {
const entry = ConsentListEntry.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<ConsentList> {
const consentList = new ConsentList()
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) => {
consentList.allow(address)
})
action.block?.walletAddresses.forEach((address) => {
consentList.block(address)
})
})
nplasterer marked this conversation as resolved.
Show resolved Hide resolved

return consentList
}

static async publish(entries: ConsentListEntry[], 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 === 'allowed'
? {
walletAddresses: [entry.value],
}
: undefined,
block:
entry.permissionType === 'blocked'
? {
walletAddresses: [entry.value],
}
: undefined,
}
return result.concat(
privatePreferences.PrivatePreferencesAction.encode(action).finish()
)
}
return result
}, [] as Uint8Array[])

const { responses } = await client.keystore.selfEncrypt({
requests: actions.map((action) => ({ payload: action })),
})

// 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>
private consentList: ConsentList
client: Client

constructor(client: Client) {
this.addresses = new Set<string>()
this.consentList = new ConsentList()
this.client = client
}

async refreshConsentList() {
if (this.client._enableConsentList) {
this.consentList = await ConsentList.load(this.client)
}
}

isAllowed(address: string) {
return this.consentList.state(address) === 'allowed'
}

isBlocked(address: string) {
return this.consentList.state(address) === 'blocked'
}

consentState(address: string) {
return this.consentList.state(address)
}

async allow(addresses: string[]) {
if (this.client._enableConsentList) {
await ConsentList.publish(
addresses.map((address) => this.consentList.allow(address)),
this.client
)
}
}

async block(addresses: string[]) {
if (this.client._enableConsentList) {
await ConsentList.publish(
addresses.map((address) => this.consentList.block(address)),
this.client
)
}
}
}
Loading
Loading