2023-11-30 10:01:58 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
String scriptHash(String address, {required bitcoin.NetworkType networkType}) {
|
2023-11-30 17:28:29 +00:00
|
|
|
try {
|
|
|
|
final outputScript = bitcoin.Address.addressToOutputScript(address, networkType);
|
|
|
|
final parts = sha256.convert(outputScript).toString().split('');
|
|
|
|
var res = '';
|
2023-11-30 10:01:58 +00:00
|
|
|
|
2023-11-30 17:28:29 +00:00
|
|
|
for (var i = parts.length - 1; i >= 0; i--) {
|
|
|
|
final char = parts[i];
|
|
|
|
i--;
|
|
|
|
final nextChar = parts[i];
|
|
|
|
res += nextChar;
|
|
|
|
res += char;
|
|
|
|
}
|
2023-11-30 10:01:58 +00:00
|
|
|
|
2023-11-30 17:28:29 +00:00
|
|
|
return res;
|
|
|
|
} catch (e) {}
|
|
|
|
return '';
|
2023-11-30 10:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String scriptHashFromScript(Uint8List outputScript, {required bitcoin.NetworkType networkType}) {
|
2021-12-24 12:52:08 +00:00
|
|
|
final parts = sha256.convert(outputScript).toString().split('');
|
|
|
|
var res = '';
|
|
|
|
|
|
|
|
for (var i = parts.length - 1; i >= 0; i--) {
|
|
|
|
final char = parts[i];
|
|
|
|
i--;
|
|
|
|
final nextChar = parts[i];
|
|
|
|
res += nextChar;
|
|
|
|
res += char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|