Skip to content

Commit 906ef86

Browse files
committed
signhub: SapientSigner.requestSignature -> SapientSigner.sign
1 parent 1009d4f commit 906ef86

File tree

7 files changed

+48
-276
lines changed

7 files changed

+48
-276
lines changed

packages/account/src/orchestrator/wrapper.ts

+2-18
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,7 @@ export class AccountOrchestratorWrapper implements signers.SapientSigner {
4646
return this.account.decorateTransactions(bundle, status)
4747
}
4848

49-
async requestSignature(
50-
_id: string,
51-
message: ethers.utils.BytesLike,
52-
metadata: object,
53-
callbacks: {
54-
onSignature: (signature: ethers.utils.BytesLike) => void
55-
onRejection: (error: string) => void
56-
onStatus: (situation: string) => void
57-
}
58-
): Promise<boolean> {
49+
sign(message: ethers.utils.BytesLike, metadata: object): Promise<ethers.utils.BytesLike> {
5950
if (!commons.isWalletSignRequestMetadata(metadata)) {
6051
throw new Error('AccountOrchestratorWrapper only supports wallet metadata requests')
6152
}
@@ -67,14 +58,7 @@ export class AccountOrchestratorWrapper implements signers.SapientSigner {
6758

6859
// For Sequence nested signatures we must use `signDigest` and not `signMessage`
6960
// otherwise the account will hash the digest and the signature will be invalid.
70-
try {
71-
callbacks.onSignature(await this.account.signDigest(message, chainId, decorate, cantValidateBehavior, metadata))
72-
} catch (err) {
73-
callbacks.onRejection('Unable to sign account')
74-
return false
75-
}
76-
77-
return true
61+
return this.account.signDigest(message, chainId, decorate, cantValidateBehavior, metadata)
7862
}
7963

8064
notifyStatusChange(_i: string, _s: Status, _m: object): void {}

packages/guard/src/signer.ts

+12-48
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,9 @@ export class GuardSigner implements signers.SapientSigner {
3737
return bundle
3838
}
3939

40-
async requestSignature(
41-
_id: string,
42-
message: BytesLike,
43-
metadata: object,
44-
callbacks: {
45-
onSignature: (signature: BytesLike) => void
46-
onRejection: (error: string) => void
47-
onStatus: (situation: string) => void
48-
}
49-
): Promise<boolean> {
50-
const { onSignature, onRejection } = callbacks
51-
40+
async sign(message: BytesLike, metadata: object): Promise<BytesLike> {
5241
if (!commons.isWalletSignRequestMetadata(metadata)) {
53-
onRejection('expected sequence signature request metadata')
54-
return false
42+
throw new Error('expected sequence signature request metadata')
5543
}
5644

5745
const guardTotpCode = (metadata as { guardTotpCode?: string }).guardTotpCode
@@ -61,8 +49,8 @@ export class GuardSigner implements signers.SapientSigner {
6149
const coder = universal.genericCoderFor(metadata.config.version)
6250
const { encoded } = coder.signature.encodeSigners(metadata.config, metadata.parts ?? new Map(), [], metadata.chainId)
6351

64-
try {
65-
const { sig: signature } = await this.guard.signWith({
52+
return (
53+
await this.guard.signWith({
6654
signer: this.address,
6755
request: {
6856
msg: ethers.utils.hexlify(message),
@@ -71,17 +59,7 @@ export class GuardSigner implements signers.SapientSigner {
7159
},
7260
token: guardTotpCode ? { id: AuthMethod.TOTP, token: guardTotpCode } : undefined
7361
})
74-
75-
if (ethers.utils.arrayify(signature).length === 0) {
76-
throw new Error('guard response contained no signature data')
77-
}
78-
79-
onSignature(signature)
80-
return true
81-
} catch (error) {
82-
onRejection(`unable to request guard signature: ${error.message ?? error.msg ?? error}`)
83-
return false
84-
}
62+
).sig
8563
}
8664

8765
notifyStatusChange(_id: string, _status: Status, _metadata: object): void {}
@@ -248,27 +226,13 @@ async function signOwnershipProof(
248226
const timestamp = new Date()
249227
const typedData = getOwnershipProofTypedData(proof.walletAddress, timestamp)
250228
const digest = encodeTypedDataDigest(typedData)
251-
const randomId = ethers.utils.hexlify(ethers.utils.randomBytes(32))
252-
253-
return new Promise((resolve, reject) =>
254-
signer.requestSignature(
255-
randomId,
256-
digest,
257-
{},
258-
{
259-
onSignature(signature) {
260-
resolve({
261-
walletAddress: proof.walletAddress,
262-
timestamp,
263-
signerAddress,
264-
signature: ethers.utils.hexlify(signature)
265-
})
266-
},
267-
onRejection: reject,
268-
onStatus(_situation) {}
269-
}
270-
)
271-
)
229+
230+
return {
231+
walletAddress: proof.walletAddress,
232+
timestamp,
233+
signerAddress,
234+
signature: ethers.utils.hexlify(await signer.sign(digest, {}))
235+
}
272236
}
273237
}
274238

packages/signhub/src/orchestrator.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,17 @@ export class Orchestrator {
190190
signers.map(async s => {
191191
const saddr = await s.getAddress()
192192
status.signers[saddr] = { situation: InitialSituation }
193-
return s.requestSignature(id, message, metadata ?? {}, {
194-
onSignature: signature => {
195-
const suffix = s.suffix()
196-
status.signers[saddr] = { signature, suffix }
197-
onStatusUpdate()
198-
},
199-
onRejection: error => {
200-
status.signers[saddr] = { rejected: true, error }
201-
onStatusUpdate()
202-
},
203-
onStatus: situation => {
204-
status.signers[saddr] = { situation }
205-
onStatusUpdate()
206-
}
207-
})
193+
try {
194+
const signature = await s.sign(message, metadata ?? {})
195+
const suffix = s.suffix()
196+
status.signers[saddr] = { signature, suffix }
197+
onStatusUpdate()
198+
return true
199+
} catch (error) {
200+
status.signers[saddr] = { rejected: true, error }
201+
onStatusUpdate()
202+
return false
203+
}
208204
})
209205
)
210206

packages/signhub/src/signers/signer.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@ export interface SapientSigner {
2323
/**
2424
* Request a signature from the signer.
2525
*/
26-
requestSignature(
27-
id: string,
28-
message: ethers.BytesLike,
29-
metadata: object,
30-
callbacks: {
31-
onSignature: (signature: ethers.BytesLike) => void
32-
onRejection: (error: string) => void
33-
onStatus: (situation: string) => void
34-
}
35-
): Promise<boolean>
26+
sign(message: ethers.BytesLike, metadata: object): Promise<ethers.BytesLike>
3627

3728
/**
3829
* Notify the signer of a status change.
@@ -48,7 +39,7 @@ export function isSapientSigner(signer: ethers.Signer | SapientSigner): signer i
4839
(signer as SapientSigner).buildDeployTransaction !== undefined &&
4940
(signer as SapientSigner).predecorateSignedTransactions !== undefined &&
5041
(signer as SapientSigner).decorateTransactions !== undefined &&
51-
(signer as SapientSigner).requestSignature !== undefined &&
42+
(signer as SapientSigner).sign !== undefined &&
5243
(signer as SapientSigner).notifyStatusChange !== undefined
5344
)
5445
}

packages/signhub/src/signers/wrapper.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,8 @@ export class SignerWrapper implements SapientSigner {
2929
return bundle
3030
}
3131

32-
async requestSignature(
33-
_id: string,
34-
message: ethers.BytesLike,
35-
_metadata: object,
36-
callbacks: {
37-
onSignature: (signature: ethers.BytesLike) => void
38-
onRejection: (error: string) => void
39-
onStatus: (situation: string) => void
40-
}
41-
): Promise<boolean> {
42-
callbacks.onSignature(await this.signer.signMessage(message))
43-
return true
32+
sign(message: ethers.utils.BytesLike, metadata: object): Promise<ethers.utils.BytesLike> {
33+
return this.signer.signMessage(message)
4434
}
4535

4636
notifyStatusChange(_i: string, _s: Status, _m: object): void {}

0 commit comments

Comments
 (0)