sbtc-bridge-lib
Advanced tools
Comparing version
@@ -6,3 +6,3 @@ import * as btc from '@scure/btc-signer'; | ||
import { toStorable, buildDepositPayload, buildDepositPayloadOpDrop } from './payload_utils.js'; | ||
import { addInputs, getPegWalletAddressFromPublicKey, inputAmt } from './wallet_utils.js'; | ||
import { addInputs, getNet, getPegWalletAddressFromPublicKey, inputAmt } from './wallet_utils.js'; | ||
const concat = P.concatBytes; | ||
@@ -26,3 +26,7 @@ const privKey = hex.decode('0101010101010101010101010101010101010101010101010101010101010101'); | ||
export function buildDepositTransaction(network, sbtcWalletPublicKey, uiPayload, btcFeeRates, utxos) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
let net = btc.TEST_NETWORK; | ||
if (network === 'devnet') | ||
net = btc.TEST_NETWORK; | ||
else if (network === 'mainnet') | ||
net = btc.NETWORK; | ||
const sbtcWalletAddress = getPegWalletAddressFromPublicKey(network, sbtcWalletPublicKey); | ||
@@ -50,3 +54,3 @@ const data = buildDepositPayload(network, uiPayload.principal); | ||
export function buildDepositTransactionOpDrop(network, uiPayload, btcFeeRates, utxos, commitTxAddress) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const txFees = calculateDepositFees(network, true, uiPayload.amountSats, btcFeeRates.feeInfo, utxos, commitTxAddress, undefined); | ||
@@ -75,3 +79,3 @@ const tx = new btc.Transaction({ allowUnknowInput: true, allowUnknowOutput: true, allowUnknownInputs: true, allowUnknownOutputs: true }); | ||
export function getBridgeDepositOpDrop(network, sbtcWalletPublicKey, uiPayload, originator) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const data = buildData(network, uiPayload.principal, uiPayload.amountSats); | ||
@@ -108,3 +112,3 @@ const scripts = [ | ||
try { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
let vsize = 0; | ||
@@ -111,0 +115,0 @@ const tx = new btc.Transaction({ allowUnknowInput: true, allowUnknowOutput: true, allowUnknownInputs: true, allowUnknownOutputs: true }); |
@@ -83,2 +83,7 @@ import * as btc from '@scure/btc-signer'; | ||
}; | ||
export declare function getMagicAndOpCode(d1: Uint8Array): { | ||
magic?: string; | ||
opcode: string; | ||
txType?: string; | ||
}; | ||
/** | ||
@@ -85,0 +90,0 @@ * Ensure we don't overwrite the original object with Uint8Arrays these can't be serialised to local storage. |
@@ -391,3 +391,3 @@ import * as secp from '@noble/secp256k1'; | ||
} | ||
function getMagicAndOpCode(d1) { | ||
export function getMagicAndOpCode(d1) { | ||
if (!d1 || d1.length < 2) | ||
@@ -394,0 +394,0 @@ throw new Error('no magic data passed'); |
@@ -6,2 +6,3 @@ import * as secp from '@noble/secp256k1'; | ||
import { buildDepositPayloadOpDrop } from './payload_utils.js'; | ||
import { getNet } from './wallet_utils.js'; | ||
const concat = P.concatBytes; | ||
@@ -24,3 +25,3 @@ const privKey = hex.decode('0101010101010101010101010101010101010101010101010101010101010101'); | ||
console.log('approxTxFees dest=' + payeeAddress); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const tx = new btc.Transaction({ allowUnknowOutput: true, allowUnknownInputs: true, allowUnknownOutputs: true }); | ||
@@ -27,0 +28,0 @@ // create a set of inputs corresponding to the utxo set |
import * as btc from '@scure/btc-signer'; | ||
import type { SbtcMiniContractsI, CommitKeysI, UTXO } from './types/sbtc_types.js'; | ||
export declare const REGTEST_NETWORK: typeof btc.NETWORK; | ||
export declare function getNet(network: string): { | ||
bech32: string; | ||
pubKeyHash: number; | ||
scriptHash: number; | ||
wif: number; | ||
}; | ||
export declare const sbtcMiniContracts: SbtcMiniContractsI; | ||
@@ -4,0 +11,0 @@ export declare const sbtcWallets: { |
@@ -7,2 +7,11 @@ import * as secp from '@noble/secp256k1'; | ||
const priv = secp.utils.randomPrivateKey(); | ||
export const REGTEST_NETWORK = { bech32: 'bcrt', pubKeyHash: 0x6f, scriptHash: 0xc4, wif: 0xc4 }; | ||
export function getNet(network) { | ||
let net = btc.TEST_NETWORK; | ||
if (network === 'devnet') | ||
net = REGTEST_NETWORK; | ||
else if (network === 'mainnet') | ||
net = btc.NETWORK; | ||
return net; | ||
} | ||
const keySetForFeeCalculation = []; | ||
@@ -60,3 +69,3 @@ keySetForFeeCalculation.push({ | ||
export function getTestAddresses(network) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
return { | ||
@@ -76,3 +85,3 @@ fromBtcAddress: btc.getAddress('tr', hex.decode(testWallets[0].privateKey), net), | ||
export function addressFromPubkey(network, pubkey) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
try { | ||
@@ -91,2 +100,4 @@ return btc.Address(net).encode(btc.OutScript.decode(pubkey)); | ||
throw new Error('Address is undefined'); | ||
if (net === 'devnet') | ||
return; | ||
if (net === 'testnet') { | ||
@@ -184,3 +195,3 @@ if (address.startsWith('bc')) | ||
export function addInputs(network, amount, revealPayment, transaction, feeCalc, utxos, paymentPublicKey) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const bar = revealPayment + amount; | ||
@@ -324,3 +335,3 @@ let amt = 0; | ||
export function getAddressFromOutScript(network, script) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const outputScript = btc.OutScript.decode(script); | ||
@@ -409,3 +420,3 @@ if (outputScript.type === 'pk' || outputScript.type === 'tr') { | ||
return; | ||
let net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
let net = getNet(network); | ||
//if (network === 'development' || network === 'simnet') { | ||
@@ -412,0 +423,0 @@ // net = { bech32: 'bcrt', pubKeyHash: 0x6f, scriptHash: 0xc4, wif: 0 } |
@@ -27,3 +27,3 @@ import * as btc from '@scure/btc-signer'; | ||
export function getWithdrawScript (network:string, data:Uint8Array, sbtcWalletAddress:string, fromBtcAddress:string):{type:string, script:Uint8Array} { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const addrScript = btc.Address(net).decode(sbtcWalletAddress) | ||
@@ -30,0 +30,0 @@ if (addrScript.type === 'wpkh') { |
@@ -6,3 +6,3 @@ import * as btc from '@scure/btc-signer'; | ||
import { buildWithdrawPayload } from './payload_utils.js'; | ||
import { addInputs, getPegWalletAddressFromPublicKey, inputAmt } from './wallet_utils.js'; | ||
import { addInputs, getNet, getPegWalletAddressFromPublicKey, inputAmt } from './wallet_utils.js'; | ||
const concat = P.concatBytes; | ||
@@ -24,3 +24,3 @@ const privKey = hex.decode('0101010101010101010101010101010101010101010101010101010101010101'); | ||
throw new Error('Signature of output 2 scriptPubKey is required'); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const sbtcWalletAddress = getPegWalletAddressFromPublicKey(network, sbtcWalletPublicKey); | ||
@@ -51,3 +51,3 @@ const data = buildData(network, uiPayload.amountSats, uiPayload.signature, false); | ||
throw new Error('Signature of output 2 scriptPubKey is required'); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const sbtcWalletAddress = getPegWalletAddressFromPublicKey(network, sbtcWalletPublicKey); | ||
@@ -72,3 +72,3 @@ const txFees = calculateWithdrawFees(network, true, utxos, uiPayload.amountSats, btcFeeRates, sbtcWalletAddress, uiPayload.bitcoinAddress, uiPayload.paymentPublicKey, undefined); | ||
let vsize = 0; | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const tx = new btc.Transaction({ allowUnknowOutput: true, allowUnknownInputs: true, allowUnknownOutputs: true }); | ||
@@ -103,3 +103,3 @@ addInputs(network, amount, revealPayment, tx, true, utxos, paymentPublicKey); | ||
export function getWithdrawScript (network:string, data:Uint8Array, sbtcWalletAddress:string, fromBtcAddress:string):{type:string, script:Uint8Array} { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const addrScript = btc.Address(net).decode(sbtcWalletAddress) | ||
@@ -141,3 +141,3 @@ if (addrScript.type === 'wpkh') { | ||
const data = buildData(network, uiPayload.amountSats, uiPayload.signature, true); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
let pk1U = hex.decode(sbtcWalletPublicKey); | ||
@@ -144,0 +144,0 @@ let pk2U = hex.decode(uiPayload.reclaimPublicKey); |
{ | ||
"name": "sbtc-bridge-lib", | ||
"version": "1.1.19", | ||
"version": "1.1.20", | ||
"description": "Library for sBTC Bridge web client and API apps ", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -8,4 +8,5 @@ import * as btc from '@scure/btc-signer'; | ||
import { toStorable, buildDepositPayload, buildDepositPayloadOpDrop } from './payload_utils.js' | ||
import { addInputs, getPegWalletAddressFromPublicKey, inputAmt } from './wallet_utils.js'; | ||
import { addInputs, getNet, getPegWalletAddressFromPublicKey, inputAmt } from './wallet_utils.js'; | ||
const concat = P.concatBytes; | ||
@@ -31,3 +32,5 @@ | ||
export function buildDepositTransaction(network:string, sbtcWalletPublicKey:string, uiPayload:DepositPayloadUIType, btcFeeRates:any, utxos:Array<UTXO>):Transaction { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
let net = btc.TEST_NETWORK; | ||
if (network === 'devnet') net = btc.TEST_NETWORK | ||
else if (network === 'mainnet') net = btc.NETWORK | ||
const sbtcWalletAddress = getPegWalletAddressFromPublicKey(network, sbtcWalletPublicKey) | ||
@@ -56,3 +59,3 @@ const data = buildDepositPayload(network, uiPayload.principal); | ||
export function buildDepositTransactionOpDrop (network:string, uiPayload:DepositPayloadUIType, btcFeeRates:any, utxos:Array<UTXO>, commitTxAddress:string) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const txFees = calculateDepositFees(network, true, uiPayload.amountSats, btcFeeRates.feeInfo, utxos, commitTxAddress, undefined) | ||
@@ -82,3 +85,3 @@ const tx = new btc.Transaction({ allowUnknowInput: true, allowUnknowOutput: true, allowUnknownInputs:true, allowUnknownOutputs:true }); | ||
export function getBridgeDepositOpDrop(network:string, sbtcWalletPublicKey:string, uiPayload:DepositPayloadUIType, originator:string):BridgeTransactionType { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
@@ -117,3 +120,3 @@ const data = buildData(network, uiPayload.principal, uiPayload.amountSats); | ||
try { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
let vsize = 0; | ||
@@ -120,0 +123,0 @@ const tx = new btc.Transaction({ allowUnknowInput: true, allowUnknowOutput: true, allowUnknownInputs:true, allowUnknownOutputs:true }); |
@@ -422,3 +422,3 @@ import * as secp from '@noble/secp256k1'; | ||
function getMagicAndOpCode(d1: Uint8Array): {magic?:string; opcode:string; txType? :string; } { | ||
export function getMagicAndOpCode(d1: Uint8Array): {magic?:string; opcode:string; txType? :string; } { | ||
if (!d1 || d1.length < 2) throw new Error('no magic data passed'); | ||
@@ -425,0 +425,0 @@ const magic = hex.encode(d1.subarray(0,2)); |
@@ -14,3 +14,3 @@ // see https://medium.com/coinmonks/merkle-tree-a-simple-explanation-and-implementation-48903442bc08#:~:text=The%20use%20of%20Merkle%20Tree,block%20or%20the%20whole%20blockchain. | ||
* | ||
* @param txIdNormal | ||
* @param txIdNormal | ||
* @param txHex | ||
@@ -17,0 +17,0 @@ * @param block |
@@ -6,2 +6,3 @@ import * as secp from '@noble/secp256k1'; | ||
import { buildDepositPayloadOpDrop } from './payload_utils.js' | ||
import { getNet } from './wallet_utils.js'; | ||
@@ -33,3 +34,3 @@ const concat = P.concatBytes; | ||
console.log('approxTxFees dest=' + payeeAddress) | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const tx = new btc.Transaction({ allowUnknowOutput: true, allowUnknownInputs:true, allowUnknownOutputs:true }); | ||
@@ -36,0 +37,0 @@ // create a set of inputs corresponding to the utxo set |
@@ -9,2 +9,12 @@ import * as secp from '@noble/secp256k1'; | ||
const priv = secp.utils.randomPrivateKey() | ||
export const REGTEST_NETWORK: typeof btc.NETWORK = { bech32: 'bcrt', pubKeyHash: 0x6f, scriptHash: 0xc4, wif: 0xc4 }; | ||
export function getNet(network:string) { | ||
let net = btc.TEST_NETWORK; | ||
if (network === 'devnet') net = REGTEST_NETWORK | ||
else if (network === 'mainnet') net = btc.NETWORK | ||
return net; | ||
} | ||
type KeySet = { | ||
@@ -15,2 +25,3 @@ priv: Uint8Array, | ||
} | ||
const keySetForFeeCalculation: KeySet[] = [] | ||
@@ -71,3 +82,3 @@ keySetForFeeCalculation.push({ | ||
export function getTestAddresses (network:string):CommitKeysI { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
return { | ||
@@ -88,3 +99,3 @@ fromBtcAddress: btc.getAddress('tr', hex.decode(testWallets[0].privateKey), net) as string, | ||
export function addressFromPubkey(network:string, pubkey:Uint8Array) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
try { | ||
@@ -101,2 +112,3 @@ return btc.Address(net).encode(btc.OutScript.decode(pubkey)); | ||
if (address.length < 10) throw new Error('Address is undefined') | ||
if (net === 'devnet') return | ||
if (net === 'testnet') { | ||
@@ -185,3 +197,3 @@ if (address.startsWith('bc')) throw new Error('Mainnet address passed to testnet app: ' + address) | ||
export function addInputs (network:string, amount:number, revealPayment:number, transaction:btc.Transaction, feeCalc:boolean, utxos:Array<UTXO>, paymentPublicKey:string) { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const bar = revealPayment + amount; | ||
@@ -314,3 +326,3 @@ let amt = 0; | ||
export function getAddressFromOutScript(network:string, script: Uint8Array):string { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const outputScript = btc.OutScript.decode(script); | ||
@@ -402,3 +414,3 @@ | ||
if (!sbtcWalletPublicKey) return | ||
let net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
let net = getNet(network); | ||
//if (network === 'development' || network === 'simnet') { | ||
@@ -405,0 +417,0 @@ // net = { bech32: 'bcrt', pubKeyHash: 0x6f, scriptHash: 0xc4, wif: 0 } |
@@ -8,3 +8,3 @@ import * as btc from '@scure/btc-signer'; | ||
import { buildWithdrawPayload, amountToBigUint64, bigUint64ToAmount } from './payload_utils.js' | ||
import { addInputs, getPegWalletAddressFromPublicKey, inputAmt, toXOnly } from './wallet_utils.js'; | ||
import { addInputs, getNet, getPegWalletAddressFromPublicKey, inputAmt, toXOnly } from './wallet_utils.js'; | ||
@@ -28,3 +28,3 @@ const concat = P.concatBytes; | ||
if (!uiPayload.signature) throw new Error('Signature of output 2 scriptPubKey is required'); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const sbtcWalletAddress = getPegWalletAddressFromPublicKey(network, sbtcWalletPublicKey) | ||
@@ -54,3 +54,3 @@ const data = buildData(network, uiPayload.amountSats, uiPayload.signature, false) | ||
if (!uiPayload.signature) throw new Error('Signature of output 2 scriptPubKey is required'); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const sbtcWalletAddress = getPegWalletAddressFromPublicKey(network, sbtcWalletPublicKey) | ||
@@ -75,3 +75,3 @@ const txFees = calculateWithdrawFees(network, true, utxos, uiPayload.amountSats, btcFeeRates, sbtcWalletAddress!, uiPayload.bitcoinAddress, uiPayload.paymentPublicKey, undefined) | ||
let vsize = 0; | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const tx = new btc.Transaction({ allowUnknowOutput: true, allowUnknownInputs:true, allowUnknownOutputs:true }); | ||
@@ -103,3 +103,3 @@ addInputs(network, amount, revealPayment, tx, true, utxos, paymentPublicKey); | ||
export function getWithdrawScript (network:string, data:Uint8Array, sbtcWalletAddress:string, fromBtcAddress:string):{type:string, script:Uint8Array} { | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
const addrScript = btc.Address(net).decode(sbtcWalletAddress) | ||
@@ -142,3 +142,3 @@ if (addrScript.type === 'wpkh') { | ||
const data = buildData(network, uiPayload.amountSats, uiPayload.signature!, true); | ||
const net = (network === 'testnet') ? btc.TEST_NETWORK : btc.NETWORK; | ||
const net = getNet(network); | ||
@@ -145,0 +145,0 @@ let pk1U = hex.decode(sbtcWalletPublicKey) |
212679
0.04%5123
0.77%