2020-12-08 21:47:08 +00:00
|
|
|
import 'dart:typed_data';
|
2021-05-07 07:36:38 +00:00
|
|
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
2020-12-08 21:47:08 +00:00
|
|
|
import 'package:bs58check/bs58check.dart' as bs58check;
|
|
|
|
import 'package:bitcoin_flutter/src/utils/constants/op.dart';
|
|
|
|
import 'package:bitcoin_flutter/src/utils/script.dart' as bscript;
|
|
|
|
import 'package:bitcoin_flutter/src/address.dart';
|
|
|
|
|
|
|
|
Uint8List p2shAddressToOutputScript(String address) {
|
|
|
|
final decodeBase58 = bs58check.decode(address);
|
|
|
|
final hash = decodeBase58.sublist(1);
|
|
|
|
return bscript.compile(<dynamic>[OPS['OP_HASH160'], hash, OPS['OP_EQUAL']]);
|
|
|
|
}
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
Uint8List addressToOutputScript(
|
|
|
|
String address, bitcoin.NetworkType networkType) {
|
2020-12-08 21:47:08 +00:00
|
|
|
try {
|
|
|
|
// FIXME: improve validation for p2sh addresses
|
2021-05-07 07:36:38 +00:00
|
|
|
// 3 for bitcoin
|
|
|
|
// m for litecoin
|
|
|
|
if (address.startsWith('3') || address.toLowerCase().startsWith('m')) {
|
2020-12-08 21:47:08 +00:00
|
|
|
return p2shAddressToOutputScript(address);
|
|
|
|
}
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
return Address.addressToOutputScript(address, networkType);
|
|
|
|
} catch (err) {
|
|
|
|
print(err);
|
2020-12-08 21:47:08 +00:00
|
|
|
return Uint8List(0);
|
|
|
|
}
|
2021-05-07 07:36:38 +00:00
|
|
|
}
|