forked from snapdao/btcsnap
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from bob-collective/feat/sign-message
feat: sign message
- Loading branch information
Showing
9 changed files
with
121 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { signMessage } from '../signMessage'; | ||
import { SnapMock } from '../__mocks__/snap'; | ||
import { bip44, MessageAndSig } from "./fixtures/bitcoinNode"; | ||
|
||
describe('signLNInvoice', () => { | ||
const snapStub = new SnapMock(); | ||
|
||
afterEach(() => { | ||
snapStub.reset() | ||
}) | ||
|
||
it('should return signature for message', async () => { | ||
snapStub.rpcStubs.snap_dialog.mockResolvedValue(true); | ||
snapStub.rpcStubs.snap_getBip32Entropy.mockResolvedValue(bip44.slip10Node); | ||
const signature = await signMessage(MessageAndSig.domain, snapStub, MessageAndSig.message, MessageAndSig.hdPath); | ||
expect(signature).toBe(MessageAndSig.signature); | ||
}) | ||
|
||
it('should reject the sign request and throw error if user reject the sign the lightning invoice', async () => { | ||
snapStub.rpcStubs.snap_dialog.mockResolvedValue(false); | ||
|
||
await expect(signMessage(MessageAndSig.domain, snapStub, MessageAndSig.message, MessageAndSig.hdPath)) | ||
.rejects | ||
.toThrowError('User reject the sign request'); | ||
}) | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Snap } from '../interface'; | ||
import { getHDNode } from '../utils/getHDNode'; | ||
import bitcoinMessage from 'bitcoinjs-message'; | ||
import { RequestErrors, SnapError } from '../errors'; | ||
import { divider, heading, panel, text } from "@metamask/snaps-ui"; | ||
|
||
export async function signMessage( | ||
domain: string, | ||
snap: Snap, | ||
message: string, | ||
hdPath: string, | ||
): Promise<string> { | ||
const result = await snap.request({ | ||
method: 'snap_dialog', | ||
params: { | ||
type: 'confirmation', | ||
content: panel([ | ||
heading('Sign Message'), | ||
text(`Please sign this message from ${domain}`), | ||
divider(), | ||
text(`${message}`), | ||
]), | ||
}, | ||
}); | ||
|
||
if (result) { | ||
const privateKey = (await getHDNode(snap, hdPath)).privateKey; | ||
const signature = bitcoinMessage | ||
.sign(message, privateKey, true) | ||
.toString('hex'); | ||
return signature; | ||
} else { | ||
throw SnapError.of(RequestErrors.RejectSign); | ||
} | ||
} |