-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ledger): stacks signing, closes #4420
- Loading branch information
1 parent
7afc103
commit cad4b0d
Showing
14 changed files
with
161 additions
and
64 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
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,64 @@ | ||
import type { StacksTransaction } from '@stacks/transactions'; | ||
|
||
type PubTypeFn<E> = <Key extends string & keyof E>( | ||
event: Key, | ||
// Ensures if we have an event with no payload, the second arg can be empty, | ||
// rather than `undefined` | ||
...message: null extends E[Key] ? [data?: never] : [data: E[Key]] | ||
) => void; | ||
|
||
type SubTypeFn<E> = <Key extends string & keyof E>( | ||
event: Key, | ||
fn: (message: E[Key]) => void | ||
) => void; | ||
|
||
type MessageFn<E> = <Key extends string & keyof E>(message: E[Key]) => void; | ||
|
||
interface PubSubType<E> { | ||
publish: PubTypeFn<E>; | ||
subscribe: SubTypeFn<E>; | ||
unsubscribe: SubTypeFn<E>; | ||
} | ||
|
||
export function PublishSubscribe<E>(): PubSubType<E> { | ||
const handlers: { [key: string]: MessageFn<any>[] } = {}; | ||
|
||
return { | ||
publish(event, msg?) { | ||
handlers[event].forEach(h => h(msg)); | ||
}, | ||
|
||
subscribe(event, callback) { | ||
const list = handlers[event] ?? []; | ||
list.push(callback); | ||
handlers[event] = list; | ||
}, | ||
|
||
unsubscribe(event, callback) { | ||
let list = handlers[event] ?? []; | ||
list = list.filter(h => h !== callback); | ||
handlers[event] = list; | ||
}, | ||
}; | ||
} | ||
|
||
// Global app events. Only add events if your feature isn't capable of | ||
//communicating internally. | ||
export interface GlobalAppEvents { | ||
ledgerStacksTxSigned: { | ||
unsignedTx: string; | ||
signedTx: StacksTransaction; | ||
}; | ||
ledgerStacksTxCancelled: null; | ||
} | ||
|
||
export const appEvents = PublishSubscribe<GlobalAppEvents>(); | ||
|
||
// function listenForNextEvent<T extends keyof GlobalAppEvents>(name: T): Promise<GlobalAppEvents[T]> { | ||
// return new Promise(resolve => { | ||
// appEvents.subscribe(name, msg => { | ||
// appEvents.unsubscribe(name, resolve); | ||
// resolve(msg); | ||
// }); | ||
// }); | ||
// } |
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
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
17 changes: 17 additions & 0 deletions
17
src/app/features/ledger/flows/stacks-tx-signing/stacks-tx-signing-event-listeners.ts
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,17 @@ | ||
import type { StacksTransaction } from '@stacks/transactions'; | ||
|
||
import { GlobalAppEvents, appEvents } from '@app/common/publish-subscribe'; | ||
|
||
export async function listenForStacksTxLedgerSigning( | ||
unsignedTx: string | ||
): Promise<StacksTransaction> { | ||
return new Promise(resolve => { | ||
function handler(msg: GlobalAppEvents['ledgerStacksTxSigned']) { | ||
if (msg.unsignedTx === unsignedTx) { | ||
appEvents.unsubscribe('ledgerStacksTxSigned', handler); | ||
resolve(msg.signedTx); | ||
} | ||
} | ||
appEvents.subscribe('ledgerStacksTxSigned', handler); | ||
}); | ||
} |
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
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
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
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
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
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
Oops, something went wrong.