Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use scure/btc-signer over bitcoinjs in gateway #312

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
nud3l marked this conversation as resolved.
Show resolved Hide resolved
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())
nud3l marked this conversation as resolved.
Show resolved Hide resolved
}
Loading