|
| 1 | +import * as bitcoin from 'bitcoinjs-lib'; |
| 2 | +const ecc = require('tiny-secp256k1'); |
| 3 | +const { BIP32Factory } = require('bip32'); |
| 4 | +const bip32 = BIP32Factory(ecc); |
| 5 | + |
| 6 | +export type NetworkType = 'bitcoin' | 'testnet' | 'regtest'; |
| 7 | + |
| 8 | +interface CreateAddressParams { |
| 9 | + seedHex: string; |
| 10 | + receiveOrChange: string; |
| 11 | + addressIndex: number; |
| 12 | + network: NetworkType; |
| 13 | + method: string; |
| 14 | +} |
| 15 | + |
| 16 | +export function createAddress(params: CreateAddressParams): { privateKey: string; publicKey: string; address: string } { |
| 17 | + const { seedHex, receiveOrChange, addressIndex, network, method } = params; |
| 18 | + const root = bip32.fromSeed(Buffer.from(seedHex, 'hex')); |
| 19 | + let path = "m/44'/0'/0'/0/" + addressIndex + ''; |
| 20 | + if (receiveOrChange === '1') { |
| 21 | + path = "m/44'/0'/0'/1/" + addressIndex + ''; |
| 22 | + } |
| 23 | + const child = root.derivePath(path); |
| 24 | + let address = ''; |
| 25 | + switch (method) { |
| 26 | + case 'p2pkh': { |
| 27 | + const p2pkhAddress = bitcoin.payments.p2pkh({ |
| 28 | + pubkey: child.publicKey, |
| 29 | + network: bitcoin.networks[network as keyof typeof bitcoin.networks] |
| 30 | + }); |
| 31 | + if (!p2pkhAddress.address) { |
| 32 | + throw new Error('Failed to generate p2pkh address'); |
| 33 | + } |
| 34 | + address = p2pkhAddress.address; |
| 35 | + break; |
| 36 | + } |
| 37 | + case 'p2wpkh': { |
| 38 | + const p2wpkhAddress = bitcoin.payments.p2wpkh({ |
| 39 | + pubkey: child.publicKey, |
| 40 | + network: bitcoin.networks[network as keyof typeof bitcoin.networks] |
| 41 | + }); |
| 42 | + if (!p2wpkhAddress.address) { |
| 43 | + throw new Error('Failed to generate p2wpkh address'); |
| 44 | + } |
| 45 | + address = p2wpkhAddress.address; |
| 46 | + break; |
| 47 | + } |
| 48 | + case 'p2sh': { |
| 49 | + const p2shAddress = bitcoin.payments.p2sh({ |
| 50 | + redeem: bitcoin.payments.p2wpkh({ |
| 51 | + pubkey: child.publicKey, |
| 52 | + network: bitcoin.networks[network as keyof typeof bitcoin.networks] |
| 53 | + }) |
| 54 | + }); |
| 55 | + if (!p2shAddress.address) { |
| 56 | + throw new Error('Failed to generate p2sh address'); |
| 57 | + } |
| 58 | + address = p2shAddress.address; |
| 59 | + break; |
| 60 | + } |
| 61 | + default: |
| 62 | + throw new Error(`Unsupported method: ${method}`); |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + privateKey: Buffer.from(child.privateKey).toString('hex'), |
| 67 | + publicKey: Buffer.from(child.publicKey).toString('hex'), |
| 68 | + address |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +interface CreateMultiSignAddressParams { |
| 73 | + pubkeys: Buffer[]; |
| 74 | + network: NetworkType; |
| 75 | + method: string; |
| 76 | + threshold: number; |
| 77 | +} |
| 78 | + |
| 79 | +export function createMultiSignAddress(params: CreateMultiSignAddressParams): string { |
| 80 | + const { pubkeys, network, method, threshold } = params; |
| 81 | + |
| 82 | + const getAddress = (payment: any): string => { |
| 83 | + const address = payment.address; |
| 84 | + if (!address) { |
| 85 | + throw new Error('Failed to generate multisig address'); |
| 86 | + } |
| 87 | + return address; |
| 88 | + }; |
| 89 | + |
| 90 | + switch (method) { |
| 91 | + case 'p2pkh': { |
| 92 | + const payment = bitcoin.payments.p2sh({ |
| 93 | + redeem: bitcoin.payments.p2ms({ |
| 94 | + m: threshold, |
| 95 | + network: bitcoin.networks[network as keyof typeof bitcoin.networks], |
| 96 | + pubkeys |
| 97 | + }) |
| 98 | + }); |
| 99 | + return getAddress(payment); |
| 100 | + } |
| 101 | + case 'p2wpkh': { |
| 102 | + const payment = bitcoin.payments.p2wsh({ |
| 103 | + redeem: bitcoin.payments.p2ms({ |
| 104 | + m: threshold, |
| 105 | + network: bitcoin.networks[network as keyof typeof bitcoin.networks], |
| 106 | + pubkeys |
| 107 | + }) |
| 108 | + }); |
| 109 | + return getAddress(payment); |
| 110 | + } |
| 111 | + case 'p2sh': { |
| 112 | + const payment = bitcoin.payments.p2sh({ |
| 113 | + redeem: bitcoin.payments.p2wsh({ |
| 114 | + redeem: bitcoin.payments.p2ms({ |
| 115 | + m: threshold, |
| 116 | + network: bitcoin.networks[network as keyof typeof bitcoin.networks], |
| 117 | + pubkeys |
| 118 | + }) |
| 119 | + }) |
| 120 | + }); |
| 121 | + return getAddress(payment); |
| 122 | + } |
| 123 | + default: |
| 124 | + throw new Error(`Unsupported multisig method: ${method}`); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +interface CreateSchnorrAddressParams { |
| 129 | + seedHex: string; |
| 130 | + receiveOrChange: string; |
| 131 | + addressIndex: number; |
| 132 | +} |
| 133 | + |
| 134 | +interface SchnorrAddressResult { |
| 135 | + privateKey: string; |
| 136 | + publicKey: string; |
| 137 | + address: string; |
| 138 | +} |
| 139 | + |
| 140 | +export function createSchnorrAddress(params: CreateSchnorrAddressParams): SchnorrAddressResult { |
| 141 | + bitcoin.initEccLib(ecc); |
| 142 | + const { seedHex, receiveOrChange, addressIndex } = params; |
| 143 | + |
| 144 | + const root = bip32.fromSeed(Buffer.from(seedHex, 'hex')); |
| 145 | + let path = "m/44'/0'/0'/0/" + addressIndex + ''; |
| 146 | + if (receiveOrChange === '1') { |
| 147 | + path = "m/44'/0'/0'/1/" + addressIndex + ''; |
| 148 | + } |
| 149 | + |
| 150 | + const childKey = root.derivePath(path); |
| 151 | + const privateKey = childKey.privateKey; |
| 152 | + if (!privateKey) { |
| 153 | + throw new Error('No private key found'); |
| 154 | + } |
| 155 | + |
| 156 | + const publicKey = childKey.publicKey; |
| 157 | + if (!publicKey) { |
| 158 | + throw new Error('No public key found'); |
| 159 | + } |
| 160 | + |
| 161 | + // 生成 P2TR 地址 |
| 162 | + const payment = bitcoin.payments.p2tr({ |
| 163 | + internalPubkey: publicKey.length === 32 ? publicKey : publicKey.slice(1, 33) |
| 164 | + }); |
| 165 | + |
| 166 | + if (!payment.address) { |
| 167 | + throw new Error('Failed to generate Schnorr address'); |
| 168 | + } |
| 169 | + |
| 170 | + return { |
| 171 | + privateKey: Buffer.from(privateKey).toString('hex'), |
| 172 | + publicKey: Buffer.from(publicKey).toString('hex'), |
| 173 | + address: payment.address |
| 174 | + }; |
| 175 | +} |
0 commit comments