|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:bip32/bip32.dart' hide NetworkType, Bip32Type; |
| 4 | +import 'package:bip47/src/network_type.dart'; |
| 5 | +import 'package:bip47/src/payment_code.dart'; |
| 6 | +import 'package:bip47/src/secret_point.dart'; |
| 7 | +import 'package:bip47/src/util.dart'; |
| 8 | +import 'package:coin/coin.dart' as coin; |
| 9 | +import 'package:pointycastle/digests/sha256.dart'; |
| 10 | +import 'package:pointycastle/pointycastle.dart'; |
| 11 | + |
| 12 | +class PaymentAddress { |
| 13 | + final PaymentCode paymentCode; |
| 14 | + final BIP32? bip32Node; |
| 15 | + final NetworkType networkType; |
| 16 | + int index; |
| 17 | + |
| 18 | + static final curveParams = ECDomainParameters("secp256k1"); |
| 19 | + |
| 20 | + PaymentAddress({ |
| 21 | + required this.paymentCode, |
| 22 | + this.bip32Node, |
| 23 | + NetworkType? networkType, |
| 24 | + this.index = 0, |
| 25 | + }) : networkType = networkType ?? bitcoin; |
| 26 | + |
| 27 | + ECPoint sG() => (curveParams.G * getSecretPoint())!; |
| 28 | + |
| 29 | + SecretPoint getSharedSecret() => SecretPoint( |
| 30 | + bip32Node!.privateKey!, |
| 31 | + paymentCode.derivePublicKey(index), |
| 32 | + ); |
| 33 | + |
| 34 | + BigInt getSecretPoint() { |
| 35 | + // convert hash to value 's' |
| 36 | + final BigInt s = hashSharedSecret().toBigInt; |
| 37 | + |
| 38 | + // check that 's' is a member of the associated scalar group |
| 39 | + if (!s.isScalarGroupMemberOf(curveParams)) { |
| 40 | + throw Exception("Secret point is not a member of the secp256k1 group"); |
| 41 | + } |
| 42 | + |
| 43 | + return s; |
| 44 | + } |
| 45 | + |
| 46 | + ECPoint getECPoint() => |
| 47 | + curveParams.curve.decodePoint(paymentCode.derivePublicKey(index))!; |
| 48 | + |
| 49 | + Uint8List hashSharedSecret() => |
| 50 | + SHA256Digest().process(getSharedSecret().ecdhSecret()); |
| 51 | + |
| 52 | + Uint8List _getSendAddressPublicKey() { |
| 53 | + final sum = getECPoint() + sG(); |
| 54 | + return sum!.getEncoded(true); |
| 55 | + } |
| 56 | + |
| 57 | + String getSendAddressP2PKH() { |
| 58 | + final pubKeyBytes = _getSendAddressPublicKey(); |
| 59 | + final pkHash = coin.hash160(pubKeyBytes); |
| 60 | + return coin.P2pkhAddr(pkHash).encode(_toCoinChain()); |
| 61 | + } |
| 62 | + |
| 63 | + String getSendAddressP2WPKH() { |
| 64 | + final pubKeyBytes = _getSendAddressPublicKey(); |
| 65 | + final pkHash = coin.hash160(pubKeyBytes); |
| 66 | + return coin.P2wpkhAddr(pkHash).encode(_toCoinChain()); |
| 67 | + } |
| 68 | + |
| 69 | + String getSendAddressP2TR() { |
| 70 | + // Step 1: BIP-47 point addition -> compressed pubkey B' |
| 71 | + final bip47PubKey = _getSendAddressPublicKey(); |
| 72 | + |
| 73 | + // Step 2: BIP-341 taproot key-path tweak (no script tree) |
| 74 | + final internalKey = coin.PublicKey(bip47PubKey); |
| 75 | + final outputKey = coin.Taproot(internalKey: internalKey).tweakedKey; |
| 76 | + |
| 77 | + // Step 3: bech32m address encoding |
| 78 | + return coin.TaprootAddr(outputKey).encode(_toCoinChain()); |
| 79 | + } |
| 80 | + |
| 81 | + /// Returns the tweaked private key and derived public key for receiving. |
| 82 | + /// |
| 83 | + /// The returned record has [privateKey] (raw 32 bytes) and [publicKey] |
| 84 | + /// (compressed 33 bytes). This replaces the old bitcoindart ECPair. |
| 85 | + ({Uint8List privateKey, Uint8List publicKey}) getReceiveAddressKeyPair() { |
| 86 | + final tweakedPrivKey = _addSecp256k1( |
| 87 | + bip32Node!.privateKey!.toBigInt, |
| 88 | + getSecretPoint(), |
| 89 | + ).to32Bytes(); |
| 90 | + |
| 91 | + final sk = coin.SecretKey(tweakedPrivKey); |
| 92 | + return (privateKey: tweakedPrivKey, publicKey: sk.publicKey.bytes); |
| 93 | + } |
| 94 | + |
| 95 | + String getReceiveAddressP2PKH() { |
| 96 | + final pair = getReceiveAddressKeyPair(); |
| 97 | + final pkHash = coin.hash160(pair.publicKey); |
| 98 | + return coin.P2pkhAddr(pkHash).encode(_toCoinChain()); |
| 99 | + } |
| 100 | + |
| 101 | + String getReceiveAddressP2WPKH() { |
| 102 | + final pair = getReceiveAddressKeyPair(); |
| 103 | + final pkHash = coin.hash160(pair.publicKey); |
| 104 | + return coin.P2wpkhAddr(pkHash).encode(_toCoinChain()); |
| 105 | + } |
| 106 | + |
| 107 | + String getReceiveAddressP2TR() { |
| 108 | + // Step 1: BIP-47 scalar addition -> keypair (b', B') |
| 109 | + final pair = getReceiveAddressKeyPair(); |
| 110 | + |
| 111 | + // Step 2: BIP-341 taproot key-path tweak using B' as internal key |
| 112 | + final internalKey = coin.PublicKey(pair.publicKey); |
| 113 | + final outputKey = coin.Taproot(internalKey: internalKey).tweakedKey; |
| 114 | + |
| 115 | + // Step 3: bech32m address encoding |
| 116 | + return coin.TaprootAddr(outputKey).encode(_toCoinChain()); |
| 117 | + } |
| 118 | + |
| 119 | + BigInt _addSecp256k1(BigInt b1, BigInt b2) { |
| 120 | + final BigInt value = b1 + b2; |
| 121 | + |
| 122 | + return value % curveParams.n; |
| 123 | + } |
| 124 | + |
| 125 | + coin.Chain _toCoinChain() { |
| 126 | + return coin.Chain( |
| 127 | + wifPrefix: networkType.wif, |
| 128 | + p2pkhPrefix: networkType.pubKeyHash, |
| 129 | + p2shPrefix: networkType.scriptHash, |
| 130 | + bech32Hrp: networkType.bech32, |
| 131 | + name: 'bip47', |
| 132 | + bip44CoinType: 0, |
| 133 | + privHDPrefix: networkType.bip32.private, |
| 134 | + pubHDPrefix: networkType.bip32.public, |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
0 commit comments