Line data Source code
1 : import 'dart:typed_data';
2 :
3 : import 'package:walletkit_dart/src/crypto/utxo/entities/op_codes.dart';
4 : import 'package:walletkit_dart/src/domain/extensions.dart';
5 :
6 : class PublicKeyScriptConverter {
7 : final Uint8List script;
8 :
9 : late final Uint8List publicKeyHash;
10 :
11 3 : PublicKeyScriptConverter(this.script) {
12 9 : if (script.length == 0) {
13 0 : throw Exception("Script length is 0");
14 : }
15 :
16 : // P2PKH
17 9 : if (script.length == 25) {
18 6 : if (script[0] != 25 &&
19 8 : script[1] != OPCODE.OP_DUP.hex &&
20 8 : script[2] != OPCODE.OP_HASH160.hex &&
21 8 : script[23] != OPCODE.OP_EQUALVERIFY.hex &&
22 0 : script.last != OPCODE.OP_CHECKSIG.hex) {
23 0 : throw Exception("Script is not P2PKH");
24 : }
25 6 : publicKeyHash = script.sublist(3, 23);
26 : return;
27 : }
28 :
29 : // P2WPKH
30 3 : if (script.length == 22) {
31 4 : if (script[0] != OPCODE.OP_0.hex && script[1] != 20) {
32 0 : throw Exception("Script is not P2WPKH");
33 : }
34 3 : publicKeyHash = script.sublist(2, 22);
35 : return;
36 : }
37 :
38 : // P2SH
39 0 : if (script.length == 23) {
40 0 : if (script.first != 23 &&
41 0 : script[1] != OPCODE.OP_HASH160.hex &&
42 0 : script[2] != 20 &&
43 0 : script.last != OPCODE.OP_EQUAL.hex) {
44 0 : throw Exception("Script is not P2SH");
45 : }
46 0 : publicKeyHash = script.sublist(3, 23);
47 : return;
48 : }
49 :
50 : // P2WPKH
51 0 : if (script.length == 34) {
52 0 : if (script.first != 34 && script[1] != OPCODE.OP_0.hex) {
53 0 : throw Exception("Script is not P2WPKH");
54 : }
55 0 : publicKeyHash = script.sublist(2, 34);
56 : return;
57 : }
58 :
59 0 : throw Exception("Unknown script type");
60 : }
61 :
62 0 : Uint8List get p2wpkhScript {
63 0 : assert(publicKeyHash.length == 20, "publicKeyHash.length != 20");
64 0 : return [
65 0 : OPCODE.OP_0.hex,
66 0 : publicKeyHash.length,
67 0 : ...publicKeyHash,
68 0 : ].toUint8List;
69 : }
70 :
71 0 : Uint8List get p2shScript {
72 0 : assert(publicKeyHash.length == 20, "publicKeyHash.length != 20");
73 0 : return [
74 0 : OPCODE.OP_HASH160.hex,
75 0 : publicKeyHash.length,
76 0 : ...publicKeyHash,
77 0 : OPCODE.OP_EQUAL.hex,
78 0 : ].toUint8List;
79 : }
80 :
81 3 : Uint8List get p2pkhScript {
82 9 : assert(publicKeyHash.length == 20, "publicKeyHash.length != 20");
83 3 : return [
84 3 : OPCODE.OP_DUP.hex,
85 3 : OPCODE.OP_HASH160.hex,
86 6 : publicKeyHash.length,
87 3 : ...publicKeyHash,
88 3 : OPCODE.OP_EQUALVERIFY.hex,
89 3 : OPCODE.OP_CHECKSIG.hex,
90 3 : ].toUint8List;
91 : }
92 :
93 0 : Uint8List get p2pkScript {
94 0 : assert(publicKeyHash.length == 33, "publicKeyHash.length != 33");
95 0 : return [
96 0 : OPCODE.OP_DUP.hex,
97 0 : OPCODE.OP_HASH160.hex,
98 0 : publicKeyHash.length,
99 0 : ...publicKeyHash,
100 0 : OPCODE.OP_EQUALVERIFY.hex,
101 0 : OPCODE.OP_CHECKSIG.hex,
102 0 : ].toUint8List;
103 : }
104 :
105 0 : Uint8List get p2wshScript {
106 0 : assert(publicKeyHash.length == 32, "publicKeyHash.length != 32");
107 0 : return [
108 0 : OPCODE.OP_0.hex,
109 0 : publicKeyHash.length,
110 0 : ...publicKeyHash,
111 0 : ].toUint8List;
112 : }
113 : }
|