Skip to content

Commit

Permalink
feat: add scriptType param
Browse files Browse the repository at this point in the history
  • Loading branch information
appbak3r committed Aug 14, 2024
1 parent 66baf89 commit db783c2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const onRpcRequest = async ({origin, request}: RpcRequest) => {
snap,
request.params.message,
request.params.protocol,
request.params.scriptType
);

default:
Expand Down
1 change: 1 addition & 0 deletions packages/snap/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface SignMessage {
params: {
message: string;
protocol: 'ecdsa' | 'bip322'
scriptType?: ScriptType.P2SH_P2WPKH | ScriptType.P2WPKH;
};
}

Expand Down
9 changes: 6 additions & 3 deletions packages/snap/src/rpc/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const signMessage = async (
message: string,
// TODO: implement bip322 message signing
protocol: 'ecdsa' | 'bip322' = 'ecdsa',
scriptType: ScriptType = ScriptType.P2SH_P2WPKH,

) => {
if (protocol !== 'ecdsa') {
throw SnapError.of(RequestErrors.ActionNotSupport);
Expand All @@ -27,7 +29,7 @@ export const signMessage = async (
);

const btcNetwork = getNetwork(snapNetwork);
const path = [...pathMap[ScriptType.P2SH_P2WPKH], "0'", '0', '0'];
const path = [...pathMap[scriptType], "0'", '0', '0'];

if (snapNetwork !== BitcoinNetwork.Main) {
path[2] = "1'";
Expand All @@ -48,9 +50,10 @@ export const signMessage = async (
params: {
type: 'confirmation',
content: panel([
heading('Sign Bitcoin Message'),
heading('Signature Request'),
text(`Please verify this sign message request from ${domain}`),
divider(),
text("Only confirm this message if you approve the content and trust the requesting site."),
panel([
text(`**Address**:\n ${address}`),
text(`**Network**:\n ${snapNetwork === BitcoinNetwork.Main ? 'Mainnet' : 'Testnet'}`),
Expand All @@ -67,7 +70,7 @@ export const signMessage = async (
}

const signature = bitcoinMessage.sign(message, privateKey, true, {
segwitType: "p2sh(p2wpkh)"
segwitType: scriptType === ScriptType.P2WPKH ? "p2wpkh" : "p2sh(p2wpkh)",
});

return {
Expand Down

0 comments on commit db783c2

Please sign in to comment.