Skip to content

Commit

Permalink
Merge pull request #312 from bob-collective/fix/update_gateway_example
Browse files Browse the repository at this point in the history
fix: use scure/btc-signer over bitcoinjs in gateway
  • Loading branch information
nud3l committed Aug 15, 2024
2 parents 2fadf27 + 25fcfa5 commit 190f7c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
17 changes: 5 additions & 12 deletions docs/docs/build/bob-sdk/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ We recommend using our [sats-wagmi](./sats-wagmi.md) package to query your user'

```ts title="/src/utils/gateway.ts"
import { createTransfer } from "@gobob/bob-sdk";
import * as bitcoin from "bitcoinjs-lib";
import { AddressType, getAddressInfo } from "bitcoin-address-validation";
import { hex } from "@scure/base";
import { Transaction as SigTx } from "@scure/btc-signer";
import { Transaction } from '@scure/btc-signer';

const tx = await createTxWithOpReturn(
fromAddress,
Expand All @@ -111,7 +109,7 @@ async function createTxWithOpReturn(
amount: number,
opReturn: string,
fromPubKey?: string
): Promise<bitcoin.Transaction> {
): Promise<Buffer> {
const addressType = getAddressInfo(fromAddress).type;

// Ensure this is not the P2TR address for ordinals (we don't want to spend from it)
Expand Down Expand Up @@ -140,15 +138,10 @@ async function createTxWithOpReturn(
opReturn
);
const psbt = unsignedTx.toPSBT(0);
const psbtHex = hex.encode(psbt);
const signedPsbtHex = psbtHex;
const signedTx = SigTx.fromPSBT(
bitcoin.Psbt.fromHex(signedPsbtHex).toBuffer()
);
signedTx.finalize();
const tx = bitcoin.Transaction.fromBuffer(Buffer.from(signedTx.extract()));

return tx;
const signedTx = Transaction.fromPSBT(psbt);

return Buffer.from(signedTx.extract())
}
```

Expand Down
15 changes: 4 additions & 11 deletions sdk/examples/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { GatewayApiClient } from "../src/gateway";
import * as bitcoin from "bitcoinjs-lib";
import { AddressType, getAddressInfo } from "bitcoin-address-validation";
import { createTransfer } from "../src/wallet/utxo";
import { hex } from '@scure/base';
import { Transaction as SigTx } from '@scure/btc-signer';
import { Transaction } from '@scure/btc-signer';

const BOB_TBTC_V2_TOKEN_ADDRESS = "0xBBa2eF945D523C4e2608C9E1214C2Cc64D4fc2e2";

Expand All @@ -25,7 +23,7 @@ export async function swapBtcForToken(evmAddress: string) {

}

async function createTxWithOpReturn(fromAddress: string, toAddress: string, amount: number, opReturn: string, fromPubKey?: string): Promise<bitcoin.Transaction> {
async function createTxWithOpReturn(fromAddress: string, toAddress: string, amount: number, opReturn: string, fromPubKey?: string): Promise<Buffer> {
const addressType = getAddressInfo(fromAddress).type;

// Ensure this is not the P2TR address for ordinals (we don't want to spend from it)
Expand All @@ -51,13 +49,8 @@ async function createTxWithOpReturn(fromAddress: string, toAddress: string, amou
);

const psbt = unsignedTx.toPSBT(0);
const psbtHex = hex.encode(psbt);

// TODO: sign PSBT
const signedPsbtHex = psbtHex;
const signedTx = Transaction.fromPSBT(psbt);

const signedTx = SigTx.fromPSBT(bitcoin.Psbt.fromHex(signedPsbtHex).toBuffer());
signedTx.finalize();

return bitcoin.Transaction.fromBuffer(Buffer.from(signedTx.extract()));
return Buffer.from(signedTx.extract())
}

0 comments on commit 190f7c2

Please sign in to comment.