cake_wallet/cw_bitcoin/lib/address_to_output_script.dart

31 lines
1.1 KiB
Dart
Raw Permalink Normal View History

import 'dart:typed_data';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
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']]);
}
2023-11-30 17:28:29 +00:00
Uint8List addressToOutputScript(String address, bitcoin.NetworkType networkType) {
try {
// FIXME: improve validation for p2sh addresses
// 3 for bitcoin
// m for litecoin
2023-11-30 17:28:29 +00:00
// (note: m is also for bitcoin's testnet. check networkType to make sure)
if (address.startsWith('3') ||
(address.toLowerCase().startsWith('m') && networkType != bitcoin.testnet)) {
return p2shAddressToOutputScript(address);
}
return Address.addressToOutputScript(address, networkType);
} catch (err) {
print(err);
return Uint8List(0);
}
}