Skip to content

Commit

Permalink
refactor: update naming
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Oct 26, 2023
1 parent 1ee84c0 commit d5c15c7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 54 deletions.
22 changes: 14 additions & 8 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export type PreEventCallbackOptions = {
preEnableIdentityCallback?: PreEventCallback
}

export type AllowListOptions = {
enableAllowList: boolean
export type ConsentListOptions = {
enableConsentList: boolean
}

/**
Expand All @@ -211,7 +211,7 @@ export type ClientOptions = Flatten<
ContentOptions &
LegacyOptions &
PreEventCallbackOptions &
AllowListOptions
ConsentListOptions
>

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

if (opts?.codecs) {
Expand Down Expand Up @@ -272,14 +272,14 @@ 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
readonly _enableConsentList: boolean

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

/**
Expand Down Expand Up @@ -340,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, opts?.enableAllowList)
>(
publicKeyBundle,
apiClient,
backupClient,
keystore,
opts?.enableConsentList
)
await client.init(options)
return client
}
Expand Down
86 changes: 42 additions & 44 deletions src/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import Client from './Client'
import { privatePreferences } from '@xmtp/proto'
import { EnvelopeWithMessage, buildUserPrivatePreferencesTopic } from './utils'

export type AllowListPermissionType = 'allow' | 'block' | 'unknown'
export type ConsentState = 'allowed' | 'blocked' | 'unknown'

export type AllowListEntryType = 'address'
export type ConsentListEntryType = 'address'

export class AllowListEntry {
export class ConsentListEntry {
value: string
entryType: AllowListEntryType
permissionType: AllowListPermissionType
entryType: ConsentListEntryType
permissionType: ConsentState

constructor(
value: string,
entryType: AllowListEntryType,
permissionType: AllowListPermissionType
entryType: ConsentListEntryType,
permissionType: ConsentState
) {
this.value = value
this.entryType = entryType
Expand All @@ -27,34 +27,34 @@ export class AllowListEntry {

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

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

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

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

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

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

Expand All @@ -67,8 +67,8 @@ export class AllowList {
return this._identifier
}

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

Expand All @@ -95,31 +95,31 @@ export class AllowList {

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

return allowList
return consentList
}

static async publish(entries: AllowListEntry[], client: Client) {
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 === 'allow'
entry.permissionType === 'allowed'
? {
walletAddresses: [entry.value],
}
: undefined,
block:
entry.permissionType === 'block'
entry.permissionType === 'blocked'
? {
walletAddresses: [entry.value],
}
Expand All @@ -132,10 +132,8 @@ export class AllowList {
return result
}, [] as Uint8Array[])

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

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

// encrypted messages
Expand Down Expand Up @@ -164,46 +162,46 @@ export class Contacts {
* Addresses that the client has connected to
*/
addresses: Set<string>
allowList: AllowList
consentList: ConsentList
client: Client

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

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

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

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

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

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

async block(addresses: string[]) {
if (this.client._enableAllowList) {
await AllowList.publish(
addresses.map((address) => this.allowList.block(address)),
if (this.client._enableConsentList) {
await ConsentList.publish(
addresses.map((address) => this.consentList.block(address)),
this.client
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/conversations/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ export class ConversationV2<ContentTypes>
return this.client.contacts.isBlocked(this.peerAddress)
}

get allowState() {
return this.client.contacts.allowState(this.peerAddress)
get consentState() {
return this.client.contacts.consentState(this.peerAddress)
}

/**
Expand Down

0 comments on commit d5c15c7

Please sign in to comment.