Skip to content

Commit

Permalink
fix: update defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Dec 17, 2024
1 parent cadb2a0 commit 36a2065
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/sbtc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sbtc",
"version": "0.3.0",
"version": "0.3.1",
"description": "Library for sBTC.",
"license": "MIT",
"author": "Hiro Systems PBC (https://hiro.so)",
Expand Down
23 changes: 16 additions & 7 deletions packages/sbtc/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export class SbtcApiClient {

async fetchUtxos(address: string): Promise<UtxoWithTx[]> {
return (
fetch(`${this.config.btcApiUrl}/address/${address}/utxo`)
fetch(`${this.config.btcApiUrl}/address/${address}/utxo`, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then(res => res.json())
// .then((utxos: MempoolApiUtxo[]) =>
// utxos.sort((a, b) => a.status.block_height - b.status.block_height)
Expand All @@ -95,7 +100,7 @@ export class SbtcApiClient {
}

async fetchFeeRates(): Promise<MempoolFeeEstimates> {
return fetch(`${this.config.btcApiUrl}/fees/recommended`).then(res => res.json());
return fetch(`${this.config.btcApiUrl}/v1/fees/recommended`).then(res => res.json());
}

async fetchFeeRate(target: 'low' | 'medium' | 'high'): Promise<number> {
Expand All @@ -107,9 +112,15 @@ export class SbtcApiClient {
async broadcastTx(tx: btc.Transaction): Promise<string> {
return await fetch(`${this.config.btcApiUrl}/tx`, {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
headers: { Accept: 'text/plain', 'Content-Type': 'text/plain' },
body: tx.hex,
}).then(res => res.json() as Promise<string>);
}).then(res => {
try {
return res.text() as Promise<string>;
} catch (e) {
return res.json() as Promise<string>; // the proxy might need a fallback decode
}
});
}

async notifySbtc({
Expand Down Expand Up @@ -146,9 +157,7 @@ export class SbtcApiClient {
functionName: 'get-current-aggregate-pubkey',
functionArgs: [],
senderAddress: STACKS_DEVNET.bootAddress, // zero address
client: {
baseUrl: this.config.stxApiUrl,
},
client: { baseUrl: this.config.stxApiUrl },
})) as BufferCV;

return res.value.slice(2);
Expand Down
30 changes: 17 additions & 13 deletions packages/sbtc/src/transactions/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
stacksAddressBytes,
} from '../utils';

export const DEFAULT_RECLAIM_LOCK_TIME = 12;
export const DEFAULT_MAX_SIGNER_FEE = 80_000;

/** Taken from [bip-0341.mediawiki](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#user-content-Constructing_and_spending_Taproot_outputs) and [sbtc](https://github.com/stacks-network/sbtc/blob/a3a927f759871440962d8f8066108e5b0af696a0/sbtc/src/lib.rs#L28) */
export const UNSPENDABLE_PUB = new Uint8Array([
0x50, 0x92, 0x9b, 0x74, 0xc1, 0xa0, 0x49, 0x54, 0xb7, 0x8b, 0x4b, 0x60, 0x35, 0xe9, 0x7a, 0x5e,
Expand Down Expand Up @@ -148,8 +151,8 @@ export function buildSbtcReclaimTx({
bitcoinAddress,
stacksAddress,
signersPublicKey,
maxSignerFee,
reclaimLockTime = 144,
maxSignerFee = DEFAULT_MAX_SIGNER_FEE,
reclaimLockTime = DEFAULT_RECLAIM_LOCK_TIME,
reclaimPublicKey,
txid,
vout = 0,
Expand All @@ -163,9 +166,9 @@ export function buildSbtcReclaimTx({
stacksAddress: string;
/** The signers public key (aggregated schnorr; needed to reconstruct the deposit tx) */
signersPublicKey: string;
/** The max signer fee (needed to reconstruct the deposit tx) */
maxSignerFee: number;
/** The lock time (needed to reconstruct the deposit tx), defaults to 144 */
/** The max signer fee (needed to reconstruct the deposit tx), defaults to 80_000 */
maxSignerFee?: number;
/** The lock time (needed to reconstruct the deposit tx), defaults to 12 */
reclaimLockTime?: number;
/** The reclaim public key (schnorr; to reconstruct the deposit tx AND sign the reclaim tx) */
reclaimPublicKey: string;
Expand All @@ -180,8 +183,8 @@ export function buildSbtcReclaimTx({
/** Fee rate in sat/vbyte for the reclaim tx */
feeRate: number;
}) {
const tx = new btc.Transaction({ allowUnknownInputs: true });
if (tx.version < 2) throw new Error('Transaction version must be >= 2');
const tx = new btc.Transaction({ allowUnknownInputs: true, allowUnknownOutputs: true });
if (tx.version !== 2) throw new Error('Transaction version must be == 2');

const deposit = buildSbtcDepositTr({
network,
Expand Down Expand Up @@ -227,16 +230,17 @@ export function buildSbtcDepositAddress({
network = MAINNET,
stacksAddress,
signersPublicKey,
maxSignerFee,
reclaimLockTime = 144,
maxSignerFee = DEFAULT_MAX_SIGNER_FEE,
reclaimLockTime = DEFAULT_RECLAIM_LOCK_TIME,
reclaimPublicKey,
}: {
network: BitcoinNetwork;
stacksAddress: string;
/** Aggregated (schnorr) public key of all signers */
signersPublicKey: string;
maxSignerFee: number;
/** The lock time (needed to reconstruct the deposit tx), defaults to 144 */
/** The max signer fee (needed to reconstruct the deposit tx), defaults to 80_000 */
maxSignerFee?: number;
/** The lock time (needed to reconstruct the deposit tx), defaults to 12 */
reclaimLockTime?: number;
/** The reclaim public key (schnorr; to reconstruct the deposit tx AND sign the reclaim tx) */
reclaimPublicKey: string;
Expand Down Expand Up @@ -301,8 +305,8 @@ export async function sbtcDepositHelper({
utxoToSpendable = DEFAULT_UTXO_TO_SPENDABLE,
paymentPublicKey,
reclaimPublicKey,
maxSignerFee = 80_000,
reclaimLockTime = 144,
maxSignerFee = DEFAULT_MAX_SIGNER_FEE,
reclaimLockTime = DEFAULT_RECLAIM_LOCK_TIME,
}: {
/** Bitcoin network, defaults to REGTEST */
network?: BitcoinNetwork;
Expand Down
5 changes: 2 additions & 3 deletions packages/sbtc/tests/helpers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { ProjectivePoint } from '@noble/secp256k1';
import { HDKey } from '@scure/bip32';
import * as bip39 from '@scure/bip39';
import * as btc from '@scure/btc-signer';
import { MAINNET, REGTEST } from '../../src';
import { bytesToHex } from '@stacks/common';
import { compressPrivateKey, privateKeyToAddress } from '@stacks/transactions';
import { STACKS_TESTNET } from '../../../network/src';
import { STACKS_MAINNET } from '@stacks/network';
import { compressPrivateKey, privateKeyToAddress } from '@stacks/transactions';
import { MAINNET } from '../../src';

export function schnorrPublicKey(privateKey: Uint8Array) {
return ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(true).slice(1);
Expand Down

0 comments on commit 36a2065

Please sign in to comment.