Skip to content

Commit 0ce5bc6

Browse files
committed
Add reference chain usage for EIP-1271 signers
1 parent 8a5fc79 commit 0ce5bc6

File tree

9 files changed

+86
-22
lines changed

9 files changed

+86
-22
lines changed

packages/account/src/account.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,15 @@ export class Account {
517517
return this.tracker.saveWitnesses({ wallet: this.address, digest, chainId, signatures })
518518
}
519519

520-
async publishWitness(): Promise<void> {
520+
async publishWitness(chainId: ethers.BigNumberish = 0, referenceChainId?: ethers.BigNumberish): Promise<void> {
521521
const digest = ethers.id(`This is a Sequence account woo! ${Date.now()}`)
522522
// Apply ERC-6492 to undeployed children
523-
const signature = await this.signDigest(digest, 0, false, 'ignore', {cantValidateBehavior: "eip6492"})
523+
const signature = await this.signDigest(digest, 0, false, 'ignore', {referenceChainId, cantValidateBehavior: "eip6492"})
524524
const decoded = this.coders.signature.decode(signature)
525-
const recovered = await this.coders.signature.recover(decoded, { digest, chainId: 0, address: this.address })
525+
const recovered = await this.coders.signature.recover(decoded, { digest, chainId, address: this.address })
526526
const signatures = this.coders.signature.signaturesOf(recovered.config)
527-
return this.tracker.saveWitnesses({ wallet: this.address, digest, chainId: 0, signatures })
527+
const signaturesWithReferenceChainId = signatures.map(s => ({...s, referenceChainId}))
528+
return this.tracker.saveWitnesses({ wallet: this.address, digest, chainId, signatures: signaturesWithReferenceChainId })
528529
}
529530

530531
async signDigest(

packages/auth/src/services.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Account } from '@0xsequence/account'
2-
import { SequenceAPIClient } from '@0xsequence/api'
2+
import { GetAuthToken2Return, GetAuthTokenReturn, SequenceAPIClient } from '@0xsequence/api'
33
import { ETHAuth, Proof } from '@0xsequence/ethauth'
44
import { Indexer, SequenceIndexer } from '@0xsequence/indexer'
55
import { SequenceMetadata } from '@0xsequence/metadata'
@@ -99,7 +99,7 @@ export class Services {
9999
}
100100
}
101101

102-
auth(maxTries: number = 5): Promise<SequenceAPIClient> {
102+
auth(maxTries: number = 5, referenceChainId?: ChainIdLike): Promise<SequenceAPIClient> {
103103
if (this._initialAuthRequest) return this._initialAuthRequest
104104

105105
this._initialAuthRequest = (async () => {
@@ -109,7 +109,7 @@ export class Services {
109109
let jwtAuth: string | undefined
110110
for (let i = 1; ; i++) {
111111
try {
112-
jwtAuth = (await this.getJWT(true)).token
112+
jwtAuth = (await this.getJWT(true, referenceChainId)).token
113113
break
114114
} catch (error) {
115115
if (i === maxTries) {
@@ -125,7 +125,7 @@ export class Services {
125125
return this._initialAuthRequest
126126
}
127127

128-
private async getJWT(tryAuth: boolean): Promise<SessionJWT> {
128+
private async getJWT(tryAuth: boolean, referenceChainId?: ChainIdLike): Promise<SessionJWT> {
129129
const url = this.settings.sequenceApiUrl
130130
if (!url) throw Error('No sequence api url')
131131

@@ -154,7 +154,12 @@ export class Services {
154154
.then(async proofString => {
155155
const api = new SequenceAPIClient(url)
156156

157-
const authResp = await api.getAuthToken({ ewtString: proofString })
157+
let authResp: GetAuthToken2Return | GetAuthTokenReturn
158+
if (referenceChainId) {
159+
authResp = await api.getAuthToken2({ ewtString: proofString, chainID: referenceChainId?.toString() })
160+
} else {
161+
authResp = await api.getAuthToken({ ewtString: proofString })
162+
}
158163

159164
if (authResp?.status === true && authResp.jwtToken.length !== 0) {
160165
return authResp.jwtToken

packages/auth/src/session.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export class Session {
289289

290290
// sign a digest and send it to the tracker
291291
// otherwise the tracker will not know about this account
292-
await account.publishWitness()
292+
await account.publishWitness(undefined, referenceChainId)
293293

294294
// safety check, the remove tracker should be able to find
295295
// this account for the reference signer
@@ -303,7 +303,7 @@ export class Session {
303303

304304
if (services) {
305305
servicesObj = new Services(account, services)
306-
servicesObj.auth() // fire and forget
306+
servicesObj.auth(1, referenceChainId) // fire and forget
307307

308308
servicesObj.onAuth(result => {
309309
if (result.status === 'fulfilled') {

packages/core/src/commons/signature.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface SignatureCoder<
5454

5555
hashSetImageHash: (imageHash: string) => string
5656

57-
signaturesOf: (config: Y) => { address: string; signature: string }[]
57+
signaturesOf: (config: Y, referenceChainId?: ethers.BigNumberish) => { address: string; signature: string }[]
5858

5959
signaturesOfDecoded: (decoded: Z) => string[]
6060
}

packages/core/src/v1/signature.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export const SignatureCoder: base.SignatureCoder<WalletConfig, Signature, Unreco
248248
throw new Error('Image hash not supported on v1')
249249
},
250250

251-
signaturesOf(config: WalletConfig): { address: string; signature: string }[] {
251+
signaturesOf(config: WalletConfig, referenceChainId?: ethers.BigNumberish): { address: string; signature: string }[] {
252252
return config.signers.filter(s => s.signature !== undefined).map(s => ({ address: s.address, signature: s.signature! }))
253253
},
254254

packages/passkeys/src/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export type PasskeySignerContext = {
3838
}
3939

4040
export type PasskeySignMetadata = {
41-
cantValidateBehavior: 'ignore' | 'eip6492' | 'throw'
41+
referenceChainId?: ethers.BigNumberish
42+
cantValidateBehavior?: 'ignore' | 'eip6492' | 'throw'
4243
}
4344

4445
function bytesToBase64URL(bytes: Uint8Array): string {
@@ -180,7 +181,8 @@ export class SequencePasskeySigner implements signers.SapientSigner {
180181
}
181182

182183
async sign(digest: ethers.BytesLike, metadata: PasskeySignMetadata): Promise<ethers.BytesLike> {
183-
const subdigest = subDigestOf(await this.getAddress(), this.chainId, digest)
184+
const referenceChainId = metadata?.referenceChainId ?? this.chainId
185+
const subdigest = subDigestOf(await this.getAddress(), referenceChainId, ethers.hexlify(digest))
184186

185187
const signature = await this.doSign(digest, subdigest)
186188

@@ -208,7 +210,7 @@ export class SequencePasskeySigner implements signers.SapientSigner {
208210
// Pack the flags as hex string for encoding
209211
const flags = `0x${(
210212
(this.requireUserValidation ? 0x40 : 0) |
211-
(BigInt(this.chainId) === 0n ? 0x20 : 0) |
213+
(BigInt(referenceChainId) === 0n ? 0x20 : 0) |
212214
(this.requireBackupSanityCheck ? 0x10 : 0)
213215
).toString(16)}`
214216

packages/sessions/src/tracker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type ConfigDataDump = {
2222
export type SignerSignature = {
2323
address: string
2424
signature: string
25+
referenceChainId?: ethers.BigNumberish
2526
}
2627

2728
export interface ConfigTracker {

packages/sessions/src/trackers/remote/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class RemoteConfigTracker implements ConfigTracker, migrator.PresignedMig
8383
digest: args.digest,
8484
chainID: numberString(args.chainId),
8585
// Rename "address" to "signer"
86-
signatures: (filteredSignatures as SignerSignature[]).map(({ address, signature }) => ({ signer: address, signature }))
86+
signatures: (filteredSignatures as SignerSignature[]).map(({ address, signature, referenceChainId }) => ({ signer: address, signature, referenceChainId: referenceChainId?.toString() }))
8787
})
8888
}
8989
}

packages/sessions/src/trackers/remote/sessions.gen.ts

+60-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,70 @@
11
/* eslint-disable */
2-
// sessions v0.0.1 67f782e8acfe452f905078a7423ed5d27c6639a8
2+
// sessions v0.0.1 48681273e3b0249c5feb593b9af1b59dc6a14869
33
// --
4-
// Code generated by webrpc-gen@v0.20.3 with typescript generator. DO NOT EDIT.
4+
// Code generated by webrpc-gen@v0.21.3 with typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=sessions.ridl -target=typescript -client -out=./clients/sessions.gen.ts
77

8+
export const WebrpcHeader = "Webrpc"
9+
10+
export const WebrpcHeaderValue = "[email protected];[email protected];[email protected]"
11+
812
// WebRPC description and code-gen version
913
export const WebRPCVersion = "v1"
1014

1115
// Schema version of your RIDL schema
1216
export const WebRPCSchemaVersion = "v0.0.1"
1317

1418
// Schema hash generated from your RIDL schema
15-
export const WebRPCSchemaHash = "67f782e8acfe452f905078a7423ed5d27c6639a8"
19+
export const WebRPCSchemaHash = "48681273e3b0249c5feb593b9af1b59dc6a14869"
20+
21+
type WebrpcGenVersions = {
22+
webrpcGenVersion: string;
23+
codeGenName: string;
24+
codeGenVersion: string;
25+
schemaName: string;
26+
schemaVersion: string;
27+
};
28+
29+
export function VersionFromHeader(headers: Headers): WebrpcGenVersions {
30+
const headerValue = headers.get(WebrpcHeader);
31+
if (!headerValue) {
32+
return {
33+
webrpcGenVersion: "",
34+
codeGenName: "",
35+
codeGenVersion: "",
36+
schemaName: "",
37+
schemaVersion: "",
38+
};
39+
}
40+
41+
return parseWebrpcGenVersions(headerValue);
42+
}
43+
44+
function parseWebrpcGenVersions(header: string): WebrpcGenVersions {
45+
const versions = header.split(";");
46+
if (versions.length < 3) {
47+
return {
48+
webrpcGenVersion: "",
49+
codeGenName: "",
50+
codeGenVersion: "",
51+
schemaName: "",
52+
schemaVersion: "",
53+
};
54+
}
55+
56+
const [_, webrpcGenVersion] = versions[0].split("@");
57+
const [codeGenName, codeGenVersion] = versions[1].split("@");
58+
const [schemaName, schemaVersion] = versions[2].split("@");
59+
60+
return {
61+
webrpcGenVersion,
62+
codeGenName,
63+
codeGenVersion,
64+
schemaName,
65+
schemaVersion,
66+
};
67+
}
1668

1769
//
1870
// Types
@@ -128,9 +180,9 @@ export interface Signature {
128180
}
129181

130182
export interface SignerSignature {
131-
referenceChainID?: string
132183
signer?: string
133184
signature: string
185+
referenceChainID?: string
134186
}
135187

136188
export interface ConfigUpdate {
@@ -459,9 +511,12 @@ export class Sessions implements Sessions {
459511
}
460512

461513
const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => {
514+
const reqHeaders: {[key: string]: string} = { ...headers, 'Content-Type': 'application/json' }
515+
reqHeaders[WebrpcHeader] = WebrpcHeaderValue
516+
462517
return {
463518
method: 'POST',
464-
headers: { ...headers, 'Content-Type': 'application/json' },
519+
headers: reqHeaders,
465520
body: JSON.stringify(body || {}),
466521
signal
467522
}

0 commit comments

Comments
 (0)