Skip to content

Commit

Permalink
Fix asm
Browse files Browse the repository at this point in the history
  • Loading branch information
opcatdev committed Nov 30, 2024
1 parent 3f4ed51 commit b6a33ec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cat-protocol/cat-sdk",
"version": "1.0.5",
"version": "1.0.6",
"description": "CAT protocol SDK.",
"author": "catprotocol.org",
"main": "./dist/cjs/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/lib/covenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export abstract class Covenant<StateT = undefined> {
this.tpubkey = tpubkey
this.lockingScript = new btc.Script(`OP_1 32 0x${tpubkey}`)
this.tapLeafContracts = tapLeafContracts
this.address = p2trLockingScriptToAddr(this.lockingScript, options.network)
this.address = p2trLockingScriptToAddr(this.lockingScript.toHex(), options.network)
this.asmVersion = Covenant.calculateAsmVersion(
subContracts.map(c => (c.contract.constructor as typeof SmartContract).getArtifact().md5)
)
Expand Down
31 changes: 17 additions & 14 deletions packages/sdk/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TxOutpoint } from "../contracts/utils/txUtil";
import { bitcoinjs, btc } from "./btc";
import { SupportedNetwork, TAPROOT_ONLY_SCRIPT_SPENT_KEY } from "./constants";
import * as btcSigner from '@scure/btc-signer';
import { payments, Psbt, TxInput } from "bitcoinjs-lib";
import { Network, networks, payments, Psbt, TxInput } from "bitcoinjs-lib";
import { randomBytes } from 'crypto';
import { ByteString, hash160, Ripemd160, UTXO } from "scrypt-ts";
import {encodingLength, encode} from 'varuint-bitcoin';
Expand Down Expand Up @@ -45,21 +45,24 @@ export function scriptToP2tr(script: Buffer): {
}
}

export function toBitcoinNetwork(network: SupportedNetwork): btc.Networks.Network {
if (network === 'btc-signet') {
return btc.Networks.testnet;
} else if (network === 'fractal-mainnet' || network === 'fractal-testnet') {
return btc.Networks.mainnet;
} else {
throw new Error(`invalid network ${network}`);
}
export function toBitcoinNetwork(network: SupportedNetwork): Network {
if (network === 'btc-signet') {
return networks.testnet
} else if (network === 'fractal-mainnet' || network === 'fractal-testnet') {
return networks.bitcoin
} else {
throw new Error(`invalid network ${network}`)
}
}

export function p2trLockingScriptToAddr(
p2tr: string | btc.Script,
p2tr: string,
network: SupportedNetwork = 'fractal-mainnet',
) {
const script = typeof p2tr === 'string' ? btc.Script.fromHex(p2tr) : p2tr;
return btc.Address.fromScript(script, toBitcoinNetwork(network)).toString();
return payments.p2tr({
output: hexToUint8Array(p2tr),
network: toBitcoinNetwork(network),
}).address
}

export function addrToP2trLockingScript(address: string | btc.Address): string {
Expand All @@ -70,15 +73,15 @@ export function addrToP2trLockingScript(address: string | btc.Address): string {
throw new Error(`address ${address} is not taproot`);
}

return btc.Script.fromAddress(address).toHex();
return uint8ArrayToHex(payments.p2tr({ address: address.toString() }).output)
}

export function xPubkeyToP2trLockingScript(xPubkey: string): btc.Script {
return new btc.Script(`OP_1 32 0x${xPubkey}`);
}

export function xPubkeyToAddr(xPubkey: string, network: SupportedNetwork = 'fractal-mainnet') {
return p2trLockingScriptToAddr(xPubkeyToP2trLockingScript(xPubkey), network);
return p2trLockingScriptToAddr(xPubkeyToP2trLockingScript(xPubkey).toHex(), network);
}

export function toPsbt(tx: btc.Transaction): bitcoinjs.Psbt {
Expand Down

0 comments on commit b6a33ec

Please sign in to comment.