From 01056843005a12adaaa8f62d5b02b572fc84ce1d Mon Sep 17 00:00:00 2001 From: vsenkiv Date: Tue, 11 Oct 2022 14:48:48 +0300 Subject: [PATCH] taproot address support implemented --- package.json | 1 + src/actions/deriveAddresses.ts | 26 +++++++++++++++++++++++++- src/configuration/currencies.ts | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a777706..fcd1341 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "bchaddrjs": "0.5.2", "bignumber.js": "9.0.2", "bip32": "3.0.1", + "bip86": "0.0.1", "bitcoinjs-lib": "6.0.1", "bitcore-lib-cash": "8.25.28", "bs58check": "2.1.2", diff --git a/src/actions/deriveAddresses.ts b/src/actions/deriveAddresses.ts index bae78af..894c306 100644 --- a/src/actions/deriveAddresses.ts +++ b/src/actions/deriveAddresses.ts @@ -14,6 +14,7 @@ import bs58 from "bs58check"; import { publicKeyTweakAdd } from "secp256k1"; const bip32 = BIP32Factory(ecc); +const BIP86 = require('bip86'); class BIP32 { publicKey: any; @@ -134,6 +135,25 @@ function getNativeSegWitAddress( return String(address); } +/** + * derive a Taproot address at a given account and index positions + * @param xpub the xpub from which to derive the native SegWit address + * @param account account number from which to derive the native SegWit address + * @param index index number from which to derive the native SegWit address + * @returns the derived Taproot address + */ + function getTaprootAddress( + xpub: string, + account: number, + index: number, +): string { + + var account1 = new BIP86.fromXPub(xpub); + var address = account1.getAddress(index,!!account); + + return String(address); +} + /** * derive a Bitcoin Cash address at a given account and index positions * @param xpub the xpub from which to derive the Bitcoin Cash address @@ -201,6 +221,8 @@ function deriveAddress( return getSegWitAddress(xpub, account, index); case DerivationMode.NATIVE: return getNativeSegWitAddress(xpub, account, index); + case DerivationMode.TAPROOT: + return getTaprootAddress(xpub,account,index); case DerivationMode.BCH: return getLegacyBitcoinCashAddress(xpub, account, index); case DerivationMode.DOGECOIN: @@ -221,8 +243,10 @@ function deriveAddress( */ function getDerivationMode(address: string) { - if (address.match("^(bc1|tb1|ltc1).*")) { + if (address.match("^(bc1q|tb1|ltc1).*")) { return DerivationMode.NATIVE; + } else if (address.match("^[bc1p].*")) { + return DerivationMode.TAPROOT; } else if (address.match("^[32M].*")) { return DerivationMode.SEGWIT; } else if (address.match("^[1nmL].*")) { diff --git a/src/configuration/currencies.ts b/src/configuration/currencies.ts index f8ce1c9..e2fa3e5 100644 --- a/src/configuration/currencies.ts +++ b/src/configuration/currencies.ts @@ -4,6 +4,7 @@ export enum DerivationMode { LEGACY = "Legacy", NATIVE = "Native SegWit", SEGWIT = "SegWit", + TAPROOT = "Taproot", BCH = "Bitcoin Cash", ETHEREUM = "Ethereum", DOGECOIN = "Dogecoin", @@ -20,6 +21,7 @@ export const currencies = { DerivationMode.LEGACY, DerivationMode.SEGWIT, DerivationMode.NATIVE, + DerivationMode.TAPROOT, ], precision: 10 ** 8, utxo_based: true,