-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ledger): stacks message signing, closes #4945
- Loading branch information
1 parent
105fedb
commit cc19e40
Showing
11 changed files
with
139 additions
and
57 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
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
29 changes: 29 additions & 0 deletions
29
...pp/features/ledger/flows/stacks-message-signing/stacks-message-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,29 @@ | ||
import isEqual from 'lodash.isequal'; | ||
|
||
import type { UnsignedMessage } from '@shared/signature/signature-types'; | ||
|
||
import { GlobalAppEvents, appEvents } from '@app/common/publish-subscribe'; | ||
|
||
export async function listenForStacksMessageSigning( | ||
unsignedMessage: UnsignedMessage | ||
): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
function stacksMessageSignedHandler(msg: GlobalAppEvents['ledgerStacksMessageSigned']) { | ||
if (isEqual(msg.unsignedMessage, unsignedMessage)) { | ||
appEvents.unsubscribe('ledgerStacksMessageSigned', stacksMessageSignedHandler); | ||
appEvents.unsubscribe('ledgerStacksMessageSigningCancelled', signingAbortedHandler); | ||
resolve(msg.messageSignatures); | ||
} | ||
} | ||
appEvents.subscribe('ledgerStacksMessageSigned', stacksMessageSignedHandler); | ||
|
||
function signingAbortedHandler(msg: GlobalAppEvents['ledgerStacksMessageSigningCancelled']) { | ||
if (isEqual(msg.unsignedMessage, unsignedMessage)) { | ||
appEvents.unsubscribe('ledgerStacksMessageSigningCancelled', signingAbortedHandler); | ||
appEvents.unsubscribe('ledgerStacksMessageSigned', stacksMessageSignedHandler); | ||
reject(new Error('User cancelled the signing operation')); | ||
} | ||
} | ||
appEvents.subscribe('ledgerStacksMessageSigningCancelled', signingAbortedHandler); | ||
}); | ||
} |
22 changes: 11 additions & 11 deletions
22
src/app/features/ledger/flows/stacks-message-signing/use-message-type.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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { ClarityValue } from '@stacks/transactions'; | ||
|
||
import { StructuredMessageDataDomain, UnsignedMessage } from '@shared/signature/signature-types'; | ||
import { | ||
type SignedMessageType, | ||
UnsignedMessage, | ||
deserializeUnsignedMessage, | ||
} from '@shared/signature/signature-types'; | ||
import { isString } from '@shared/utils'; | ||
|
||
import { useLocationStateWithCache } from '@app/common/hooks/use-location-state'; | ||
|
||
export function useUnsignedMessageType(): UnsignedMessage | null { | ||
const message = useLocationStateWithCache<string | ClarityValue>('message'); | ||
const domain = useLocationStateWithCache<StructuredMessageDataDomain>('domain'); | ||
const messageType = useLocationStateWithCache<SignedMessageType>('messageType'); | ||
const message = useLocationStateWithCache<string | Uint8Array>('message'); | ||
const domain = useLocationStateWithCache<Uint8Array>('domain'); | ||
|
||
if (isString(message)) | ||
return { | ||
messageType: 'utf8', | ||
message, | ||
}; | ||
if (messageType === 'utf8' && isString(message)) return { messageType, message }; | ||
|
||
if (message && domain) return { messageType: 'structured', message, domain }; | ||
if (messageType === 'structured' && message && !isString(message) && domain) | ||
return deserializeUnsignedMessage({ messageType, message, domain }); | ||
|
||
return null; | ||
} |
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.