dart-bitcoin / bitcoin_flutter

A dart Bitcoin library for Flutter.
MIT License
165 stars 123 forks source link

bitcoinjs toOutputScript equivalent? #47

Closed willyfromtheblock closed 3 years ago

willyfromtheblock commented 3 years ago

Hey,

I saw that this project is inspired by bitcoinjs. Bitcoinjs has toOutputScript and I was wondering if there is an equivalent inside bitcoin_flutter. I didn't find any so far.

What I'm trying to do is basically this:

let script = bitcoin.address.toOutputScript(address)
let hash = bitcoin.crypto.sha256(script)
let reversedHash = new Buffer(hash.reverse())

console.log(address, ' maps to ', reversedHash.toString('hex'))

https://github.com/bitcoinjs/bitcoinjs-lib/issues/990#issuecomment-358701657

I'm working with electrum and I do believe that there is no script hashing possible without this method.

Thanks

Willy

willyfromtheblock commented 3 years ago

Should've looked closer. https://github.com/dart-bitcoin/bitcoin_flutter/blob/ccea4d906433a85df6b2f54f5881d8277fd57057/lib/src/transaction_builder.dart#L372

willyfromtheblock commented 3 years ago
  String reverseString(String input) {
      List items = [];
      for (var i = 0; i < input.length; i++) {
        items.add(input[i]);
      }
      List itemsReversed = [];
      items.asMap().forEach((index, value) {
        if (index % 2 == 0) {
          itemsReversed.insert(0, items[index + 1]);
          itemsReversed.insert(0, value);
        }
      });
      return itemsReversed.join();
    }
var script = Address.addressToOutputScript(wallet.address);
    var hash = sha256.convert(script).toString();
    reversed = reverseString(hash);

et voila, electrumx will be very happy with reversed