Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnetwork committed Sep 27, 2023
1 parent 6a6e1df commit 7a60a1b
Show file tree
Hide file tree
Showing 29 changed files with 422 additions and 381 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
## 1.0.0

* TransactionBuilder
* Fix bugs
* Fix bugs

## 1.0.1

* Update dependencies
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BITCOIN Dart Package
a comprehensive and versatile Go library for all your Bitcoin transaction needs. offers robust support for various Bitcoin transaction types, including spending transactions, Bitcoin address management, Bitcoin Schnorr signatures, BIP-39 mnemonic phrase generation, hierarchical deterministic (HD) wallet derivation, and Web3 Secret Storage Definition.
a comprehensive and versatile Dart library for all your Bitcoin transaction needs. offers robust support for various Bitcoin transaction types, including spending transactions, Bitcoin address management, Bitcoin Schnorr signatures, BIP-39 mnemonic phrase generation, hierarchical deterministic (HD) wallet derivation, and Web3 Secret Storage Definition.

For BIP32 HD wallet, BIP39, and Secret storage definitions, please refer to the [blockchain_utils](https://github.com/mrtnetwork/blockchain_utils) package.

Expand Down Expand Up @@ -32,7 +32,7 @@ This comprehensive package provides robust support for a wide array of Bitcoin t

### Create Transaction
Using this package, you can create a Bitcoin transaction in two ways: either through the `BtcTransaction` or the `BitcoinTransactionBuilder` class
- BtcTransaction: To use the `BtcTransaction` struct, you should have a general understanding of how Bitcoin transactions work, including knowledge of UTXOs, scripts, various types of scripts, Bitcoin addresses, signatures, and more. We created examples and tests to enhance your understanding. An example of this transaction type is explained below, and you can also find numerous examples in the [`test`](https://github.com/mrtnetwork/bitcoin_base/tree/main/test) folder.
- BtcTransaction: To use the `BtcTransaction` class, you should have a general understanding of how Bitcoin transactions work, including knowledge of UTXOs, scripts, various types of scripts, Bitcoin addresses, signatures, and more. We created examples and tests to enhance your understanding. An example of this transaction type is explained below, and you can also find numerous examples in the [`test`](https://github.com/mrtnetwork/bitcoin_base/tree/main/test) folder.

- BitcoinTransactionBuilder: Even with limited prior knowledge, you can utilize this class to send various types of transactions. Below, I've provided an example in which a transaction features 8 distinct input addresses with different types and private keys, as well as 10 different output addresses. Furthermore, additional examples have been prepared, which you can find in the [`example`](https://github.com/mrtnetwork/bitcoin_base/tree/main/example) folder.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// spend from 8 different address type to 10 different output
// ignore_for_file: unused_local_variable

import 'package:bitcoin_base/bitcoin.dart';
import 'package:bitcoin_base/src/bitcoin/address/core.dart';
import 'package:bitcoin_base/src/crypto/crypto.dart';
import 'package:bitcoin_base/src/models/network.dart';
import 'package:bitcoin_base/src/provider/api_provider.dart';
import 'package:bitcoin_base/src/provider/multisig_script.dart';
import 'package:bitcoin_base/src/provider/transaction_builder.dart';
import 'package:bitcoin_base/src/provider/utxo_details.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

void main() async {
// select network
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:bitcoin_base/bitcoin.dart';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:bitcoin_base/src/models/network.dart';
import 'package:bitcoin_base/src/provider/api_provider.dart';
import 'package:bitcoin_base/src/provider/transaction_builder.dart';
import 'package:bitcoin_base/src/provider/utxo_details.dart';
import 'package:blockchain_utils/blockchain_utils.dart';

// spend from 8 different address type to 10 different output
void main() async {
Expand Down
18 changes: 0 additions & 18 deletions lib/bitcoin.dart

This file was deleted.

8 changes: 7 additions & 1 deletion lib/bitcoin_base.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
library bitcoin;
/// library bitcoin_base
/// a comprehensive and versatile Go library for all your Bitcoin transaction needs.
/// offers robust support for various Bitcoin transaction types,
/// including spending transactions, Bitcoin address management,
/// Bitcoin Schnorr signatures, BIP-39 mnemonic phrase generation,
/// hierarchical deterministic (HD) wallet derivation, and Web3 Secret Storage Definition.
library bitcoin_base;

export 'package:bitcoin_base/src/bitcoin/address/segwit_address.dart';
export 'package:bitcoin_base/src/bitcoin/address/address.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bitcoin/address/address.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:typed_data';

// import 'package:bitcoin_base/src/base58/base58.dart' as bs58;
/// import 'package:bitcoin_base/src/base58/base58.dart' as bs58;
import 'package:bitcoin_base/src/bitcoin/address/core.dart';
import 'package:bitcoin_base/src/bitcoin/script/script.dart';
import 'package:bitcoin_base/src/bitcoin/tools/tools.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bitcoin/address/core.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:bitcoin_base/bitcoin.dart';
import 'package:bitcoin_base/bitcoin_base.dart';
import 'package:bitcoin_base/src/models/network.dart';

enum AddressType {
Expand Down
78 changes: 41 additions & 37 deletions lib/src/bitcoin/constant/constant.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: constant_identifier_names, equal_keys_in_map, non_constant_identifier_names
// Constants and identifiers used in the Bitcoin-related code.
/// ignore_for_file: constant_identifier_names, equal_keys_in_map, non_constant_identifier_names
/// Constants and identifiers used in the Bitcoin-related code.
// ignore_for_file: constant_identifier_names, non_constant_identifier_names, equal_keys_in_map

const Map<String, List<int>> OP_CODES = {
'OP_0': [0x00],
'OP_FALSE': [0x00],
Expand All @@ -25,7 +27,7 @@ const Map<String, List<int>> OP_CODES = {
'OP_15': [0x5f],
'OP_16': [0x60],

// flow control
/// flow control
'OP_NOP': [0x61],
'OP_IF': [0x63],
'OP_NOTIF': [0x64],
Expand All @@ -34,7 +36,7 @@ const Map<String, List<int>> OP_CODES = {
'OP_VERIFY': [0x69],
'OP_RETURN': [0x6a],

// stack
/// stack
'OP_TOALTSTACK': [0x6b],
'OP_FROMALTSTACK': [0x6c],
'OP_IFDUP': [0x73],
Expand All @@ -55,37 +57,39 @@ const Map<String, List<int>> OP_CODES = {
'OP_2ROT': [0x71],
'OP_2SWAP': [0x72],

// splice
// 'OP_CAT': [0x7e],
// 'OP_SUBSTR': [0x7f],
// 'OP_LEFT': [0x80],
// 'OP_RIGHT': [0x81],
/// splice
/// 'OP_CAT': [0x7e],
/// 'OP_SUBSTR': [0x7f],
/// 'OP_LEFT': [0x80],
/// 'OP_RIGHT': [0x81],
'OP_SIZE': [0x82],

// bitwise logic
// 'OP_INVERT': [0x83],
// 'OP_AND': [0x84],
// 'OP_OR': [0x85],
// 'OP_XOR': [0x86],
/// bitwise logic
/// 'OP_INVERT': [0x83],
/// 'OP_AND': [0x84],
/// 'OP_OR': [0x85],
/// 'OP_XOR': [0x86],
'OP_EQUAL': [0x87],
'OP_EQUALVERIFY': [0x88],

// arithmetic
/// arithmetic
'OP_1ADD': [0x8b],
'OP_1SUB': [0x8c],
// 'OP_2MUL': [0x8d],
// 'OP_2DIV': [0x8e],

/// 'OP_2MUL': [0x8d],
/// 'OP_2DIV': [0x8e],
'OP_NEGATE': [0x8f],
'OP_ABS': [0x90],
'OP_NOT': [0x91],
'OP_0NOTEQUAL': [0x92],
'OP_ADD': [0x93],
'OP_SUB': [0x94],
// 'OP_MUL': [0x95],
// 'OP_DIV': [0x96],
// 'OP_MOD': [0x97],
// 'OP_LSHIFT': [0x98],
// 'OP_RSHIFT': [0x99],

/// 'OP_MUL': [0x95],
/// 'OP_DIV': [0x96],
/// 'OP_MOD': [0x97],
/// 'OP_LSHIFT': [0x98],
/// 'OP_RSHIFT': [0x99],
'OP_BOOLAND': [0x9a],
'OP_BOOLOR': [0x9b],
'OP_NUMEQUAL': [0x9c],
Expand All @@ -99,7 +103,7 @@ const Map<String, List<int>> OP_CODES = {
'OP_MAX': [0xa4],
'OP_WITHIN': [0xa5],

// crypto
/// crypto
'OP_RIPEMD160': [0xa6],
'OP_SHA1': [0xa7],
'OP_SHA256': [0xa8],
Expand All @@ -111,15 +115,15 @@ const Map<String, List<int>> OP_CODES = {
'OP_CHECKMULTISIG': [0xae],
'OP_CHECKMULTISIGVERIFY': [0xaf],

// locktime
/// locktime
'OP_NOP2': [0xb1],
'OP_CHECKLOCKTIMEVERIFY': [0xb1],
'OP_NOP3': [0xb2],
'OP_CHECKSEQUENCEVERIFY': [0xb2],
};

Map<int, String> CODE_OPS = {
// constants
/// constants
0: 'OP_0',
76: 'OP_PUSHDATA1',
77: 'OP_PUSHDATA2',
Expand All @@ -142,7 +146,7 @@ Map<int, String> CODE_OPS = {
95: 'OP_15',
96: 'OP_16',

// flow control
/// flow control
97: 'OP_NOP',
99: 'OP_IF',
100: 'OP_NOTIF',
Expand All @@ -151,7 +155,7 @@ Map<int, String> CODE_OPS = {
105: 'OP_VERIFY',
106: 'OP_RETURN',

// stack
/// stack
107: 'OP_TOALTSTACK',
108: 'OP_FROMALTSTACK',
115: 'OP_IFDUP',
Expand All @@ -172,14 +176,14 @@ Map<int, String> CODE_OPS = {
113: 'OP_2ROT',
114: 'OP_2SWAP',

// splice
/// splice
130: 'OP_SIZE',

// bitwise logic
/// bitwise logic
135: 'OP_EQUAL',
136: 'OP_EQUALVERIFY',

// arithmetic
/// arithmetic
139: 'OP_1ADD',
140: 'OP_1SUB',
143: 'OP_NEGATE',
Expand All @@ -201,7 +205,7 @@ Map<int, String> CODE_OPS = {
164: 'OP_MAX',
165: 'OP_WITHIN',

// crypto
/// crypto
166: 'OP_RIPEMD160',
167: 'OP_SHA1',
168: 'OP_SHA256',
Expand All @@ -213,39 +217,39 @@ Map<int, String> CODE_OPS = {
174: 'OP_CHECKMULTISIG',
175: 'OP_CHECKMULTISIGVERIFY',

// locktime
/// locktime
177: 'OP_NOP2',
178: 'OP_NOP3',
177: 'OP_CHECKLOCKTIMEVERIFY',
178: 'OP_CHECKSEQUENCEVERIFY',
};

// SIGHASH types
/// SIGHASH types
const int SIGHASH_SINGLE = 0x03;
const int SIGHASH_ANYONECANPAY = 0x80;
const int SIGHASH_ALL = 0x01;
const int SIGHASH_NONE = 0x02;
const int TAPROOT_SIGHASH_ALL = 0x00;

// Transaction lock types
/// Transaction lock types
const int TYPE_ABSOLUTE_TIMELOCK = 0x101;
const int TYPE_RELATIVE_TIMELOCK = 0x201;
const int TYPE_REPLACE_BY_FEE = 0x301;

// Default values and sequences
/// Default values and sequences
const List<int> DEFAULT_TX_LOCKTIME = [0x00, 0x00, 0x00, 0x00];
const List<int> EMPTY_TX_SEQUENCE = [0x00, 0x00, 0x00, 0x00];
const List<int> DEFAULT_TX_SEQUENCE = [0xff, 0xff, 0xff, 0xff];
const List<int> ABSOLUTE_TIMELOCK_SEQUENCE = [0xfe, 0xff, 0xff, 0xff];
const List<int> REPLACE_BY_FEE_SEQUENCE = [0x01, 0x00, 0x00, 0x00];

// Script version and Bitcoin-related identifiers
/// Script version and Bitcoin-related identifiers
const int LEAF_VERSION_TAPSCRIPT = 0xc0;
const List<int> DEFAULT_TX_VERSION = [0x02, 0x00, 0x00, 0x00];
const int SATOSHIS_PER_BITCOIN = 100000000;
const int NEGATIVE_SATOSHI = -1;

// Bitcoin address types
/// Bitcoin address types
const String P2PKH_ADDRESS = "p2pkh";
const String P2SH_ADDRESS = "p2sh";
const String P2WPKH_ADDRESS_V0 = "p2wpkhv0";
Expand Down
3 changes: 2 additions & 1 deletion lib/src/bitcoin/script/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class TxInput {
TxInput(
{required this.txId, required this.txIndex, Script? sig, Uint8List? sq})
: sequence = sq ?? Uint8List.fromList(DEFAULT_TX_SEQUENCE),
// ignore: prefer_const_constructors

/// ignore: prefer_const_constructors
scriptSig = sig ?? Script(script: []);
final String txId;
final int txIndex;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bitcoin/script/script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:bitcoin_base/src/formating/bytes_num_formating.dart';

import 'package:bitcoin_base/src/formating/bytes_tracker.dart';

// ignore: constant_identifier_names
/// ignore: constant_identifier_names
enum ScriptType { P2PKH, P2SH, P2WPKH, P2WSH, P2PK }

/// A Script contains just a list of OP_CODES and also knows how to serialize into bytes
Expand Down
4 changes: 3 additions & 1 deletion lib/src/bitcoin/script/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ class BtcTransaction {
txForSign.add(hashAmounts);

for (final s in scriptPubKeys) {
final h = s.toHex(); // must checked
final h = s.toHex();

/// must checked
int scriptLen = h.length ~/ 2;
Uint8List scriptBytes = hexToBytes(h);
Uint8List lenBytes = Uint8List.fromList([scriptLen]);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/bitcoin/tools/tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ Uint8List pushInteger(int integer) {
throw ArgumentError('Integer is currently required to be positive.');
}

// Calculate the number of bytes required to represent the integer
/// Calculate the number of bytes required to represent the integer
int numberOfBytes = (integer.bitLength + 7) ~/ 8;

// Convert to little-endian bytes
/// Convert to little-endian bytes
Uint8List integerBytes = Uint8List(numberOfBytes);
for (int i = 0; i < numberOfBytes; i++) {
integerBytes[i] = (integer >> (i * 8)) & 0xFF;
}

// If the last bit is set, add a sign byte to signify a positive integer
/// If the last bit is set, add a sign byte to signify a positive integer
if ((integer & (1 << (numberOfBytes * 8 - 1))) != 0) {
integerBytes = Uint8List.fromList([...integerBytes, 0x00]);
}
Expand Down
Loading

0 comments on commit 7a60a1b

Please sign in to comment.