-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { BytesLike } from 'ethers' | ||
import { Status, isSignerStatusPending } from '../orchestrator' | ||
import { SapientSigner } from './signer' | ||
|
||
export class InteractiveSigner implements SapientSigner { | ||
private readonly requests: Map<string, Request> = new Map() | ||
|
||
constructor( | ||
private readonly signer: SapientSigner, | ||
private readonly dependencies: string[] | ||
) {} | ||
|
||
async prompt(id: string) { | ||
const request = this.requests.get(id) | ||
|
||
if (request === undefined) { | ||
return | ||
} | ||
|
||
this.signer.requestSignature(id, request.message, request.metadata, { | ||
onSignature(signature) { | ||
request.callbacks.onSignature(signature) | ||
request.resolve() | ||
}, | ||
onRejection(_error) {}, | ||
onStatus(_situation) {} | ||
}) | ||
} | ||
|
||
getAddress(): Promise<string> { | ||
return this.signer.getAddress() | ||
} | ||
|
||
requestSignature( | ||
id: string, | ||
message: BytesLike, | ||
metadata: object, | ||
callbacks: { | ||
onSignature: (signature: BytesLike) => void | ||
onRejection: (error: string) => void | ||
onStatus: (situation: string) => void | ||
} | ||
): Promise<boolean> { | ||
return new Promise(resolve => { | ||
const request = { | ||
message, | ||
metadata, | ||
callbacks, | ||
autoPrompts: 0, | ||
resolve: () => resolve(true) | ||
} | ||
|
||
this.requests.set(id, request) | ||
|
||
// prompt immediately if we're not waiting for other signers | ||
if (this.dependencies.length === 0) { | ||
request.autoPrompts++ | ||
this.prompt(id) | ||
} | ||
}) | ||
} | ||
|
||
notifyStatusChange(id: string, status: Status, metadata: object): void { | ||
const request = this.requests.get(id) | ||
|
||
if (request === undefined) { | ||
return | ||
} | ||
|
||
request.metadata = metadata | ||
|
||
if (status.ended) { | ||
this.requests.delete(id) | ||
return | ||
} | ||
|
||
if (request.autoPrompts > 0) { | ||
return | ||
} | ||
|
||
const isPending = (dependency: string) => { | ||
const signerStatus = status.signers[dependency] | ||
return signerStatus !== undefined && isSignerStatusPending(signerStatus) | ||
} | ||
|
||
if (this.dependencies.some(isPending)) { | ||
return | ||
} | ||
|
||
request.autoPrompts++ | ||
this.prompt(id) | ||
} | ||
|
||
suffix(): BytesLike { | ||
return this.signer.suffix() | ||
} | ||
} | ||
|
||
type Request = { | ||
message: BytesLike | ||
metadata: object | ||
callbacks: { | ||
onSignature: (signature: BytesLike) => void | ||
onRejection: (error: string) => void | ||
onStatus: (situation: string) => void | ||
} | ||
autoPrompts: number | ||
resolve: () => void | ||
} |