Skip to content

Commit

Permalink
Drop string[] type for signatures saveWitnesses
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingHawk committed Dec 9, 2024
1 parent 260bacb commit bf77087
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion packages/sessions/src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ConfigTracker {

savePresignedConfiguration: (args: PresignedConfig) => Promise<void>

saveWitnesses: (args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: string[] | SignerSignature[] }) => Promise<void>
saveWitnesses: (args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: SignerSignature[] }) => Promise<void>

configOfImageHash: (args: { imageHash: string; noCache?: boolean }) => Promise<commons.config.Config | undefined>

Expand Down
2 changes: 1 addition & 1 deletion packages/sessions/src/trackers/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class ArweaveReader implements ConfigTracker, migrator.PresignedMigration
throw new Error('arweave backend does not support saving config updates')
}

saveWitnesses(_args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: string[] | SignerSignature[] }): Promise<void> {
saveWitnesses(_args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: SignerSignature[] }): Promise<void> {
throw new Error('arweave backend does not support saving signatures')
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sessions/src/trackers/cached.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class CachedTracker implements migrator.PresignedMigrationTracker, Config
wallet: string
digest: string
chainId: ethers.BigNumberish
signatures: string[] | SignerSignature[]
signatures: SignerSignature[]
}): Promise<void> {
await Promise.all([this.tracker.saveWitnesses(args), this.cache.saveWitnesses(args)])
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sessions/src/trackers/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class DebugConfigTracker implements ConfigTracker, migrator.PresignedMigr
return this.tracker.savePresignedConfiguration(args)
}

saveWitnesses(args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: string[] | SignerSignature[] }): Promise<void> {
saveWitnesses(args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures:SignerSignature[] }): Promise<void> {
console.debug('? saveWitnesses')
debug(args, '? ')
return this.tracker.saveWitnesses(args)
Expand Down
2 changes: 1 addition & 1 deletion packages/sessions/src/trackers/deduped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DedupedTracker implements migrator.PresignedMigrationTracker, Confi
return this.cache.do('savePresignedConfiguration', undefined, args => this.tracker.savePresignedConfiguration(args), args)
}

saveWitnesses(args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: string[] | SignerSignature[] }): Promise<void> {
saveWitnesses(args: { wallet: string; digest: string; chainId: ethers.BigNumberish; signatures: SignerSignature[] }): Promise<void> {
return this.cache.do('saveWitnesses', undefined, args => this.tracker.saveWitnesses(args), args)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sessions/src/trackers/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export class LocalConfigTracker implements ConfigTracker, migrator.PresignedMigr
wallet: string
digest: string
chainId: ethers.BigNumberish
signatures: string[] | SignerSignature[]
signatures: SignerSignature[]
}): Promise<void> => {
const payload = {
digest: args.digest,
Expand All @@ -406,7 +406,7 @@ export class LocalConfigTracker implements ConfigTracker, migrator.PresignedMigr
await Promise.all([
this.savePayload({ payload }),
...args.signatures
.map(s => (typeof s === 'string' ? s : s.signature))
.map(s => s.signature)
.filter(signature => {
// We don't support saving witnesses for non-recoverable signatures
// we could change this eventually, but the issue is that the witness may become invalid
Expand Down
8 changes: 4 additions & 4 deletions packages/sessions/src/trackers/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf

const result = Object.keys(wallets).map(w => ({ wallet: w, proof: wallets[w] }))

const witnesses = new Map<string, { wallet: string; digest: string; chainId: bigint; signatures: string[] }>()
const witnesses = new Map<string, { wallet: string; digest: string; chainId: bigint; signatures: SignerSignature[] }>()
result.forEach(({ wallet, proof: { digest, chainId, signature } }) => {
const key = `${wallet}-${digest}-${chainId}`
let signatures = witnesses.get(key)
if (!signatures) {
signatures = { wallet, digest, chainId, signatures: [] }
witnesses.set(key, signatures)
}
signatures.signatures.push(signature)
signatures.signatures.push({ address: args.signer, signature, referenceChainId: chainId })
})
witnesses.forEach(witnesses => this.saveWitnesses(witnesses))
witnesses.forEach(witness => this.saveWitnesses(witness))

return result
}
Expand All @@ -151,7 +151,7 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf
wallet: string
digest: string
chainId: ethers.BigNumberish
signatures: string[] | SignerSignature[]
signatures: SignerSignature[]
}): Promise<void> {
await Promise.all(this.trackers.map(t => t.saveWitnesses(args)))
}
Expand Down
38 changes: 11 additions & 27 deletions packages/sessions/src/trackers/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,22 @@ export class RemoteConfigTracker implements ConfigTracker, migrator.PresignedMig
wallet: string
digest: string
chainId: ethers.BigNumberish
signatures: string[] | SignerSignature[]
signatures: SignerSignature[]
}): Promise<void> {
let filteredSignatures = args.signatures
if (this.onlyRecoverable) {
filteredSignatures = filteredSignatures.filter(signature => {
if (typeof signature === 'string') {
return commons.signer.canRecover(signature)
} else {
return !!signature.address
}
}) as string[] | SignerSignature[]
filteredSignatures = filteredSignatures.filter(signature => !!signature.address)
}

if (filteredSignatures.some(sig => typeof sig === 'string') && filteredSignatures.some(sig => typeof sig === 'object')) {
throw new Error('Signatures must be string[] | SignerSignature[]')
}
if (filteredSignatures.length === 0 || typeof args.signatures[0] === 'string') {
await this.sessions.saveSignerSignatures({
wallet: args.wallet,
digest: args.digest,
chainID: numberString(args.chainId),
signatures: filteredSignatures as string[]
})
} else {
await this.sessions.saveSignerSignatures2({
wallet: args.wallet,
digest: args.digest,
chainID: numberString(args.chainId),
// Rename "address" to "signer"
signatures: (filteredSignatures as SignerSignature[]).map(({ address, signature, referenceChainId }) => ({ signer: address, signature, referenceChainId: referenceChainId?.toString() }))
})
}
// Rename "address" to "signer"
const mappedSignatures = filteredSignatures.map(({ address, signature, referenceChainId }) => ({ signer: address, signature, referenceChainId: referenceChainId?.toString() }))

await this.sessions.saveSignerSignatures2({
wallet: args.wallet,
digest: args.digest,
chainID: numberString(args.chainId),
signatures: mappedSignatures
})
}

async configOfImageHash(args: { imageHash: string }): Promise<commons.config.Config | undefined> {
Expand Down

0 comments on commit bf77087

Please sign in to comment.