Line data Source code
1 : import 'dart:typed_data';
2 :
3 : import 'package:convert/convert.dart';
4 : import 'package:walletkit_dart/src/crypto/utxo/entities/raw_transaction/input.dart';
5 : import 'package:walletkit_dart/src/crypto/utxo/entities/script.dart';
6 : import 'package:walletkit_dart/src/utils/int.dart';
7 : import 'package:walletkit_dart/src/utils/var_uint.dart';
8 :
9 : const value_length = 8;
10 :
11 : abstract class Output {
12 : final BigInt value;
13 : final Uint8List scriptPubKey;
14 :
15 10 : BigInt get weight => 1.toBI + getScriptWeight(scriptPubKey);
16 :
17 18 : int get intValue => value.toInt();
18 :
19 0 : String get scriptPubKeyHex => hex.encode(scriptPubKey);
20 :
21 9 : int get size => bytes.length;
22 :
23 : Uint8List get bytes;
24 :
25 0 : String get toHex => hex.encode(bytes);
26 :
27 6 : const Output({
28 : required this.value,
29 : required this.scriptPubKey,
30 : });
31 : }
32 :
33 : class BTCOutput extends Output {
34 5 : const BTCOutput({
35 : required super.value,
36 : required super.scriptPubKey,
37 : });
38 :
39 2 : factory BTCOutput.fromBuffer(Uint8List buffer) {
40 : var offset = 0;
41 :
42 : /// Value
43 4 : final (value, off1) = buffer.bytes.readUint64(offset);
44 2 : offset += off1;
45 :
46 : /// ScriptPubKey
47 2 : final (scriptPubKey, off2) = buffer.readVarSlice(offset);
48 2 : offset += off2;
49 :
50 2 : return BTCOutput(
51 2 : value: BigInt.from(value),
52 : scriptPubKey: scriptPubKey,
53 : );
54 : }
55 :
56 5 : Uint8List get bytes {
57 25 : final buffer = Uint8List(value_length + scriptPubKey.length + 1);
58 :
59 : var offset = 0;
60 :
61 : // Write Value
62 20 : offset += buffer.bytes.writeUint64(offset, intValue);
63 :
64 : // Write ScriptPubKey
65 15 : offset += buffer.writeVarSlice(offset, scriptPubKey);
66 :
67 : return buffer;
68 : }
69 : }
70 :
71 : class EC8Output extends Output {
72 2 : const EC8Output({
73 : required super.value,
74 : required super.scriptPubKey,
75 : });
76 :
77 2 : Uint8List get bytes {
78 2 : final buffer = Uint8List(
79 10 : value_length + weight_length + scriptPubKey.length + 1,
80 : );
81 :
82 : var offset = 0;
83 :
84 : // Write Value
85 8 : offset += buffer.bytes.writeUint64(offset, intValue);
86 :
87 : // Write Weight
88 10 : offset += buffer.bytes.writeUint32(offset, weight.toInt()); // Should be 146
89 :
90 : // Write ScriptPubKey
91 6 : offset += buffer.writeVarSlice(offset, scriptPubKey);
92 :
93 : return buffer;
94 : }
95 :
96 1 : Uint8List get bytesForTxId {
97 1 : final buffer = Uint8List(
98 5 : value_length + weight_length + scriptPubKey.length + 1,
99 : );
100 :
101 : var offset = 0;
102 :
103 : // Write Value
104 4 : offset += buffer.bytes.writeUint64(offset, intValue);
105 :
106 : // Write Weight
107 3 : offset += buffer.bytes.writeUint32(offset, 0);
108 :
109 : // Write ScriptPubKey
110 3 : offset += buffer.writeVarSlice(offset, scriptPubKey);
111 :
112 : return buffer;
113 : }
114 :
115 2 : factory EC8Output.fromBuffer(Uint8List buffer) {
116 : var offset = 0;
117 :
118 : /// Value
119 4 : final (value, off1) = buffer.bytes.readUint64(offset);
120 2 : offset += off1;
121 :
122 : /// Weight (is ingored since it is calculated)
123 4 : final (_, off2) = buffer.bytes.readUint32(offset);
124 2 : offset += off2;
125 :
126 : /// ScriptPubKey
127 2 : final (scriptPubKey, off3) = buffer.readVarSlice(offset);
128 2 : offset += off3;
129 :
130 2 : return EC8Output(
131 2 : value: value.toBI,
132 : scriptPubKey: scriptPubKey,
133 : );
134 : }
135 : }
|