Skip to content

Commit

Permalink
Merge pull request #16 from bob-collective/refactor/sign-psbt
Browse files Browse the repository at this point in the history
refactor: return psbt, auto-finalize
  • Loading branch information
gregdhill authored May 21, 2024
2 parents 4622084 + 5ba8c5d commit 2cd5cbf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "2.0.11",
"version": "2.1.0",
"description": "BOB: Metamask snap to manage your Bitcoin",
"proposedName": "BOB",
"repository": {
"type": "git",
"url": "https://github.com/bob-collective/bob-snap"
},
"source": {
"shasum": "Ea+2ea/YTw965c+UA8zhzQIQ8CWIV3yEt5llK6YUjkE=",
"shasum": "RxgmYv7eYlRsqDEBd1D8ilbcfoA6zXd0U6qr+qsPEBw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
15 changes: 13 additions & 2 deletions packages/snap/src/bitcoin/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ changeAddress: tb1qx5wzl3f27d6dgzk9u7h47pqdts60xdpax4w5rf

it('should be able to sign transaction and extract txId and txHex', () => {
const tx = new BtcPsbt(psbtFixture.base64, BitcoinNetwork.Test);
const { txId, txHex } = tx.signPsbt(signer);
const psbt = Psbt.fromBase64(tx.signPsbt(signer));
expect(psbt.data.inputs[0].finalScriptWitness).toBeDefined();
const txId = psbt.extractTransaction().getId();
const txHex = psbt.extractTransaction().toHex();
expect(txId).toBe(
'4db856b1b7049cce26dc298458796f66d0dee39a5d0651b4c51aa0c66326adec',
);
Expand All @@ -115,6 +118,12 @@ changeAddress: tb1qx5wzl3f27d6dgzk9u7h47pqdts60xdpax4w5rf
);
});

it('should be able to sign transaction without finalizing', () => {
const tx = new BtcPsbt(psbtFixture.base64, BitcoinNetwork.Test);
const psbt = Psbt.fromBase64(tx.signPsbt(signer, { autoFinalize: false }));
expect(psbt.data.inputs[0].finalScriptWitness).toBeUndefined();
});

it('should be able to sign taproot transaction and extract txId and txHex', () => {
bitcoin.initEccLib(ecc);

Expand Down Expand Up @@ -154,7 +163,9 @@ changeAddress: tb1qx5wzl3f27d6dgzk9u7h47pqdts60xdpax4w5rf
.toBase64();

const tx = new BtcPsbt(psbtBase64, BitcoinNetwork.Test);
const { txId, txHex } = tx.signPsbt(signer);
const psbt = Psbt.fromBase64(tx.signPsbt(signer));
const txId = psbt.extractTransaction().getId();
const txHex = psbt.extractTransaction().toHex();
expect(txId).toBe(
'e0a70feca465add48d89fade7c66b68589fc95f21c8e69e3121a4bd6cee3302d',
);
Expand Down
13 changes: 5 additions & 8 deletions packages/snap/src/bitcoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,15 @@ export class BtcPsbt {
}
}

signPsbt(accountSigner: AccountSigner, opts: SignPsbtOptions = {}) {
signPsbt(accountSigner: AccountSigner, opts: SignPsbtOptions = { autoFinalize: true }) {
try {
const signInputOpts = opts.signInputOpts !== undefined ? opts.signInputOpts : new Array<SignInputOptions>(this.psbt.data.inputs.length).fill({});
signAllInputsHD(this.psbt, accountSigner, signInputOpts);
if (validateSignaturesOfAllInputs(this.psbt, validator, schnorrValidator)) {
if (!opts.autoFinalize) {
return this.psbt.toBase64();
} else if (validateSignaturesOfAllInputs(this.psbt, validator, schnorrValidator)) {
this.psbt.finalizeAllInputs();
const txId = this.psbt.extractTransaction().getId();
const txHex = this.psbt.extractTransaction().toHex();
return {
txId,
txHex
}
return this.psbt.toBase64();
} else {
throw new Error('Signature verification failed');
}
Expand Down
1 change: 0 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const onRpcRequest = async ({ origin, request }: RpcRequest) => {
origin,
snap,
);
// keep this for backwards compatibility
case 'btc_signPsbt':
return signPsbt(
origin,
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 @@ -12,6 +12,7 @@ export interface GetAllXpubsRequest {
}

export interface SignPsbtOptions {
autoFinalize: boolean,
signInputOpts?: SignInputOptions[];
}

Expand Down
6 changes: 3 additions & 3 deletions packages/snap/src/rpc/signPSBT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import { heading, panel, text, divider } from "@metamask/snaps-ui";
export async function signPsbt(
domain: string,
snap: Snap,
psbt: string,
psbtBase64: string,
network: BitcoinNetwork,
scriptType: ScriptType,
opts?: SignPsbtOptions
): Promise<{ txId: string, txHex: string }> {
): Promise<string> {
const snapNetwork = await getPersistedData<BitcoinNetwork>(snap, "network", '' as BitcoinNetwork);
if (snapNetwork != network) {
throw SnapError.of(RequestErrors.NetworkNotMatch);
}

const btcPsbt = new BtcPsbt(psbt, snapNetwork);
const btcPsbt = new BtcPsbt(psbtBase64, snapNetwork);
const txDetails = btcPsbt.extractPsbtJson()

const result = await snap.request({
Expand Down

0 comments on commit 2cd5cbf

Please sign in to comment.