import 'package:flutter/services.dart'; import 'package:flutter_bitcoin/flutter_bitcoin.dart' as bitcoin; import 'dart:typed_data'; import 'package:hex/hex.dart'; import 'package:crypto/crypto.dart'; import 'package:bip39/bip39.dart' as bip39; import 'package:bip32/bip32.dart' as bip32; final bip32.NetworkType hnsMainnet = bip32.NetworkType( bip32: bip32.Bip32Type(public: 0x0488b21e, private: 0x0488ade4), wif: 0x80, ); final bitcoin.NetworkType hnsMainnetBitcoin = bitcoin.NetworkType( messagePrefix: '\x18Handshake Signed Message:\n', bech32: 'hs', bip32: bitcoin.Bip32Type(public: 0x0488b21e, private: 0x0488ade4), pubKeyHash: 0x00, scriptHash: 0x05, wif: 0x80); String getXpub(String seedPhrase) { List seedints = bip39.mnemonicToSeed(seedPhrase); Uint8List seed = Uint8List.fromList(seedints); bip32.BIP32 master = bip32.BIP32.fromSeed(seed, hnsMainnet); return master.derivePath("m/44'/5353'/0'").neutered().toBase58(); } Future signTX(String seedPhrase, Map txData) async { final seed = bip39.mnemonicToSeed(seedPhrase); final master = bip32.BIP32.fromSeed(seed, hnsMainnet); final txb = bitcoin.TransactionBuilder( network: hnsMainnetBitcoin, ); // for (var input in txData['inputs']) { // final prevout = input['prevout']; // final vin = input['vin']; // if (vin != null && vin is int) { // final hash = prevout['hash']; // final index = prevout['index']; // bitcoin.ECPair keyPair = bitcoin.ECPair.fromPrivateKey( // master.derivePath("m/44'/5353'/0'/0/$vin").privateKey!, // network: hnsMainnetBitcoin); // txb.addInput(hash, index); // txb.sign(vin: vin, keyPair: keyPair); // } else { // print('vin is not an int'); // print(input); // } // } for (var input in txData['inputs']) { final prevout = input['prevout']; final coin = input['coin']; final index = prevout['index']; final hash = prevout['hash']; final vin = coin['height']; print(input); bitcoin.ECPair keyPair = bitcoin.ECPair.fromPrivateKey( master.derivePath("m/44'/5353'/0'/0/$vin").privateKey!, network: hnsMainnetBitcoin, ); txb.addInput(hash, index); txb.sign(vin: int.parse(vin.toString()), keyPair: keyPair); } for (var output in txData['outputs']) { txb.addOutput(output['address'], output['value']); } return txb.build().toHex(); }