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

switch to derived NUMS #89

Closed
wants to merge 3 commits into from
Closed
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
19 changes: 12 additions & 7 deletions src/util/depositRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import ecc from "@bitcoinerlab/secp256k1";

bitcoin.initEccLib(ecc);

export const NUMS_X_COORDINATE = new Uint8Array([
0x50, 0x92, 0x9b, 0x74, 0xc1, 0xa0, 0x49, 0x54, 0xb7, 0x8b, 0x4b, 0x60, 0x35,
0xe9, 0x7a, 0x5e, 0x07, 0x8a, 0x5a, 0x0f, 0x28, 0xec, 0x96, 0xd5, 0x47, 0xbf,
0xee, 0x9a, 0xce, 0x80, 0x3a, 0xc0,
// Pubkey, derived from NUMS pubkey with derivation path "0/0".
// Read about NUMS:
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#constructing-and-spending-taproot-outputs
export const DERIVED_NUMS_X_COORDINATE = new Uint8Array([
0x4a, 0x30, 0xb2, 0xe4, 0x61, 0xb2, 0x80, 0xc0, 0xb1, 0x3a, 0x03, 0x79, 0x90,
0x96, 0xab, 0x12, 0x56, 0x58, 0x91, 0x53, 0xfc, 0x4b, 0x9c, 0x8c, 0xea, 0xd1,
0x6d, 0xc0, 0xb6, 0x42, 0x06, 0x97,
]);



// Helper function to convert a little-endian 8-byte number to big-endian
const flipEndian = (buffer: Uint8Array): Uint8Array => {
const flipped = new Uint8Array(buffer.length);
Expand Down Expand Up @@ -126,19 +131,19 @@ export const createDepositAddress = (

// Step 1: Generate the tweak

const tweak = bip341.tapTweakHash(NUMS_X_COORDINATE, merkleRoot.hash);
const tweak = bip341.tapTweakHash(DERIVED_NUMS_X_COORDINATE, merkleRoot.hash);

// Step 2: Apply the tweak to the internal public key to get the tweaked Taproot output key

const taprootPubKey = bip341.tweakKey(NUMS_X_COORDINATE, tweak);
const taprootPubKey = bip341.tweakKey(DERIVED_NUMS_X_COORDINATE, tweak);

if (taprootPubKey === null) {
throw new Error("Failed to tweak the internal public key.");
}

// Step 1: Convert the Taproot public key to a P2TR address
const p2tr = bitcoin.payments.p2tr({
internalPubkey: NUMS_X_COORDINATE, // The tweaked Taproot public key
internalPubkey: DERIVED_NUMS_X_COORDINATE, // The tweaked Taproot public key
network: network,
scriptTree: scriptTree,
}) as bitcoin.Payment;
Expand Down
4 changes: 2 additions & 2 deletions src/util/reclaimHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as bitcoin from "bitcoinjs-lib";
import { Taptree } from "bitcoinjs-lib/src/types";

import { hexToBytes as hexToUint8Array } from "@stacks/common";
import { NUMS_X_COORDINATE } from "./depositRequest";
import { DERIVED_NUMS_X_COORDINATE } from "./depositRequest";
import getBitcoinNetwork from "./get-bitcoin-network";

export const finalizePsbt = (
Expand Down Expand Up @@ -81,7 +81,7 @@ export const constructPsbtForReclaim = ({
const psbt = new bitcoin.Psbt({ network });

const p2trRes = bitcoin.payments.p2tr({
internalPubkey: NUMS_X_COORDINATE,
internalPubkey: DERIVED_NUMS_X_COORDINATE,
scriptTree,
redeem: {
output: uInt8ReclaimScript,
Expand Down
Loading