Skip to content

Commit

Permalink
downgrade dart sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Dec 15, 2023
1 parent 959ed04 commit e84597e
Show file tree
Hide file tree
Showing 22 changed files with 236 additions and 154 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0

* Downgrade dart SDK from 3.1 to 2.15
* Update dependencies

## 2.0.1

* add some types for find network address support
Expand Down
2 changes: 1 addition & 1 deletion example/bch_example/bch_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:http/http.dart' as http;

BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold(
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);

final remind = sum - sumAll;
Expand Down
2 changes: 1 addition & 1 deletion example/bitcoin/bitcoin_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import '../example_service.dart';
/// Returns:
/// - The change value.
BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold(
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);

final remind = sum - sumAll;
Expand Down
2 changes: 1 addition & 1 deletion example/dash_example/dash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:http/http.dart' as http;

BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold(
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);

final remind = sum - sumAll;
Expand Down
2 changes: 1 addition & 1 deletion example/doge_example/doge_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:http/http.dart' as http;

BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold(
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);

final remind = sum - sumAll;
Expand Down
2 changes: 1 addition & 1 deletion example/litecoin_example/litecoin_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:http/http.dart' as http;

BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold(
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);

final remind = sum - sumAll;
Expand Down
53 changes: 37 additions & 16 deletions lib/src/bitcoin/address/core.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import 'package:bitcoin_base/bitcoin_base.dart';

enum BitcoinAddressType {
p2pkh("P2PKH"),
p2wpkh("P2WPKH"),
p2pk("P2PK"),
p2tr("P2TR"),
p2wsh("P2WSH"),
p2wshInP2sh("P2SH/P2WSH"),
p2wpkhInP2sh("P2SH/P2WPKh"),
p2pkhInP2sh("P2SH/P2PKH"),
p2pkInP2sh("P2SH/P2PK");

static BitcoinAddressType fromNameOrValue(String value) {
return values.firstWhere(
(element) => element.name == value || element.value == value);
}
class BitcoinAddressType {
static const BitcoinAddressType p2pkh = BitcoinAddressType._("P2PKH");
static const BitcoinAddressType p2wpkh = BitcoinAddressType._("P2WPKH");
static const BitcoinAddressType p2pk = BitcoinAddressType._("P2PK");
static const BitcoinAddressType p2tr = BitcoinAddressType._("P2TR");
static const BitcoinAddressType p2wsh = BitcoinAddressType._("P2WSH");
static const BitcoinAddressType p2wshInP2sh =
BitcoinAddressType._("P2SH/P2WSH");
static const BitcoinAddressType p2wpkhInP2sh =
BitcoinAddressType._("P2SH/P2WPKH");
static const BitcoinAddressType p2pkhInP2sh =
BitcoinAddressType._("P2SH/P2PKH");
static const BitcoinAddressType p2pkInP2sh =
BitcoinAddressType._("P2SH/P2PK");

final String value;
const BitcoinAddressType(this.value);

const BitcoinAddressType._(this.value);

/// Factory method to create a BitcoinAddressType enum value from a name or value.
static BitcoinAddressType fromValue(String value) {
return values.firstWhere((element) => element.value == value,
orElse: () =>
throw ArgumentError('Invalid BitcoinAddressType: $value'));
}

/// Check if the address type is Pay-to-Script-Hash (P2SH).
bool get isP2sh {
switch (this) {
case p2wshInP2sh:
Expand All @@ -30,6 +38,19 @@ enum BitcoinAddressType {
return false;
}
}

// Enum values as a list for iteration
static const List<BitcoinAddressType> values = [
p2pkh,
p2wpkh,
p2pk,
p2tr,
p2wsh,
p2wshInP2sh,
p2wpkhInP2sh,
p2pkhInP2sh,
p2pkInP2sh,
];
}

abstract class BitcoinAddress {
Expand Down
21 changes: 12 additions & 9 deletions lib/src/bitcoin/address/legacy_address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ abstract class LegacyAddress implements BitcoinAddress {
case BitcoinAddressType.p2pkInP2sh:
scriptBytes = [...network.p2shNetVer, ...scriptBytes];
break;
case const (BitcoinAddressType.p2pkh) || const (BitcoinAddressType.p2pk):
case BitcoinAddressType.p2pkh:
case BitcoinAddressType.p2pk:
scriptBytes = [...network.p2pkhNetVer, ...scriptBytes];
break;
default:
Expand Down Expand Up @@ -98,16 +99,16 @@ abstract class LegacyAddress implements BitcoinAddress {

class P2shAddress extends LegacyAddress {
P2shAddress.fromScript(
{required super.script, this.type = BitcoinAddressType.p2pkInP2sh})
: super.fromScript() {
{required Script script, this.type = BitcoinAddressType.p2pkInP2sh})
: super.fromScript(script: script) {
_validateP2shType();
}

P2shAddress.fromAddress(
{required super.address,
required super.network,
{required String address,
required BasedUtxoNetwork network,
this.type = BitcoinAddressType.p2pkInP2sh})
: super.fromAddress() {
: super.fromAddress(address: address, network: network) {
_validateP2shType();
}
P2shAddress.fromHash160(
Expand Down Expand Up @@ -141,9 +142,11 @@ class P2shAddress extends LegacyAddress {
}

class P2pkhAddress extends LegacyAddress {
P2pkhAddress.fromScript({required super.script}) : super.fromScript();
P2pkhAddress.fromAddress({required super.address, required super.network})
: super.fromAddress();
P2pkhAddress.fromScript({required Script script})
: super.fromScript(script: script);
P2pkhAddress.fromAddress(
{required String address, required BasedUtxoNetwork network})
: super.fromAddress(address: address, network: network);
P2pkhAddress.fromHash160({required String addrHash})
: super.fromHash160(addrHash);

Expand Down
60 changes: 39 additions & 21 deletions lib/src/bitcoin/address/segwit_address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ abstract class SegwitAddress implements BitcoinAddress {
String _addressToHash(String address, BasedUtxoNetwork network) {
final convert = SegwitBech32Decoder.decode(network.p2wpkhHrp, address);

final version = convert.$1;
final version = convert.item1;
if (version != segwitNumVersion) {
throw ArgumentError("Invalid segwit version.");
}
return BytesUtils.toHexString(convert.$2);
return BytesUtils.toHexString(convert.item2);
}

/// returns the address's string encoding (Bech32)
Expand All @@ -70,13 +70,19 @@ abstract class SegwitAddress implements BitcoinAddress {
}

class P2wpkhAddress extends SegwitAddress {
P2wpkhAddress.fromAddress({required super.address, required super.network})
: super.fromAddress(version: BitcoinOpCodeConst.P2WPKH_ADDRESS_V0);

P2wpkhAddress.fromProgram({required super.program})
: super.fromProgram(version: BitcoinOpCodeConst.P2WPKH_ADDRESS_V0);
P2wpkhAddress.fromScript({required super.script})
: super.fromScript(version: BitcoinOpCodeConst.P2WPKH_ADDRESS_V0);
P2wpkhAddress.fromAddress(
{required String address, required BasedUtxoNetwork network})
: super.fromAddress(
version: BitcoinOpCodeConst.P2WPKH_ADDRESS_V0,
address: address,
network: network);

P2wpkhAddress.fromProgram({required String program})
: super.fromProgram(
version: BitcoinOpCodeConst.P2WPKH_ADDRESS_V0, program: program);
P2wpkhAddress.fromScript({required Script script})
: super.fromScript(
version: BitcoinOpCodeConst.P2WPKH_ADDRESS_V0, script: script);

/// returns the scriptPubKey of a P2WPKH witness script
@override
Expand All @@ -90,12 +96,18 @@ class P2wpkhAddress extends SegwitAddress {
}

class P2trAddress extends SegwitAddress {
P2trAddress.fromAddress({required super.address, required super.network})
: super.fromAddress(version: BitcoinOpCodeConst.P2TR_ADDRESS_V1);
P2trAddress.fromProgram({required super.program})
: super.fromProgram(version: BitcoinOpCodeConst.P2TR_ADDRESS_V1);
P2trAddress.fromScript({required super.script})
: super.fromScript(version: BitcoinOpCodeConst.P2TR_ADDRESS_V1);
P2trAddress.fromAddress(
{required String address, required BasedUtxoNetwork network})
: super.fromAddress(
version: BitcoinOpCodeConst.P2TR_ADDRESS_V1,
address: address,
network: network);
P2trAddress.fromProgram({required String program})
: super.fromProgram(
version: BitcoinOpCodeConst.P2TR_ADDRESS_V1, program: program);
P2trAddress.fromScript({required Script script})
: super.fromScript(
version: BitcoinOpCodeConst.P2TR_ADDRESS_V1, script: script);

/// returns the scriptPubKey of a P2TR witness script
@override
Expand All @@ -109,12 +121,18 @@ class P2trAddress extends SegwitAddress {
}

class P2wshAddress extends SegwitAddress {
P2wshAddress.fromAddress({required super.address, required super.network})
: super.fromAddress(version: BitcoinOpCodeConst.P2WSH_ADDRESS_V0);
P2wshAddress.fromProgram({required super.program})
: super.fromProgram(version: BitcoinOpCodeConst.P2WSH_ADDRESS_V0);
P2wshAddress.fromScript({required super.script})
: super.fromScript(version: BitcoinOpCodeConst.P2WSH_ADDRESS_V0);
P2wshAddress.fromAddress(
{required String address, required BasedUtxoNetwork network})
: super.fromAddress(
version: BitcoinOpCodeConst.P2WSH_ADDRESS_V0,
address: address,
network: network);
P2wshAddress.fromProgram({required String program})
: super.fromProgram(
version: BitcoinOpCodeConst.P2WSH_ADDRESS_V0, program: program);
P2wshAddress.fromScript({required Script script})
: super.fromScript(
version: BitcoinOpCodeConst.P2WSH_ADDRESS_V0, script: script);

/// Returns the scriptPubKey of a P2WPKH witness script
@override
Expand Down
4 changes: 2 additions & 2 deletions lib/src/bitcoin/address/validate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ List<int>? decodeBchAddress(
}

final decode = BchBech32Decoder.decode(hrp, address);
if (bytesEqual(decode.$1, netVersion)) {
return decode.$2;
if (bytesEqual(decode.item1, netVersion)) {
return decode.item2;
}
return null;
} catch (e) {
Expand Down
28 changes: 14 additions & 14 deletions lib/src/bitcoin/script/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:bitcoin_base/src/bitcoin/script/op_code/constant.dart';
import 'package:blockchain_utils/binary/binary_operation.dart';
import 'package:blockchain_utils/binary/utils.dart';
import 'package:blockchain_utils/numbers/int_utils.dart';
import 'package:blockchain_utils/tuple/tuple.dart';
import 'script.dart';

/// A transaction input requires a transaction id of a UTXO and the index of that UTXO.
Expand Down Expand Up @@ -49,7 +50,7 @@ class TxInput {
return data;
}

static (TxInput, int) fromRaw(
static Tuple<TxInput, int> fromRaw(
{required String raw, int cursor = 0, bool hasSegwit = false}) {
final txInputRaw = BytesUtils.fromHexString(raw);
List<int> inpHash =
Expand All @@ -62,20 +63,19 @@ class TxInput {
txInputRaw.sublist(cursor + 32, cursor + 36).reversed.toList();
cursor += 36;
final vi = IntUtils.decodeVarint(txInputRaw.sublist(cursor, cursor + 9));
cursor += vi.$2;
List<int> unlockingScript = txInputRaw.sublist(cursor, cursor + vi.$1);
cursor += vi.$1;
cursor += vi.item2;
List<int> unlockingScript = txInputRaw.sublist(cursor, cursor + vi.item1);
cursor += vi.item1;
List<int> sequenceNumberData = txInputRaw.sublist(cursor, cursor + 4);
cursor += 4;
return (
TxInput(
txId: BytesUtils.toHexString(inpHash),
txIndex: int.parse(BytesUtils.toHexString(outputN), radix: 16),
scriptSig: Script.fromRaw(
hexData: BytesUtils.toHexString(unlockingScript),
hasSegwit: hasSegwit),
sequance: sequenceNumberData),
cursor
);
return Tuple(
TxInput(
txId: BytesUtils.toHexString(inpHash),
txIndex: int.parse(BytesUtils.toHexString(outputN), radix: 16),
scriptSig: Script.fromRaw(
hexData: BytesUtils.toHexString(unlockingScript),
hasSegwit: hasSegwit),
sequance: sequenceNumberData),
cursor);
}
}
24 changes: 12 additions & 12 deletions lib/src/bitcoin/script/output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:bitcoin_base/src/bitcoin/script/script.dart';
import 'package:blockchain_utils/binary/utils.dart';
import 'package:blockchain_utils/numbers/bigint_utils.dart';
import 'package:blockchain_utils/numbers/int_utils.dart';
import 'package:blockchain_utils/tuple/tuple.dart';

/// Represents a transaction output.
///
Expand Down Expand Up @@ -31,7 +32,7 @@ class TxOutput {
return data;
}

static (TxOutput, int) fromRaw(
static Tuple<TxOutput, int> fromRaw(
{required String raw, required int cursor, bool hasSegwit = false}) {
final txoutputraw = BytesUtils.fromHexString(raw);
final value = BigintUtils.fromBytes(txoutputraw.sublist(cursor, cursor + 8),
Expand All @@ -40,16 +41,15 @@ class TxOutput {
cursor += 8;

final vi = IntUtils.decodeVarint(txoutputraw.sublist(cursor, cursor + 9));
cursor += vi.$2;
List<int> lockScript = txoutputraw.sublist(cursor, cursor + vi.$1);
cursor += vi.$1;
return (
TxOutput(
amount: value,
scriptPubKey: Script.fromRaw(
hexData: BytesUtils.toHexString(lockScript),
hasSegwit: hasSegwit)),
cursor
);
cursor += vi.item2;
List<int> lockScript = txoutputraw.sublist(cursor, cursor + vi.item1);
cursor += vi.item1;
return Tuple(
TxOutput(
amount: value,
scriptPubKey: Script.fromRaw(
hexData: BytesUtils.toHexString(lockScript),
hasSegwit: hasSegwit)),
cursor);
}
}
Loading

0 comments on commit e84597e

Please sign in to comment.