-
Notifications
You must be signed in to change notification settings - Fork 46
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 #267 from bob-collective/refactor/sdk
refactor: import gateway client and updated wallet logic
- Loading branch information
Showing
24 changed files
with
881 additions
and
165 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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'; | ||
|
||
const BOB_TBTC_V2_TOKEN_ADDRESS = "0xBBa2eF945D523C4e2608C9E1214C2Cc64D4fc2e2"; | ||
|
||
export async function swapBtcForToken(evmAddress: string) { | ||
const gatewayClient = new GatewayApiClient("https://onramp-api-mainnet.gobob.xyz"); | ||
|
||
const amount = 10000; // 0.0001 BTC | ||
const { fee: _fee, onramp_address: onrampAddress, bitcoin_address: bitcoinAddress, gratuity: _gratuity } = await gatewayClient.getQuote( | ||
BOB_TBTC_V2_TOKEN_ADDRESS, | ||
amount | ||
); | ||
|
||
const orderId = await gatewayClient.createOrder(onrampAddress, evmAddress, amount); | ||
|
||
const tx = await createTxWithOpReturn("bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d", bitcoinAddress, amount, evmAddress); | ||
|
||
// NOTE: relayer should broadcast the tx | ||
await gatewayClient.updateOrder(orderId, tx.toHex()); | ||
|
||
} | ||
|
||
async function createTxWithOpReturn(fromAddress: string, toAddress: string, amount: number, opReturn: string, fromPubKey?: string): Promise<bitcoin.Transaction> { | ||
const addressType = getAddressInfo(fromAddress).type; | ||
|
||
// Ensure this is not the P2TR address for ordinals (we don't want to spend from it) | ||
if (addressType === AddressType.p2tr) { | ||
throw new Error('Cannot transfer using Taproot (P2TR) address. Please use another address type.'); | ||
} | ||
|
||
// We need the public key to generate the redeem and witness script to spend the scripts | ||
if (addressType === (AddressType.p2sh || AddressType.p2wsh)) { | ||
if (!fromPubKey) { | ||
throw new Error('Public key is required to spend from the selected address type'); | ||
} | ||
} | ||
|
||
const unsignedTx = await createTransfer( | ||
'mainnet', | ||
addressType, | ||
fromAddress, | ||
toAddress, | ||
amount, | ||
fromPubKey, | ||
opReturn, | ||
); | ||
|
||
const psbt = unsignedTx.toPSBT(0); | ||
const psbtHex = hex.encode(psbt); | ||
|
||
// TODO: sign PSBT | ||
const signedPsbtHex = psbtHex; | ||
|
||
const signedTx = SigTx.fromPSBT(bitcoin.Psbt.fromHex(signedPsbtHex).toBuffer()); | ||
signedTx.finalize(); | ||
|
||
return bitcoin.Transaction.fromBuffer(Buffer.from(signedTx.extract())); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.