@ckb-ccc/core 0.0.9-alpha.1 → 0.0.10-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ckb/transaction.d.ts +19 -15
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +44 -10
- package/dist/client/client.d.ts +1 -1
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.advanced.js +30 -2
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.js +1 -1
- package/dist/client/clientPublicTestnet.advanced.js +3 -3
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.js +1 -1
- package/dist/client/clientTypes.advanced.d.ts +8 -8
- package/dist/client/clientTypes.advanced.d.ts.map +1 -1
- package/dist/client/clientTypes.d.ts +2 -2
- package/dist/client/clientTypes.d.ts.map +1 -1
- package/dist/client/clientTypes.js +2 -2
- package/dist/signer/ckb/index.d.ts +3 -0
- package/dist/signer/ckb/index.d.ts.map +1 -1
- package/dist/signer/ckb/index.js +3 -0
- package/dist/signer/ckb/signerCkbPrivateKey.d.ts +12 -0
- package/dist/signer/ckb/signerCkbPrivateKey.d.ts.map +1 -0
- package/dist/signer/ckb/signerCkbPrivateKey.js +38 -0
- package/dist/signer/ckb/signerCkbPublicKey.d.ts +18 -0
- package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +1 -0
- package/dist/signer/ckb/signerCkbPublicKey.js +44 -0
- package/dist/signer/ckb/verifyCkbSecp256k1.d.ts +5 -0
- package/dist/signer/ckb/verifyCkbSecp256k1.d.ts.map +1 -0
- package/dist/signer/ckb/verifyCkbSecp256k1.js +14 -0
- package/dist/signer/evm/signerEvm.d.ts +2 -1
- package/dist/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts +2 -2
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist/signer/signer/index.d.ts +8 -6
- package/dist/signer/signer/index.d.ts.map +1 -1
- package/dist/signer/signer/index.js +9 -5
- package/dist/utils/index.d.ts +18 -9
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -2
- package/dist.commonjs/address/address.advanced.d.ts +29 -24
- package/dist.commonjs/address/address.advanced.js +115 -89
- package/dist.commonjs/address/advanced.d.ts +1 -1
- package/dist.commonjs/address/advanced.js +30 -14
- package/dist.commonjs/address/index.d.ts +51 -44
- package/dist.commonjs/address/index.js +84 -65
- package/dist.commonjs/advanced.d.ts +1 -1
- package/dist.commonjs/advanced.js +47 -22
- package/dist.commonjs/advancedBarrel.d.ts +1 -1
- package/dist.commonjs/advancedBarrel.js +30 -14
- package/dist.commonjs/barrel.d.ts +1 -1
- package/dist.commonjs/barrel.js +30 -14
- package/dist.commonjs/bytes/advanced.d.ts +11 -2
- package/dist.commonjs/bytes/index.d.ts +9 -3
- package/dist.commonjs/bytes/index.js +28 -26
- package/dist.commonjs/ckb/advanced.d.ts +1 -1
- package/dist.commonjs/ckb/advanced.js +47 -22
- package/dist.commonjs/ckb/index.d.ts +1 -1
- package/dist.commonjs/ckb/index.js +30 -14
- package/dist.commonjs/ckb/molecule.advanced/generated.d.ts +420 -260
- package/dist.commonjs/ckb/molecule.advanced/generated.js +1536 -1332
- package/dist.commonjs/ckb/molecule.advanced/index.d.ts +3 -3
- package/dist.commonjs/ckb/molecule.advanced/index.js +34 -18
- package/dist.commonjs/ckb/script.advanced.d.ts +1 -1
- package/dist.commonjs/ckb/script.advanced.js +12 -9
- package/dist.commonjs/ckb/script.d.ts +121 -117
- package/dist.commonjs/ckb/script.js +220 -176
- package/dist.commonjs/ckb/transaction.advanced.d.ts +1 -1
- package/dist.commonjs/ckb/transaction.advanced.js +4 -4
- package/dist.commonjs/ckb/transaction.d.ts +806 -718
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +1425 -1158
- package/dist.commonjs/client/advanced.d.ts +1 -1
- package/dist.commonjs/client/advanced.js +30 -14
- package/dist.commonjs/client/client.advanced.d.ts +23 -6
- package/dist.commonjs/client/client.advanced.js +89 -61
- package/dist.commonjs/client/client.d.ts +114 -54
- package/dist.commonjs/client/client.d.ts.map +1 -1
- package/dist.commonjs/client/client.js +215 -182
- package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +8 -4
- package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicMainnet.advanced.js +215 -161
- package/dist.commonjs/client/clientPublicMainnet.d.ts +8 -6
- package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicMainnet.js +17 -15
- package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +6 -3
- package/dist.commonjs/client/clientPublicTestnet.advanced.js +227 -199
- package/dist.commonjs/client/clientPublicTestnet.d.ts +8 -6
- package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicTestnet.js +13 -13
- package/dist.commonjs/client/clientTypes.advanced.d.ts +14 -14
- package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/clientTypes.advanced.js +1 -1
- package/dist.commonjs/client/clientTypes.d.ts +52 -27
- package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
- package/dist.commonjs/client/clientTypes.js +56 -22
- package/dist.commonjs/client/index.d.ts +1 -1
- package/dist.commonjs/client/index.js +30 -14
- package/dist.commonjs/client/jsonRpc/advanced.d.ts +1 -1
- package/dist.commonjs/client/jsonRpc/advanced.js +30 -14
- package/dist.commonjs/client/jsonRpc/index.d.ts +94 -75
- package/dist.commonjs/client/jsonRpc/index.js +135 -108
- package/dist.commonjs/client/jsonRpc/transformers.d.ts +76 -36
- package/dist.commonjs/client/jsonRpc/transformers.js +174 -152
- package/dist.commonjs/client/jsonRpc/types.d.ts +38 -38
- package/dist.commonjs/fixedPoint/index.d.ts +9 -3
- package/dist.commonjs/fixedPoint/index.js +26 -20
- package/dist.commonjs/fixedPoint/index.test.d.ts +1 -1
- package/dist.commonjs/fixedPoint/index.test.js +42 -36
- package/dist.commonjs/hasher/advanced.d.ts +1 -1
- package/dist.commonjs/hasher/index.d.ts +40 -37
- package/dist.commonjs/hasher/index.js +58 -48
- package/dist.commonjs/hex/index.d.ts +1 -1
- package/dist.commonjs/hex/index.js +1 -1
- package/dist.commonjs/index.d.ts +1 -1
- package/dist.commonjs/index.js +47 -22
- package/dist.commonjs/num/index.d.ts +1 -1
- package/dist.commonjs/num/index.js +39 -27
- package/dist.commonjs/signer/btc/index.d.ts +1 -1
- package/dist.commonjs/signer/btc/index.js +30 -14
- package/dist.commonjs/signer/btc/signerBtc.d.ts +47 -47
- package/dist.commonjs/signer/btc/signerBtc.js +89 -66
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +45 -45
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +52 -52
- package/dist.commonjs/signer/btc/verify.d.ts +6 -2
- package/dist.commonjs/signer/btc/verify.js +10 -3
- package/dist.commonjs/signer/ckb/index.d.ts +4 -1
- package/dist.commonjs/signer/ckb/index.d.ts.map +1 -1
- package/dist.commonjs/signer/ckb/index.js +33 -14
- package/dist.commonjs/signer/ckb/signerCkbPrivateKey.d.ts +12 -0
- package/dist.commonjs/signer/ckb/signerCkbPrivateKey.d.ts.map +1 -0
- package/dist.commonjs/signer/ckb/signerCkbPrivateKey.js +56 -0
- package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts +18 -0
- package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +1 -0
- package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +58 -0
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +45 -45
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +56 -56
- package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.d.ts +11 -0
- package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.d.ts.map +1 -0
- package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.js +27 -0
- package/dist.commonjs/signer/ckb/verifyJoyId.d.ts +6 -2
- package/dist.commonjs/signer/ckb/verifyJoyId.js +11 -8
- package/dist.commonjs/signer/dummy/alwaysError.d.ts +4 -4
- package/dist.commonjs/signer/dummy/alwaysError.js +7 -7
- package/dist.commonjs/signer/dummy/dummy.d.ts +7 -7
- package/dist.commonjs/signer/dummy/dummy.js +16 -16
- package/dist.commonjs/signer/dummy/index.d.ts +1 -1
- package/dist.commonjs/signer/dummy/index.js +30 -14
- package/dist.commonjs/signer/dummy/openLink.d.ts +4 -4
- package/dist.commonjs/signer/dummy/openLink.js +7 -7
- package/dist.commonjs/signer/evm/index.d.ts +1 -1
- package/dist.commonjs/signer/evm/index.js +30 -14
- package/dist.commonjs/signer/evm/signerEvm.d.ts +43 -38
- package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/signerEvm.js +105 -70
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +38 -38
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +42 -42
- package/dist.commonjs/signer/evm/verify.d.ts +6 -2
- package/dist.commonjs/signer/evm/verify.js +7 -2
- package/dist.commonjs/signer/index.d.ts +1 -1
- package/dist.commonjs/signer/index.js +30 -14
- package/dist.commonjs/signer/nostr/index.d.ts +1 -1
- package/dist.commonjs/signer/nostr/index.js +30 -14
- package/dist.commonjs/signer/nostr/signerNostr.d.ts +57 -57
- package/dist.commonjs/signer/nostr/signerNostr.js +100 -71
- package/dist.commonjs/signer/nostr/verify.d.ts +9 -3
- package/dist.commonjs/signer/nostr/verify.js +50 -39
- package/dist.commonjs/signer/signer/index.d.ts +171 -160
- package/dist.commonjs/signer/signer/index.d.ts.map +1 -1
- package/dist.commonjs/signer/signer/index.js +235 -195
- package/dist.commonjs/utils/index.d.ts +56 -14
- package/dist.commonjs/utils/index.d.ts.map +1 -1
- package/dist.commonjs/utils/index.js +19 -13
- package/package.json +2 -2
- package/src/ckb/transaction.ts +87 -33
- package/src/client/client.ts +1 -1
- package/src/client/clientPublicMainnet.advanced.ts +34 -2
- package/src/client/clientPublicMainnet.ts +1 -1
- package/src/client/clientPublicTestnet.advanced.ts +3 -3
- package/src/client/clientPublicTestnet.ts +1 -1
- package/src/client/clientTypes.advanced.ts +8 -8
- package/src/client/clientTypes.ts +4 -4
- package/src/signer/ckb/index.ts +3 -0
- package/src/signer/ckb/signerCkbPrivateKey.ts +58 -0
- package/src/signer/ckb/signerCkbPublicKey.ts +63 -0
- package/src/signer/ckb/verifyCkbSecp256k1.ts +27 -0
- package/src/signer/evm/signerEvm.ts +2 -2
- package/src/signer/evm/signerEvmAddressReadonly.ts +1 -1
- package/src/signer/signer/index.ts +17 -9
- package/src/utils/index.ts +24 -12
|
@@ -1,29 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
("get" in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o["default"] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
19
37
|
if (mod && mod.__esModule) return mod;
|
|
20
38
|
var result = {};
|
|
21
|
-
if (mod != null)
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
22
43
|
__setModuleDefault(result, mod);
|
|
23
44
|
return result;
|
|
24
|
-
};
|
|
45
|
+
};
|
|
25
46
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.Transaction =
|
|
47
|
+
exports.Transaction =
|
|
48
|
+
exports.udtBalanceFrom =
|
|
49
|
+
exports.WitnessArgs =
|
|
50
|
+
exports.CellDep =
|
|
51
|
+
exports.CellInput =
|
|
52
|
+
exports.Cell =
|
|
53
|
+
exports.CellOutput =
|
|
54
|
+
exports.OutPoint =
|
|
55
|
+
exports.depTypeFromBytes =
|
|
56
|
+
exports.depTypeToBytes =
|
|
57
|
+
exports.depTypeFrom =
|
|
58
|
+
void 0;
|
|
27
59
|
const bytes_1 = require("../bytes");
|
|
28
60
|
const fixedPoint_1 = require("../fixedPoint");
|
|
29
61
|
const hasher_1 = require("../hasher");
|
|
@@ -48,19 +80,19 @@ const transaction_advanced_1 = require("./transaction.advanced");
|
|
|
48
80
|
* ```
|
|
49
81
|
*/
|
|
50
82
|
function depTypeFrom(val) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
83
|
+
const depType = (() => {
|
|
84
|
+
if (typeof val === "number") {
|
|
85
|
+
return transaction_advanced_1.NUM_TO_DEP_TYPE[val];
|
|
86
|
+
}
|
|
87
|
+
if (typeof val === "bigint") {
|
|
88
|
+
return transaction_advanced_1.NUM_TO_DEP_TYPE[Number(val)];
|
|
89
|
+
}
|
|
90
|
+
return val;
|
|
91
|
+
})();
|
|
92
|
+
if (depType === undefined) {
|
|
93
|
+
throw new Error(`Invalid dep type ${val}`);
|
|
94
|
+
}
|
|
95
|
+
return depType;
|
|
64
96
|
}
|
|
65
97
|
exports.depTypeFrom = depTypeFrom;
|
|
66
98
|
/**
|
|
@@ -75,7 +107,9 @@ exports.depTypeFrom = depTypeFrom;
|
|
|
75
107
|
* ```
|
|
76
108
|
*/
|
|
77
109
|
function depTypeToBytes(depType) {
|
|
78
|
-
|
|
110
|
+
return (0, bytes_1.bytesFrom)([
|
|
111
|
+
transaction_advanced_1.DEP_TYPE_TO_NUM[depTypeFrom(depType)],
|
|
112
|
+
]);
|
|
79
113
|
}
|
|
80
114
|
exports.depTypeToBytes = depTypeToBytes;
|
|
81
115
|
/**
|
|
@@ -92,1167 +126,1400 @@ exports.depTypeToBytes = depTypeToBytes;
|
|
|
92
126
|
* ```
|
|
93
127
|
*/
|
|
94
128
|
function depTypeFromBytes(bytes) {
|
|
95
|
-
|
|
129
|
+
return transaction_advanced_1.NUM_TO_DEP_TYPE[
|
|
130
|
+
(0, bytes_1.bytesFrom)(bytes)[0]
|
|
131
|
+
];
|
|
96
132
|
}
|
|
97
133
|
exports.depTypeFromBytes = depTypeFromBytes;
|
|
98
134
|
class OutPoint {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Creates an instance of OutPoint.
|
|
137
|
+
*
|
|
138
|
+
* @param txHash - The transaction hash.
|
|
139
|
+
* @param index - The index of the output in the transaction.
|
|
140
|
+
*/
|
|
141
|
+
constructor(txHash, index) {
|
|
142
|
+
this.txHash = txHash;
|
|
143
|
+
this.index = index;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Clone an OutPoint.
|
|
147
|
+
*
|
|
148
|
+
* @returns A cloned OutPoint instance.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const outPoint1 = outPoint0.clone();
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
clone() {
|
|
156
|
+
return new OutPoint(this.txHash, this.index);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Creates an OutPoint instance from an OutPointLike object.
|
|
160
|
+
*
|
|
161
|
+
* @param outPoint - An OutPointLike object or an instance of OutPoint.
|
|
162
|
+
* @returns An OutPoint instance.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const outPoint = OutPoint.from({ txHash: "0x...", index: 0 });
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
static from(outPoint) {
|
|
170
|
+
if (outPoint instanceof OutPoint) {
|
|
171
|
+
return outPoint;
|
|
172
|
+
}
|
|
173
|
+
return new OutPoint(
|
|
174
|
+
(0, hex_1.hexFrom)(outPoint.txHash),
|
|
175
|
+
(0, num_1.numFrom)(outPoint.index),
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Converts the OutPoint instance to molecule data format.
|
|
180
|
+
*
|
|
181
|
+
* @returns An object representing the outpoint in molecule data format.
|
|
182
|
+
*/
|
|
183
|
+
_toMolData() {
|
|
184
|
+
return {
|
|
185
|
+
txHash: (0, bytes_1.bytesFrom)(this.txHash),
|
|
186
|
+
index: (0, num_1.numToBytes)(this.index, 4),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Converts the OutPoint instance to bytes.
|
|
191
|
+
*
|
|
192
|
+
* @returns A Uint8Array containing the outpoint bytes.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const outPointBytes = outPoint.toBytes();
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
toBytes() {
|
|
200
|
+
return (0, bytes_1.bytesFrom)(mol.SerializeOutPoint(this._toMolData()));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Creates an OutPoint instance from a byte-like value or molecule OutPoint.
|
|
204
|
+
*
|
|
205
|
+
* @param bytes - The byte-like value or molecule OutPoint to convert.
|
|
206
|
+
* @returns An OutPoint instance.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* const outPoint = OutPoint.fromBytes(new Uint8Array([/* outpoint bytes *\/]));
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
static fromBytes(bytes) {
|
|
214
|
+
const view =
|
|
215
|
+
bytes instanceof mol.OutPoint
|
|
216
|
+
? bytes
|
|
217
|
+
: new mol.OutPoint((0, bytes_1.bytesFrom)(bytes));
|
|
218
|
+
return new OutPoint(
|
|
219
|
+
(0, hex_1.hexFrom)(view.getTxHash().raw()),
|
|
220
|
+
(0, num_1.numFromBytes)(view.getIndex().raw()),
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Compares the current OutPoint instance with another OutPointLike object for equality.
|
|
225
|
+
*
|
|
226
|
+
* @param val - The OutPointLike object to compare with.
|
|
227
|
+
* @returns True if the out points are equal, otherwise false.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const isEqual = outPoint.eq(anotherOutPoint);
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
eq(val) {
|
|
235
|
+
const outPoint = OutPoint.from(val);
|
|
236
|
+
return this.txHash === outPoint.txHash && this.index === outPoint.index;
|
|
237
|
+
}
|
|
195
238
|
}
|
|
196
239
|
exports.OutPoint = OutPoint;
|
|
197
240
|
class CellOutput {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
241
|
+
/**
|
|
242
|
+
* Creates an instance of CellOutput.
|
|
243
|
+
*
|
|
244
|
+
* @param capacity - The capacity of the cell.
|
|
245
|
+
* @param lock - The lock script of the cell.
|
|
246
|
+
* @param type - The optional type script of the cell.
|
|
247
|
+
*/
|
|
248
|
+
constructor(capacity, lock, type) {
|
|
249
|
+
this.capacity = capacity;
|
|
250
|
+
this.lock = lock;
|
|
251
|
+
this.type = type;
|
|
252
|
+
}
|
|
253
|
+
get occupiedSize() {
|
|
254
|
+
return 8 + this.lock.occupiedSize + (this.type?.occupiedSize ?? 0);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Clone a CellOutput.
|
|
258
|
+
*
|
|
259
|
+
* @returns A cloned CellOutput instance.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* const cellOutput1 = cellOutput0.clone();
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
clone() {
|
|
267
|
+
return new CellOutput(this.capacity, this.lock.clone(), this.type?.clone());
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Creates a CellOutput instance from a CellOutputLike object.
|
|
271
|
+
*
|
|
272
|
+
* @param cellOutput - A CellOutputLike object or an instance of CellOutput.
|
|
273
|
+
* @returns A CellOutput instance.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* const cellOutput = CellOutput.from({
|
|
278
|
+
* capacity: 1000n,
|
|
279
|
+
* lock: { codeHash: "0x...", hashType: "type", args: "0x..." },
|
|
280
|
+
* type: { codeHash: "0x...", hashType: "type", args: "0x..." }
|
|
281
|
+
* });
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
static from(cellOutput) {
|
|
285
|
+
if (cellOutput instanceof CellOutput) {
|
|
286
|
+
return cellOutput;
|
|
287
|
+
}
|
|
288
|
+
return new CellOutput(
|
|
289
|
+
(0, num_1.numFrom)(cellOutput.capacity),
|
|
290
|
+
script_1.Script.from(cellOutput.lock),
|
|
291
|
+
(0, utils_1.apply)(script_1.Script.from, cellOutput.type),
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Converts the CellOutput instance to molecule data format.
|
|
296
|
+
*
|
|
297
|
+
* @returns An object representing the cell output in molecule data format.
|
|
298
|
+
*/
|
|
299
|
+
_toMolData() {
|
|
300
|
+
return {
|
|
301
|
+
capacity: (0, num_1.numToBytes)(this.capacity, 8),
|
|
302
|
+
lock: this.lock._toMolData(),
|
|
303
|
+
type: this.type?._toMolData(),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Converts the CellOutput instance to bytes.
|
|
308
|
+
*
|
|
309
|
+
* @returns A Uint8Array containing the cell output bytes.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* const cellOutputBytes = cellOutput.toBytes();
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
toBytes() {
|
|
317
|
+
return (0, bytes_1.bytesFrom)(mol.SerializeCellOutput(this._toMolData()));
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Creates a CellOutput instance from a byte-like value or molecule CellOutput.
|
|
321
|
+
*
|
|
322
|
+
* @param bytes - The byte-like value or molecule CellOutput to convert.
|
|
323
|
+
* @returns A CellOutput instance.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```typescript
|
|
327
|
+
* const cellOutput = CellOutput.fromBytes(new Uint8Array([/* cell output bytes *\/]));
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
static fromBytes(bytes) {
|
|
331
|
+
const view =
|
|
332
|
+
bytes instanceof mol.CellOutput
|
|
333
|
+
? bytes
|
|
334
|
+
: new mol.CellOutput((0, bytes_1.bytesFrom)(bytes));
|
|
335
|
+
return new CellOutput(
|
|
336
|
+
(0, num_1.numFromBytes)(view.getCapacity().raw()),
|
|
337
|
+
script_1.Script.fromBytes(view.getLock()),
|
|
338
|
+
(0, utils_1.apply)(
|
|
339
|
+
script_1.Script.fromBytes,
|
|
340
|
+
mol.molOptional(view.getType()),
|
|
341
|
+
),
|
|
342
|
+
);
|
|
343
|
+
}
|
|
289
344
|
}
|
|
290
345
|
exports.CellOutput = CellOutput;
|
|
291
346
|
class Cell {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
347
|
+
/**
|
|
348
|
+
* Creates an instance of Cell.
|
|
349
|
+
*
|
|
350
|
+
* @param outPoint - The output point of the cell.
|
|
351
|
+
* @param cellOutput - The cell output of the cell.
|
|
352
|
+
* @param outputData - The output data of the cell.
|
|
353
|
+
*/
|
|
354
|
+
constructor(outPoint, cellOutput, outputData) {
|
|
355
|
+
this.outPoint = outPoint;
|
|
356
|
+
this.cellOutput = cellOutput;
|
|
357
|
+
this.outputData = outputData;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Creates a Cell instance from a CellLike object.
|
|
361
|
+
*
|
|
362
|
+
* @param cell - A CellLike object or an instance of Cell.
|
|
363
|
+
* @returns A Cell instance.
|
|
364
|
+
*/
|
|
365
|
+
static from(cell) {
|
|
366
|
+
if (cell instanceof Cell) {
|
|
367
|
+
return cell;
|
|
368
|
+
}
|
|
369
|
+
return new Cell(
|
|
370
|
+
OutPoint.from(cell.outPoint),
|
|
371
|
+
CellOutput.from(cell.cellOutput),
|
|
372
|
+
(0, hex_1.hexFrom)(cell.outputData),
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Clone a Cell
|
|
377
|
+
*
|
|
378
|
+
* @returns A cloned Cell instance.
|
|
379
|
+
*
|
|
380
|
+
* @example
|
|
381
|
+
* ```typescript
|
|
382
|
+
* const cell1 = cell0.clone();
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
clone() {
|
|
386
|
+
return new Cell(
|
|
387
|
+
this.outPoint.clone(),
|
|
388
|
+
this.cellOutput.clone(),
|
|
389
|
+
this.outputData,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
329
392
|
}
|
|
330
393
|
exports.Cell = Cell;
|
|
331
394
|
class CellInput {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
395
|
+
/**
|
|
396
|
+
* Creates an instance of CellInput.
|
|
397
|
+
*
|
|
398
|
+
* @param previousOutput - The previous outpoint of the cell.
|
|
399
|
+
* @param since - The since value of the cell input.
|
|
400
|
+
* @param cellOutput - The optional cell output associated with the cell input.
|
|
401
|
+
* @param outputData - The optional output data associated with the cell input.
|
|
402
|
+
*/
|
|
403
|
+
constructor(previousOutput, since, cellOutput, outputData) {
|
|
404
|
+
this.previousOutput = previousOutput;
|
|
405
|
+
this.since = since;
|
|
406
|
+
this.cellOutput = cellOutput;
|
|
407
|
+
this.outputData = outputData;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Clone a CellInput.
|
|
411
|
+
*
|
|
412
|
+
* @returns A cloned CellInput instance.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* const cellInput1 = cellInput0.clone();
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
clone() {
|
|
420
|
+
return new CellInput(
|
|
421
|
+
this.previousOutput.clone(),
|
|
422
|
+
this.since,
|
|
423
|
+
this.cellOutput?.clone(),
|
|
424
|
+
this.outputData,
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Creates a CellInput instance from a CellInputLike object.
|
|
429
|
+
*
|
|
430
|
+
* @param cellInput - A CellInputLike object or an instance of CellInput.
|
|
431
|
+
* @returns A CellInput instance.
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* const cellInput = CellInput.from({
|
|
436
|
+
* previousOutput: { txHash: "0x...", index: 0 },
|
|
437
|
+
* since: 0n
|
|
438
|
+
* });
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
static from(cellInput) {
|
|
442
|
+
if (cellInput instanceof CellInput) {
|
|
443
|
+
return cellInput;
|
|
444
|
+
}
|
|
445
|
+
return new CellInput(
|
|
446
|
+
OutPoint.from(cellInput.previousOutput),
|
|
447
|
+
(0, num_1.numFrom)(cellInput.since ?? 0),
|
|
448
|
+
(0, utils_1.apply)(CellOutput.from, cellInput.cellOutput),
|
|
449
|
+
(0, utils_1.apply)(hex_1.hexFrom, cellInput.outputData),
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Complete extra infos in the input. Like the output of the out point.
|
|
454
|
+
* The instance will be modified.
|
|
455
|
+
*
|
|
456
|
+
* @returns true if succeed.
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* await cellInput.completeExtraInfos();
|
|
460
|
+
* ```
|
|
461
|
+
*/
|
|
462
|
+
async completeExtraInfos(client) {
|
|
463
|
+
if (this.cellOutput && this.outputData) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const cell = await client.getCell(this.previousOutput);
|
|
467
|
+
if (!cell) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
this.cellOutput = cell.cellOutput;
|
|
471
|
+
this.outputData = cell.outputData;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Converts the CellInput instance to molecule data format.
|
|
475
|
+
*
|
|
476
|
+
* @returns An object representing the cell input in molecule data format.
|
|
477
|
+
*/
|
|
478
|
+
_toMolData() {
|
|
479
|
+
return {
|
|
480
|
+
previousOutput: this.previousOutput._toMolData(),
|
|
481
|
+
since: (0, num_1.numToBytes)(this.since, 8),
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Converts the CellInput instance to bytes.
|
|
486
|
+
*
|
|
487
|
+
* @returns A Uint8Array containing the cell input bytes.
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```typescript
|
|
491
|
+
* const cellInputBytes = cellInput.toBytes();
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
494
|
+
toBytes() {
|
|
495
|
+
return (0, bytes_1.bytesFrom)(mol.SerializeCellInput(this._toMolData()));
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Creates a CellInput instance from a byte-like value or molecule CellInput.
|
|
499
|
+
*
|
|
500
|
+
* @param bytes - The byte-like value or molecule CellInput to convert.
|
|
501
|
+
* @returns A CellInput instance.
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```typescript
|
|
505
|
+
* const cellInput = CellInput.fromBytes(new Uint8Array([/* cell input bytes *\/]));
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
static fromBytes(bytes) {
|
|
509
|
+
const view =
|
|
510
|
+
bytes instanceof mol.CellInput
|
|
511
|
+
? bytes
|
|
512
|
+
: new mol.CellInput((0, bytes_1.bytesFrom)(bytes));
|
|
513
|
+
return new CellInput(
|
|
514
|
+
OutPoint.fromBytes(view.getPreviousOutput()),
|
|
515
|
+
(0, num_1.numFromBytes)(view.getSince().raw()),
|
|
516
|
+
);
|
|
517
|
+
}
|
|
441
518
|
}
|
|
442
519
|
exports.CellInput = CellInput;
|
|
443
520
|
class CellDep {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
521
|
+
/**
|
|
522
|
+
* Creates an instance of CellDep.
|
|
523
|
+
*
|
|
524
|
+
* @param outPoint - The outpoint of the cell dependency.
|
|
525
|
+
* @param depType - The dependency type.
|
|
526
|
+
*/
|
|
527
|
+
constructor(outPoint, depType) {
|
|
528
|
+
this.outPoint = outPoint;
|
|
529
|
+
this.depType = depType;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Clone a CellDep.
|
|
533
|
+
*
|
|
534
|
+
* @returns A cloned CellDep instance.
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* const cellDep1 = cellDep0.clone();
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
541
|
+
clone() {
|
|
542
|
+
return new CellDep(this.outPoint.clone(), this.depType);
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Creates a CellDep instance from a CellDepLike object.
|
|
546
|
+
*
|
|
547
|
+
* @param cellDep - A CellDepLike object or an instance of CellDep.
|
|
548
|
+
* @returns A CellDep instance.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```typescript
|
|
552
|
+
* const cellDep = CellDep.from({
|
|
553
|
+
* outPoint: { txHash: "0x...", index: 0 },
|
|
554
|
+
* depType: "depGroup"
|
|
555
|
+
* });
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
static from(cellDep) {
|
|
559
|
+
if (cellDep instanceof CellDep) {
|
|
560
|
+
return cellDep;
|
|
561
|
+
}
|
|
562
|
+
return new CellDep(
|
|
563
|
+
OutPoint.from(cellDep.outPoint),
|
|
564
|
+
depTypeFrom(cellDep.depType),
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Converts the CellDep instance to molecule data format.
|
|
569
|
+
*
|
|
570
|
+
* @returns An object representing the cell dependency in molecule data format.
|
|
571
|
+
*/
|
|
572
|
+
_toMolData() {
|
|
573
|
+
return {
|
|
574
|
+
outPoint: this.outPoint._toMolData(),
|
|
575
|
+
depType: depTypeToBytes(this.depType),
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Converts the CellDep instance to bytes.
|
|
580
|
+
*
|
|
581
|
+
* @returns A Uint8Array containing the cell dependency bytes.
|
|
582
|
+
*
|
|
583
|
+
* @example
|
|
584
|
+
* ```typescript
|
|
585
|
+
* const cellDepBytes = cellDep.toBytes();
|
|
586
|
+
* ```
|
|
587
|
+
*/
|
|
588
|
+
toBytes() {
|
|
589
|
+
return (0, bytes_1.bytesFrom)(mol.SerializeCellDep(this._toMolData()));
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Creates a CellDep instance from a byte-like value or molecule CellDep.
|
|
593
|
+
*
|
|
594
|
+
* @param bytes - The byte-like value or molecule CellDep to convert.
|
|
595
|
+
* @returns A CellDep instance.
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```typescript
|
|
599
|
+
* const cellDep = CellDep.fromBytes(new Uint8Array([/* cell dep bytes *\/]));
|
|
600
|
+
* ```
|
|
601
|
+
*/
|
|
602
|
+
fromBytes(bytes) {
|
|
603
|
+
const view =
|
|
604
|
+
bytes instanceof mol.CellDep
|
|
605
|
+
? bytes
|
|
606
|
+
: new mol.CellDep((0, bytes_1.bytesFrom)(bytes));
|
|
607
|
+
return new CellDep(
|
|
608
|
+
OutPoint.fromBytes(view.getOutPoint()),
|
|
609
|
+
depTypeFromBytes([view.getDepType()]),
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Compares the current CellDep instance with another CellDepLike object for equality.
|
|
614
|
+
*
|
|
615
|
+
* @param val - The CellDepLike object to compare with.
|
|
616
|
+
* @returns True if the cell deps are equal, otherwise false.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```typescript
|
|
620
|
+
* const isEqual = cellDep.eq(anotherCellDep);
|
|
621
|
+
* ```
|
|
622
|
+
*/
|
|
623
|
+
eq(val) {
|
|
624
|
+
const cellDep = CellDep.from(val);
|
|
625
|
+
return (
|
|
626
|
+
this.outPoint.eq(cellDep.outPoint) && this.depType === cellDep.depType
|
|
627
|
+
);
|
|
628
|
+
}
|
|
541
629
|
}
|
|
542
630
|
exports.CellDep = CellDep;
|
|
543
631
|
class WitnessArgs {
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
632
|
+
/**
|
|
633
|
+
* Creates an instance of WitnessArgs.
|
|
634
|
+
*
|
|
635
|
+
* @param lock - The optional lock field of the witness.
|
|
636
|
+
* @param inputType - The optional input type field of the witness.
|
|
637
|
+
* @param outputType - The optional output type field of the witness.
|
|
638
|
+
*/
|
|
639
|
+
constructor(lock, inputType, outputType) {
|
|
640
|
+
this.lock = lock;
|
|
641
|
+
this.inputType = inputType;
|
|
642
|
+
this.outputType = outputType;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Creates a WitnessArgs instance from a WitnessArgsLike object.
|
|
646
|
+
*
|
|
647
|
+
* @param witnessArgs - A WitnessArgsLike object or an instance of WitnessArgs.
|
|
648
|
+
* @returns A WitnessArgs instance.
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```typescript
|
|
652
|
+
* const witnessArgs = WitnessArgs.from({
|
|
653
|
+
* lock: "0x...",
|
|
654
|
+
* inputType: "0x...",
|
|
655
|
+
* outputType: "0x..."
|
|
656
|
+
* });
|
|
657
|
+
* ```
|
|
658
|
+
*/
|
|
659
|
+
static from(witnessArgs) {
|
|
660
|
+
if (witnessArgs instanceof WitnessArgs) {
|
|
661
|
+
return witnessArgs;
|
|
662
|
+
}
|
|
663
|
+
return new WitnessArgs(
|
|
664
|
+
(0, utils_1.apply)(hex_1.hexFrom, witnessArgs.lock),
|
|
665
|
+
(0, utils_1.apply)(hex_1.hexFrom, witnessArgs.inputType),
|
|
666
|
+
(0, utils_1.apply)(hex_1.hexFrom, witnessArgs.outputType),
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Converts the WitnessArgs instance to molecule data format.
|
|
671
|
+
*
|
|
672
|
+
* @returns An object representing the witness arguments in molecule data format.
|
|
673
|
+
*/
|
|
674
|
+
_toMolData() {
|
|
675
|
+
return {
|
|
676
|
+
lock: (0, utils_1.apply)(bytes_1.bytesFrom, this.lock),
|
|
677
|
+
inputType: (0, utils_1.apply)(bytes_1.bytesFrom, this.inputType),
|
|
678
|
+
outputType: (0, utils_1.apply)(bytes_1.bytesFrom, this.outputType),
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Converts the WitnessArgs instance to bytes.
|
|
683
|
+
*
|
|
684
|
+
* @returns A Uint8Array containing the witness arguments bytes.
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```typescript
|
|
688
|
+
* const witnessArgsBytes = witnessArgs.toBytes();
|
|
689
|
+
* ```
|
|
690
|
+
*/
|
|
691
|
+
toBytes() {
|
|
692
|
+
return (0, bytes_1.bytesFrom)(mol.SerializeWitnessArgs(this._toMolData()));
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Creates a WitnessArgs instance from a byte-like value or molecule WitnessArgs.
|
|
696
|
+
*
|
|
697
|
+
* @param bytes - The byte-like value or molecule WitnessArgs to convert.
|
|
698
|
+
* @returns A WitnessArgs instance.
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* ```typescript
|
|
702
|
+
* const witnessArgs = WitnessArgs.fromBytes(new Uint8Array([/* witness args bytes *\/]));
|
|
703
|
+
* ```
|
|
704
|
+
*/
|
|
705
|
+
static fromBytes(bytes) {
|
|
706
|
+
const view =
|
|
707
|
+
bytes instanceof mol.WitnessArgs
|
|
708
|
+
? bytes
|
|
709
|
+
: new mol.WitnessArgs((0, bytes_1.bytesFrom)(bytes));
|
|
710
|
+
return new WitnessArgs(
|
|
711
|
+
(0, utils_1.apply)(hex_1.hexFrom, mol.molOptional(view.getLock())?.raw()),
|
|
712
|
+
(0, utils_1.apply)(
|
|
713
|
+
hex_1.hexFrom,
|
|
714
|
+
mol.molOptional(view.getInputType())?.raw(),
|
|
715
|
+
),
|
|
716
|
+
(0, utils_1.apply)(
|
|
717
|
+
hex_1.hexFrom,
|
|
718
|
+
mol.molOptional(view.getOutputType())?.raw(),
|
|
719
|
+
),
|
|
720
|
+
);
|
|
721
|
+
}
|
|
619
722
|
}
|
|
620
723
|
exports.WitnessArgs = WitnessArgs;
|
|
621
724
|
function udtBalanceFrom(dataLike) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
725
|
+
const data = (0, bytes_1.bytesFrom)(dataLike).slice(0, 16);
|
|
726
|
+
if (data.length !== 16) {
|
|
727
|
+
throw new Error("Invalid UDT cell data");
|
|
728
|
+
}
|
|
729
|
+
return (0, num_1.numFromBytes)(data);
|
|
627
730
|
}
|
|
628
731
|
exports.udtBalanceFrom = udtBalanceFrom;
|
|
629
732
|
class Transaction {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
* @param skeleton - The Lumos transaction skeleton.
|
|
739
|
-
* @returns A Transaction instance.
|
|
740
|
-
*
|
|
741
|
-
* @throws Will throw an error if an input's outPoint is missing.
|
|
742
|
-
*
|
|
743
|
-
* @example
|
|
744
|
-
* ```typescript
|
|
745
|
-
* const transaction = Transaction.fromLumosSkeleton(skeleton);
|
|
746
|
-
* ```
|
|
747
|
-
*/
|
|
748
|
-
static fromLumosSkeleton(skeleton) {
|
|
749
|
-
return Transaction.from({
|
|
750
|
-
version: 0n,
|
|
751
|
-
cellDeps: skeleton.cellDeps.toArray(),
|
|
752
|
-
headerDeps: skeleton.headerDeps.toArray(),
|
|
753
|
-
inputs: skeleton.inputs.toArray().map((input, i) => {
|
|
754
|
-
if (!input.outPoint) {
|
|
755
|
-
throw new Error("outPoint is required in input");
|
|
756
|
-
}
|
|
757
|
-
return CellInput.from({
|
|
758
|
-
previousOutput: input.outPoint,
|
|
759
|
-
since: skeleton.inputSinces.get(i, "0x0"),
|
|
760
|
-
cellOutput: input.cellOutput,
|
|
761
|
-
outputData: input.data,
|
|
762
|
-
});
|
|
763
|
-
}),
|
|
764
|
-
outputs: skeleton.outputs.toArray().map((output) => output.cellOutput),
|
|
765
|
-
outputsData: skeleton.outputs.toArray().map((output) => output.data),
|
|
766
|
-
witnesses: skeleton.witnesses.toArray(),
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
stringify() {
|
|
770
|
-
return JSON.stringify(this, (_, value) => {
|
|
771
|
-
if (typeof value === "bigint") {
|
|
772
|
-
return (0, num_1.numToHex)(value);
|
|
773
|
-
}
|
|
774
|
-
return value;
|
|
733
|
+
/**
|
|
734
|
+
* Creates an instance of Transaction.
|
|
735
|
+
*
|
|
736
|
+
* @param version - The version of the transaction.
|
|
737
|
+
* @param cellDeps - The cell dependencies of the transaction.
|
|
738
|
+
* @param headerDeps - The header dependencies of the transaction.
|
|
739
|
+
* @param inputs - The inputs of the transaction.
|
|
740
|
+
* @param outputs - The outputs of the transaction.
|
|
741
|
+
* @param outputsData - The data associated with the outputs.
|
|
742
|
+
* @param witnesses - The witnesses of the transaction.
|
|
743
|
+
*/
|
|
744
|
+
constructor(
|
|
745
|
+
version,
|
|
746
|
+
cellDeps,
|
|
747
|
+
headerDeps,
|
|
748
|
+
inputs,
|
|
749
|
+
outputs,
|
|
750
|
+
outputsData,
|
|
751
|
+
witnesses,
|
|
752
|
+
) {
|
|
753
|
+
this.version = version;
|
|
754
|
+
this.cellDeps = cellDeps;
|
|
755
|
+
this.headerDeps = headerDeps;
|
|
756
|
+
this.inputs = inputs;
|
|
757
|
+
this.outputs = outputs;
|
|
758
|
+
this.outputsData = outputsData;
|
|
759
|
+
this.witnesses = witnesses;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Creates a default Transaction instance with empty fields.
|
|
763
|
+
*
|
|
764
|
+
* @returns A default Transaction instance.
|
|
765
|
+
*
|
|
766
|
+
* @example
|
|
767
|
+
* ```typescript
|
|
768
|
+
* const defaultTx = Transaction.default();
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
static default() {
|
|
772
|
+
return new Transaction(0n, [], [], [], [], [], []);
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Copy every properties from another transaction.
|
|
776
|
+
*
|
|
777
|
+
* @example
|
|
778
|
+
* ```typescript
|
|
779
|
+
* this.copy(Transaction.default());
|
|
780
|
+
* ```
|
|
781
|
+
*/
|
|
782
|
+
copy(txLike) {
|
|
783
|
+
const tx = Transaction.from(txLike);
|
|
784
|
+
this.version = tx.version;
|
|
785
|
+
this.cellDeps = tx.cellDeps;
|
|
786
|
+
this.headerDeps = tx.headerDeps;
|
|
787
|
+
this.inputs = tx.inputs;
|
|
788
|
+
this.outputs = tx.outputs;
|
|
789
|
+
this.outputsData = tx.outputsData;
|
|
790
|
+
this.witnesses = tx.witnesses;
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Clone a Transaction.
|
|
794
|
+
*
|
|
795
|
+
* @returns A cloned instance
|
|
796
|
+
*
|
|
797
|
+
* @example
|
|
798
|
+
* ```typescript
|
|
799
|
+
* const tx1 = tx0.clone();
|
|
800
|
+
* ```
|
|
801
|
+
*/
|
|
802
|
+
clone() {
|
|
803
|
+
return new Transaction(
|
|
804
|
+
0n,
|
|
805
|
+
this.cellDeps.map((c) => c.clone()),
|
|
806
|
+
[...this.headerDeps],
|
|
807
|
+
this.inputs.map((i) => i.clone()),
|
|
808
|
+
this.outputs.map((o) => o.clone()),
|
|
809
|
+
[...this.outputsData],
|
|
810
|
+
[...this.witnesses],
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Creates a Transaction instance from a TransactionLike object.
|
|
815
|
+
*
|
|
816
|
+
* @param tx - A TransactionLike object or an instance of Transaction.
|
|
817
|
+
* @returns A Transaction instance.
|
|
818
|
+
*
|
|
819
|
+
* @example
|
|
820
|
+
* ```typescript
|
|
821
|
+
* const transaction = Transaction.from({
|
|
822
|
+
* version: 0,
|
|
823
|
+
* cellDeps: [],
|
|
824
|
+
* headerDeps: [],
|
|
825
|
+
* inputs: [],
|
|
826
|
+
* outputs: [],
|
|
827
|
+
* outputsData: [],
|
|
828
|
+
* witnesses: []
|
|
829
|
+
* });
|
|
830
|
+
* ```
|
|
831
|
+
*/
|
|
832
|
+
static from(tx) {
|
|
833
|
+
if (tx instanceof Transaction) {
|
|
834
|
+
return tx;
|
|
835
|
+
}
|
|
836
|
+
const outputs =
|
|
837
|
+
tx.outputs?.map((output, i) => {
|
|
838
|
+
const o = CellOutput.from({
|
|
839
|
+
...output,
|
|
840
|
+
capacity: output.capacity ?? 0,
|
|
775
841
|
});
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
* @example
|
|
783
|
-
* ```typescript
|
|
784
|
-
* const rawTxBytes = transaction.rawToBytes();
|
|
785
|
-
* ```
|
|
786
|
-
*/
|
|
787
|
-
rawToBytes() {
|
|
788
|
-
return (0, bytes_1.bytesFrom)(mol.SerializeRawTransaction({
|
|
789
|
-
version: (0, num_1.numToBytes)(this.version, 4),
|
|
790
|
-
cellDeps: this.cellDeps.map((d) => d._toMolData()),
|
|
791
|
-
headerDeps: this.headerDeps.map((header) => (0, bytes_1.bytesFrom)(header)),
|
|
792
|
-
inputs: this.inputs.map((i) => i._toMolData()),
|
|
793
|
-
outputs: this.outputs.map((o) => o._toMolData()),
|
|
794
|
-
outputsData: this.outputsData.map((header) => (0, bytes_1.bytesFrom)(header)),
|
|
795
|
-
}));
|
|
796
|
-
}
|
|
797
|
-
/**
|
|
798
|
-
* Converts the whole transaction data to bytes.
|
|
799
|
-
*
|
|
800
|
-
* @returns A Uint8Array containing the full transaction bytes.
|
|
801
|
-
*
|
|
802
|
-
* @example
|
|
803
|
-
* ```typescript
|
|
804
|
-
* const txBytes = transaction.toBytes();
|
|
805
|
-
* ```
|
|
806
|
-
*/
|
|
807
|
-
toBytes() {
|
|
808
|
-
return (0, bytes_1.bytesFrom)(mol.SerializeTransaction({
|
|
809
|
-
raw: {
|
|
810
|
-
version: (0, num_1.numToBytes)(this.version, 4),
|
|
811
|
-
cellDeps: this.cellDeps.map((d) => d._toMolData()),
|
|
812
|
-
headerDeps: this.headerDeps.map((header) => (0, bytes_1.bytesFrom)(header)),
|
|
813
|
-
inputs: this.inputs.map((i) => i._toMolData()),
|
|
814
|
-
outputs: this.outputs.map((o) => o._toMolData()),
|
|
815
|
-
outputsData: this.outputsData.map((header) => (0, bytes_1.bytesFrom)(header)),
|
|
816
|
-
},
|
|
817
|
-
witnesses: this.witnesses.map((witness) => (0, bytes_1.bytesFrom)(witness)),
|
|
818
|
-
}));
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Calculates the hash of the transaction.
|
|
822
|
-
*
|
|
823
|
-
* @returns The hash of the transaction.
|
|
824
|
-
*
|
|
825
|
-
* @example
|
|
826
|
-
* ```typescript
|
|
827
|
-
* const txHash = transaction.hash();
|
|
828
|
-
* ```
|
|
829
|
-
*/
|
|
830
|
-
hash() {
|
|
831
|
-
return (0, hasher_1.hashCkb)(this.rawToBytes());
|
|
832
|
-
}
|
|
833
|
-
/**
|
|
834
|
-
* Hashes a witness and updates the hasher.
|
|
835
|
-
*
|
|
836
|
-
* @param witness - The witness to hash.
|
|
837
|
-
* @param hasher - The hasher instance to update.
|
|
838
|
-
*
|
|
839
|
-
* @example
|
|
840
|
-
* ```typescript
|
|
841
|
-
* Transaction.hashWitnessToHasher("0x...", hasher);
|
|
842
|
-
* ```
|
|
843
|
-
*/
|
|
844
|
-
static hashWitnessToHasher(witness, hasher) {
|
|
845
|
-
const raw = (0, bytes_1.bytesFrom)((0, hex_1.hexFrom)(witness));
|
|
846
|
-
hasher.update((0, num_1.numToBytes)(raw.length, 8));
|
|
847
|
-
hasher.update(raw);
|
|
848
|
-
}
|
|
849
|
-
/**
|
|
850
|
-
* Computes the signing hash information for a given script.
|
|
851
|
-
*
|
|
852
|
-
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
853
|
-
* @param client - The client for complete extra infos in the transaction.
|
|
854
|
-
* @returns A promise that resolves to an object containing the signing message and the witness position,
|
|
855
|
-
* or undefined if no matching input is found.
|
|
856
|
-
*
|
|
857
|
-
* @example
|
|
858
|
-
* ```typescript
|
|
859
|
-
* const signHashInfo = await tx.getSignHashInfo(scriptLike, client);
|
|
860
|
-
* if (signHashInfo) {
|
|
861
|
-
* console.log(signHashInfo.message); // Outputs the signing message
|
|
862
|
-
* console.log(signHashInfo.position); // Outputs the witness position
|
|
863
|
-
* }
|
|
864
|
-
* ```
|
|
865
|
-
*/
|
|
866
|
-
async getSignHashInfo(scriptLike, client) {
|
|
867
|
-
const script = script_1.Script.from(scriptLike);
|
|
868
|
-
let position = -1;
|
|
869
|
-
const hasher = new hasher_1.Hasher();
|
|
870
|
-
hasher.update(this.hash());
|
|
871
|
-
for (let i = 0; i < this.witnesses.length; i += 1) {
|
|
872
|
-
const input = this.inputs[i];
|
|
873
|
-
if (input) {
|
|
874
|
-
await input.completeExtraInfos(client);
|
|
875
|
-
if (!input.cellOutput) {
|
|
876
|
-
throw new Error("Unable to complete input");
|
|
877
|
-
}
|
|
878
|
-
if (!script.eq(input.cellOutput.lock)) {
|
|
879
|
-
continue;
|
|
880
|
-
}
|
|
881
|
-
if (position === -1) {
|
|
882
|
-
position = i;
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
if (position === -1) {
|
|
886
|
-
return undefined;
|
|
887
|
-
}
|
|
888
|
-
Transaction.hashWitnessToHasher(this.witnesses[i], hasher);
|
|
842
|
+
if (o.capacity === fixedPoint_1.Zero) {
|
|
843
|
+
o.capacity = (0, fixedPoint_1.fixedPointFrom)(
|
|
844
|
+
o.occupiedSize +
|
|
845
|
+
((0, utils_1.apply)(bytes_1.bytesFrom, tx.outputsData?.[i])
|
|
846
|
+
?.length ?? 0),
|
|
847
|
+
);
|
|
889
848
|
}
|
|
890
|
-
|
|
891
|
-
|
|
849
|
+
return o;
|
|
850
|
+
}) ?? [];
|
|
851
|
+
const outputsData = outputs.map((_, i) =>
|
|
852
|
+
(0, hex_1.hexFrom)(tx.outputsData?.[i] ?? "0x"),
|
|
853
|
+
);
|
|
854
|
+
if (tx.outputsData != null && outputsData.length < tx.outputsData.length) {
|
|
855
|
+
outputsData.push(
|
|
856
|
+
...tx.outputsData
|
|
857
|
+
.slice(outputsData.length)
|
|
858
|
+
.map((d) => (0, hex_1.hexFrom)(d)),
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
return new Transaction(
|
|
862
|
+
(0, num_1.numFrom)(tx.version ?? 0),
|
|
863
|
+
tx.cellDeps?.map((cellDep) => CellDep.from(cellDep)) ?? [],
|
|
864
|
+
tx.headerDeps?.map(hex_1.hexFrom) ?? [],
|
|
865
|
+
tx.inputs?.map((input) => CellInput.from(input)) ?? [],
|
|
866
|
+
outputs,
|
|
867
|
+
outputsData,
|
|
868
|
+
tx.witnesses?.map(hex_1.hexFrom) ?? [],
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Creates a Transaction instance from a Lumos skeleton.
|
|
873
|
+
*
|
|
874
|
+
* @param skeleton - The Lumos transaction skeleton.
|
|
875
|
+
* @returns A Transaction instance.
|
|
876
|
+
*
|
|
877
|
+
* @throws Will throw an error if an input's outPoint is missing.
|
|
878
|
+
*
|
|
879
|
+
* @example
|
|
880
|
+
* ```typescript
|
|
881
|
+
* const transaction = Transaction.fromLumosSkeleton(skeleton);
|
|
882
|
+
* ```
|
|
883
|
+
*/
|
|
884
|
+
static fromLumosSkeleton(skeleton) {
|
|
885
|
+
return Transaction.from({
|
|
886
|
+
version: 0n,
|
|
887
|
+
cellDeps: skeleton.cellDeps.toArray(),
|
|
888
|
+
headerDeps: skeleton.headerDeps.toArray(),
|
|
889
|
+
inputs: skeleton.inputs.toArray().map((input, i) => {
|
|
890
|
+
if (!input.outPoint) {
|
|
891
|
+
throw new Error("outPoint is required in input");
|
|
892
892
|
}
|
|
893
|
-
return {
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
893
|
+
return CellInput.from({
|
|
894
|
+
previousOutput: input.outPoint,
|
|
895
|
+
since: skeleton.inputSinces.get(i, "0x0"),
|
|
896
|
+
cellOutput: input.cellOutput,
|
|
897
|
+
outputData: input.data,
|
|
898
|
+
});
|
|
899
|
+
}),
|
|
900
|
+
outputs: skeleton.outputs.toArray().map((output) => output.cellOutput),
|
|
901
|
+
outputsData: skeleton.outputs.toArray().map((output) => output.data),
|
|
902
|
+
witnesses: skeleton.witnesses.toArray(),
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
stringify() {
|
|
906
|
+
return JSON.stringify(this, (_, value) => {
|
|
907
|
+
if (typeof value === "bigint") {
|
|
908
|
+
return (0, num_1.numToHex)(value);
|
|
909
|
+
}
|
|
910
|
+
return value;
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Converts the raw transaction data to bytes.
|
|
915
|
+
*
|
|
916
|
+
* @returns A Uint8Array containing the raw transaction bytes.
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* ```typescript
|
|
920
|
+
* const rawTxBytes = transaction.rawToBytes();
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
923
|
+
rawToBytes() {
|
|
924
|
+
return (0, bytes_1.bytesFrom)(
|
|
925
|
+
mol.SerializeRawTransaction({
|
|
926
|
+
version: (0, num_1.numToBytes)(this.version, 4),
|
|
927
|
+
cellDeps: this.cellDeps.map((d) => d._toMolData()),
|
|
928
|
+
headerDeps: this.headerDeps.map((header) =>
|
|
929
|
+
(0, bytes_1.bytesFrom)(header),
|
|
930
|
+
),
|
|
931
|
+
inputs: this.inputs.map((i) => i._toMolData()),
|
|
932
|
+
outputs: this.outputs.map((o) => o._toMolData()),
|
|
933
|
+
outputsData: this.outputsData.map((header) =>
|
|
934
|
+
(0, bytes_1.bytesFrom)(header),
|
|
935
|
+
),
|
|
936
|
+
}),
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Converts the whole transaction data to bytes.
|
|
941
|
+
*
|
|
942
|
+
* @returns A Uint8Array containing the full transaction bytes.
|
|
943
|
+
*
|
|
944
|
+
* @example
|
|
945
|
+
* ```typescript
|
|
946
|
+
* const txBytes = transaction.toBytes();
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
toBytes() {
|
|
950
|
+
return (0, bytes_1.bytesFrom)(
|
|
951
|
+
mol.SerializeTransaction({
|
|
952
|
+
raw: {
|
|
953
|
+
version: (0, num_1.numToBytes)(this.version, 4),
|
|
954
|
+
cellDeps: this.cellDeps.map((d) => d._toMolData()),
|
|
955
|
+
headerDeps: this.headerDeps.map((header) =>
|
|
956
|
+
(0, bytes_1.bytesFrom)(header),
|
|
957
|
+
),
|
|
958
|
+
inputs: this.inputs.map((i) => i._toMolData()),
|
|
959
|
+
outputs: this.outputs.map((o) => o._toMolData()),
|
|
960
|
+
outputsData: this.outputsData.map((header) =>
|
|
961
|
+
(0, bytes_1.bytesFrom)(header),
|
|
962
|
+
),
|
|
963
|
+
},
|
|
964
|
+
witnesses: this.witnesses.map((witness) =>
|
|
965
|
+
(0, bytes_1.bytesFrom)(witness),
|
|
966
|
+
),
|
|
967
|
+
}),
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Calculates the hash of the transaction.
|
|
972
|
+
*
|
|
973
|
+
* @returns The hash of the transaction.
|
|
974
|
+
*
|
|
975
|
+
* @example
|
|
976
|
+
* ```typescript
|
|
977
|
+
* const txHash = transaction.hash();
|
|
978
|
+
* ```
|
|
979
|
+
*/
|
|
980
|
+
hash() {
|
|
981
|
+
return (0, hasher_1.hashCkb)(this.rawToBytes());
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Hashes a witness and updates the hasher.
|
|
985
|
+
*
|
|
986
|
+
* @param witness - The witness to hash.
|
|
987
|
+
* @param hasher - The hasher instance to update.
|
|
988
|
+
*
|
|
989
|
+
* @example
|
|
990
|
+
* ```typescript
|
|
991
|
+
* Transaction.hashWitnessToHasher("0x...", hasher);
|
|
992
|
+
* ```
|
|
993
|
+
*/
|
|
994
|
+
static hashWitnessToHasher(witness, hasher) {
|
|
995
|
+
const raw = (0, bytes_1.bytesFrom)((0, hex_1.hexFrom)(witness));
|
|
996
|
+
hasher.update((0, num_1.numToBytes)(raw.length, 8));
|
|
997
|
+
hasher.update(raw);
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Computes the signing hash information for a given script.
|
|
1001
|
+
*
|
|
1002
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1003
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
1004
|
+
* @returns A promise that resolves to an object containing the signing message and the witness position,
|
|
1005
|
+
* or undefined if no matching input is found.
|
|
1006
|
+
*
|
|
1007
|
+
* @example
|
|
1008
|
+
* ```typescript
|
|
1009
|
+
* const signHashInfo = await tx.getSignHashInfo(scriptLike, client);
|
|
1010
|
+
* if (signHashInfo) {
|
|
1011
|
+
* console.log(signHashInfo.message); // Outputs the signing message
|
|
1012
|
+
* console.log(signHashInfo.position); // Outputs the witness position
|
|
1013
|
+
* }
|
|
1014
|
+
* ```
|
|
1015
|
+
*/
|
|
1016
|
+
async getSignHashInfo(scriptLike, client) {
|
|
1017
|
+
const script = script_1.Script.from(scriptLike);
|
|
1018
|
+
let position = -1;
|
|
1019
|
+
const hasher = new hasher_1.Hasher();
|
|
1020
|
+
hasher.update(this.hash());
|
|
1021
|
+
for (let i = 0; i < this.witnesses.length; i += 1) {
|
|
1022
|
+
const input = this.inputs[i];
|
|
1023
|
+
if (input) {
|
|
1024
|
+
await input.completeExtraInfos(client);
|
|
1025
|
+
if (!input.cellOutput) {
|
|
1026
|
+
throw new Error("Unable to complete input");
|
|
921
1027
|
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
* Find the last occurrence of a input with the specified lock
|
|
925
|
-
*
|
|
926
|
-
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
927
|
-
* @param client - The client for complete extra infos in the transaction.
|
|
928
|
-
* @returns A promise that resolves to the prepared transaction
|
|
929
|
-
*
|
|
930
|
-
* @example
|
|
931
|
-
* ```typescript
|
|
932
|
-
* const index = await tx.findLastInputIndexByLock(scriptLike, client);
|
|
933
|
-
* ```
|
|
934
|
-
*/
|
|
935
|
-
async findLastInputIndexByLock(scriptLike, client) {
|
|
936
|
-
const script = script_1.Script.from(scriptLike);
|
|
937
|
-
for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
|
|
938
|
-
const input = this.inputs[i];
|
|
939
|
-
await input.completeExtraInfos(client);
|
|
940
|
-
if (!input.cellOutput) {
|
|
941
|
-
throw new Error("Unable to complete input");
|
|
942
|
-
}
|
|
943
|
-
if (script.eq(input.cellOutput.lock)) {
|
|
944
|
-
return i;
|
|
945
|
-
}
|
|
1028
|
+
if (!script.eq(input.cellOutput.lock)) {
|
|
1029
|
+
continue;
|
|
946
1030
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
* Add cell deps if they are not existed
|
|
950
|
-
*
|
|
951
|
-
* @param cellDepLikes - The cell deps to add
|
|
952
|
-
*
|
|
953
|
-
* @example
|
|
954
|
-
* ```typescript
|
|
955
|
-
* tx.addCellDeps(cellDep);
|
|
956
|
-
* ```
|
|
957
|
-
*/
|
|
958
|
-
addCellDeps(...cellDepsLike) {
|
|
959
|
-
cellDepsLike.flat().forEach((cellDepLike) => {
|
|
960
|
-
const cellDep = CellDep.from(cellDepLike);
|
|
961
|
-
if (this.cellDeps.some((c) => c.eq(cellDep))) {
|
|
962
|
-
return;
|
|
963
|
-
}
|
|
964
|
-
this.cellDeps.push(cellDep);
|
|
965
|
-
});
|
|
966
|
-
}
|
|
967
|
-
/**
|
|
968
|
-
* Add cell dep from infos if they are not existed
|
|
969
|
-
*
|
|
970
|
-
* @param client - A client for searching cell deps
|
|
971
|
-
* @param cellDepLikes - The cell dep infos to add
|
|
972
|
-
*
|
|
973
|
-
* @example
|
|
974
|
-
* ```typescript
|
|
975
|
-
* tx.addCellDepInfos(client, cellDepInfos);
|
|
976
|
-
* ```
|
|
977
|
-
*/
|
|
978
|
-
async addCellDepInfos(client, ...cellDepInfosLike) {
|
|
979
|
-
this.addCellDeps(await client.getCellDeps(...cellDepInfosLike));
|
|
980
|
-
}
|
|
981
|
-
/**
|
|
982
|
-
* Add cell deps from known script
|
|
983
|
-
*
|
|
984
|
-
* @param client - The client for searching known script and cell deps
|
|
985
|
-
* @param scripts - The known scripts to add
|
|
986
|
-
*
|
|
987
|
-
* @example
|
|
988
|
-
* ```typescript
|
|
989
|
-
* tx.addCellDepsOfKnownScripts(client, KnownScript.OmniLock);
|
|
990
|
-
* ```
|
|
991
|
-
*/
|
|
992
|
-
async addCellDepsOfKnownScripts(client, ...scripts) {
|
|
993
|
-
await Promise.all(scripts
|
|
994
|
-
.flat()
|
|
995
|
-
.map(async (script) => this.addCellDepInfos(client, (await client.getKnownScript(script)).cellDeps)));
|
|
996
|
-
}
|
|
997
|
-
/**
|
|
998
|
-
* Set output data at index.
|
|
999
|
-
*
|
|
1000
|
-
* @param index - The index of the output data.
|
|
1001
|
-
* @param witness - The data to set.
|
|
1002
|
-
*
|
|
1003
|
-
* @example
|
|
1004
|
-
* ```typescript
|
|
1005
|
-
* await tx.setOutputData(0, "0x00");
|
|
1006
|
-
* ```
|
|
1007
|
-
*/
|
|
1008
|
-
setOutputDataAt(index, data) {
|
|
1009
|
-
if (this.outputsData.length < index) {
|
|
1010
|
-
this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
|
|
1031
|
+
if (position === -1) {
|
|
1032
|
+
position = i;
|
|
1011
1033
|
}
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1034
|
+
}
|
|
1035
|
+
if (position === -1) {
|
|
1036
|
+
return undefined;
|
|
1037
|
+
}
|
|
1038
|
+
Transaction.hashWitnessToHasher(this.witnesses[i], hasher);
|
|
1039
|
+
}
|
|
1040
|
+
if (position === -1) {
|
|
1041
|
+
return undefined;
|
|
1042
|
+
}
|
|
1043
|
+
return {
|
|
1044
|
+
message: hasher.digest(),
|
|
1045
|
+
position,
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Find the first occurrence of a input with the specified lock
|
|
1050
|
+
*
|
|
1051
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1052
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
1053
|
+
* @returns A promise that resolves to the prepared transaction
|
|
1054
|
+
*
|
|
1055
|
+
* @example
|
|
1056
|
+
* ```typescript
|
|
1057
|
+
* const index = await tx.findInputIndexByLock(scriptLike, client);
|
|
1058
|
+
* ```
|
|
1059
|
+
*/
|
|
1060
|
+
async findInputIndexByLock(scriptLike, client) {
|
|
1061
|
+
const script = script_1.Script.from(scriptLike);
|
|
1062
|
+
for (let i = 0; i < this.inputs.length; i += 1) {
|
|
1063
|
+
const input = this.inputs[i];
|
|
1064
|
+
await input.completeExtraInfos(client);
|
|
1065
|
+
if (!input.cellOutput) {
|
|
1066
|
+
throw new Error("Unable to complete input");
|
|
1067
|
+
}
|
|
1068
|
+
if (script.eq(input.cellOutput.lock)) {
|
|
1069
|
+
return i;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Find the last occurrence of a input with the specified lock
|
|
1075
|
+
*
|
|
1076
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1077
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
1078
|
+
* @returns A promise that resolves to the prepared transaction
|
|
1079
|
+
*
|
|
1080
|
+
* @example
|
|
1081
|
+
* ```typescript
|
|
1082
|
+
* const index = await tx.findLastInputIndexByLock(scriptLike, client);
|
|
1083
|
+
* ```
|
|
1084
|
+
*/
|
|
1085
|
+
async findLastInputIndexByLock(scriptLike, client) {
|
|
1086
|
+
const script = script_1.Script.from(scriptLike);
|
|
1087
|
+
for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
|
|
1088
|
+
const input = this.inputs[i];
|
|
1089
|
+
await input.completeExtraInfos(client);
|
|
1090
|
+
if (!input.cellOutput) {
|
|
1091
|
+
throw new Error("Unable to complete input");
|
|
1092
|
+
}
|
|
1093
|
+
if (script.eq(input.cellOutput.lock)) {
|
|
1094
|
+
return i;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Add cell deps if they are not existed
|
|
1100
|
+
*
|
|
1101
|
+
* @param cellDepLikes - The cell deps to add
|
|
1102
|
+
*
|
|
1103
|
+
* @example
|
|
1104
|
+
* ```typescript
|
|
1105
|
+
* tx.addCellDeps(cellDep);
|
|
1106
|
+
* ```
|
|
1107
|
+
*/
|
|
1108
|
+
addCellDeps(...cellDepsLike) {
|
|
1109
|
+
cellDepsLike.flat().forEach((cellDepLike) => {
|
|
1110
|
+
const cellDep = CellDep.from(cellDepLike);
|
|
1111
|
+
if (this.cellDeps.some((c) => c.eq(cellDep))) {
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
this.cellDeps.push(cellDep);
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Add cell dep from infos if they are not existed
|
|
1119
|
+
*
|
|
1120
|
+
* @param client - A client for searching cell deps
|
|
1121
|
+
* @param cellDepLikes - The cell dep infos to add
|
|
1122
|
+
*
|
|
1123
|
+
* @example
|
|
1124
|
+
* ```typescript
|
|
1125
|
+
* tx.addCellDepInfos(client, cellDepInfos);
|
|
1126
|
+
* ```
|
|
1127
|
+
*/
|
|
1128
|
+
async addCellDepInfos(client, ...cellDepInfosLike) {
|
|
1129
|
+
this.addCellDeps(await client.getCellDeps(...cellDepInfosLike));
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Add cell deps from known script
|
|
1133
|
+
*
|
|
1134
|
+
* @param client - The client for searching known script and cell deps
|
|
1135
|
+
* @param scripts - The known scripts to add
|
|
1136
|
+
*
|
|
1137
|
+
* @example
|
|
1138
|
+
* ```typescript
|
|
1139
|
+
* tx.addCellDepsOfKnownScripts(client, KnownScript.OmniLock);
|
|
1140
|
+
* ```
|
|
1141
|
+
*/
|
|
1142
|
+
async addCellDepsOfKnownScripts(client, ...scripts) {
|
|
1143
|
+
await Promise.all(
|
|
1144
|
+
scripts
|
|
1145
|
+
.flat()
|
|
1146
|
+
.map(async (script) =>
|
|
1147
|
+
this.addCellDepInfos(
|
|
1148
|
+
client,
|
|
1149
|
+
(await client.getKnownScript(script)).cellDeps,
|
|
1150
|
+
),
|
|
1151
|
+
),
|
|
1152
|
+
);
|
|
1153
|
+
}
|
|
1154
|
+
/**
|
|
1155
|
+
* Set output data at index.
|
|
1156
|
+
*
|
|
1157
|
+
* @param index - The index of the output data.
|
|
1158
|
+
* @param witness - The data to set.
|
|
1159
|
+
*
|
|
1160
|
+
* @example
|
|
1161
|
+
* ```typescript
|
|
1162
|
+
* await tx.setOutputData(0, "0x00");
|
|
1163
|
+
* ```
|
|
1164
|
+
*/
|
|
1165
|
+
setOutputDataAt(index, data) {
|
|
1166
|
+
if (this.outputsData.length < index) {
|
|
1167
|
+
this.outputsData.push(
|
|
1168
|
+
...Array.from(new Array(index - this.outputsData.length), () => "0x"),
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
1171
|
+
this.outputsData[index] = (0, hex_1.hexFrom)(data);
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Add output
|
|
1175
|
+
*
|
|
1176
|
+
* @param output - The cell output to add
|
|
1177
|
+
* @param data - optional output data
|
|
1178
|
+
*
|
|
1179
|
+
* @example
|
|
1180
|
+
* ```typescript
|
|
1181
|
+
* await tx.addOutput(cellOutput, "0xabcd");
|
|
1182
|
+
* ```
|
|
1183
|
+
*/
|
|
1184
|
+
addOutput(outputLike, outputData = "0x") {
|
|
1185
|
+
const output = CellOutput.from({
|
|
1186
|
+
...outputLike,
|
|
1187
|
+
capacity: outputLike.capacity ?? 0,
|
|
1188
|
+
});
|
|
1189
|
+
if (output.capacity === fixedPoint_1.Zero) {
|
|
1190
|
+
output.capacity = (0, fixedPoint_1.fixedPointFrom)(
|
|
1191
|
+
output.occupiedSize + (0, bytes_1.bytesFrom)(outputData).length,
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
const i = this.outputs.push(output) - 1;
|
|
1195
|
+
this.setOutputDataAt(i, outputData);
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Get witness at index as WitnessArgs
|
|
1199
|
+
*
|
|
1200
|
+
* @param index - The index of the witness.
|
|
1201
|
+
* @returns The witness parsed as WitnessArgs.
|
|
1202
|
+
*
|
|
1203
|
+
* @example
|
|
1204
|
+
* ```typescript
|
|
1205
|
+
* const witnessArgs = await tx.getWitnessArgsAt(0);
|
|
1206
|
+
* ```
|
|
1207
|
+
*/
|
|
1208
|
+
getWitnessArgsAt(index) {
|
|
1209
|
+
const rawWitness = this.witnesses[index];
|
|
1210
|
+
return (rawWitness ?? "0x") !== "0x"
|
|
1211
|
+
? WitnessArgs.fromBytes(rawWitness)
|
|
1212
|
+
: undefined;
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* Set witness at index by WitnessArgs
|
|
1216
|
+
*
|
|
1217
|
+
* @param index - The index of the witness.
|
|
1218
|
+
* @param witness - The WitnessArgs to set.
|
|
1219
|
+
*
|
|
1220
|
+
* @example
|
|
1221
|
+
* ```typescript
|
|
1222
|
+
* await tx.setWitnessArgsAt(0, witnessArgs);
|
|
1223
|
+
* ```
|
|
1224
|
+
*/
|
|
1225
|
+
setWitnessArgsAt(index, witness) {
|
|
1226
|
+
if (this.witnesses.length < index) {
|
|
1227
|
+
this.witnesses.push(
|
|
1228
|
+
...Array.from(new Array(index - this.witnesses.length), () => "0x"),
|
|
1229
|
+
);
|
|
1230
|
+
}
|
|
1231
|
+
this.witnesses[index] = (0, hex_1.hexFrom)(witness.toBytes());
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Prepare dummy witness for sighash all method
|
|
1235
|
+
*
|
|
1236
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1237
|
+
* @param lockLen - The length of dummy lock bytes.
|
|
1238
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
1239
|
+
* @returns A promise that resolves to the prepared transaction
|
|
1240
|
+
*
|
|
1241
|
+
* @example
|
|
1242
|
+
* ```typescript
|
|
1243
|
+
* await tx.prepareSighashAllWitness(scriptLike, 85, client);
|
|
1244
|
+
* ```
|
|
1245
|
+
*/
|
|
1246
|
+
async prepareSighashAllWitness(scriptLike, lockLen, client) {
|
|
1247
|
+
const position = await this.findInputIndexByLock(scriptLike, client);
|
|
1248
|
+
if (position === undefined) {
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
|
|
1252
|
+
witness.lock = (0, hex_1.hexFrom)(Array.from(new Array(lockLen), () => 0));
|
|
1253
|
+
this.setWitnessArgsAt(position, witness);
|
|
1254
|
+
}
|
|
1255
|
+
async getInputsCapacity(client) {
|
|
1256
|
+
return (0, utils_1.reduceAsync)(
|
|
1257
|
+
this.inputs,
|
|
1258
|
+
async (acc, input) => {
|
|
1259
|
+
await input.completeExtraInfos(client);
|
|
1260
|
+
if (!input.cellOutput) {
|
|
1261
|
+
throw new Error("Unable to complete input");
|
|
1032
1262
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
: undefined;
|
|
1052
|
-
}
|
|
1053
|
-
/**
|
|
1054
|
-
* Set witness at index by WitnessArgs
|
|
1055
|
-
*
|
|
1056
|
-
* @param index - The index of the witness.
|
|
1057
|
-
* @param witness - The WitnessArgs to set.
|
|
1058
|
-
*
|
|
1059
|
-
* @example
|
|
1060
|
-
* ```typescript
|
|
1061
|
-
* await tx.setWitnessArgsAt(0, witnessArgs);
|
|
1062
|
-
* ```
|
|
1063
|
-
*/
|
|
1064
|
-
setWitnessArgsAt(index, witness) {
|
|
1065
|
-
if (this.witnesses.length < index) {
|
|
1066
|
-
this.witnesses.push(...Array.from(new Array(index - this.witnesses.length), () => "0x"));
|
|
1263
|
+
return acc + input.cellOutput.capacity;
|
|
1264
|
+
},
|
|
1265
|
+
(0, num_1.numFrom)(0),
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
getOutputsCapacity() {
|
|
1269
|
+
return this.outputs.reduce(
|
|
1270
|
+
(acc, { capacity }) => acc + capacity,
|
|
1271
|
+
(0, num_1.numFrom)(0),
|
|
1272
|
+
);
|
|
1273
|
+
}
|
|
1274
|
+
async getInputsUdtBalance(client, type) {
|
|
1275
|
+
return (0, utils_1.reduceAsync)(
|
|
1276
|
+
this.inputs,
|
|
1277
|
+
async (acc, input) => {
|
|
1278
|
+
await input.completeExtraInfos(client);
|
|
1279
|
+
if (!input.cellOutput || !input.outputData) {
|
|
1280
|
+
throw new Error("Unable to complete input");
|
|
1067
1281
|
}
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
/**
|
|
1071
|
-
* Prepare dummy witness for sighash all method
|
|
1072
|
-
*
|
|
1073
|
-
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1074
|
-
* @param lockLen - The length of dummy lock bytes.
|
|
1075
|
-
* @param client - The client for complete extra infos in the transaction.
|
|
1076
|
-
* @returns A promise that resolves to the prepared transaction
|
|
1077
|
-
*
|
|
1078
|
-
* @example
|
|
1079
|
-
* ```typescript
|
|
1080
|
-
* await tx.prepareSighashAllWitness(scriptLike, 85, client);
|
|
1081
|
-
* ```
|
|
1082
|
-
*/
|
|
1083
|
-
async prepareSighashAllWitness(scriptLike, lockLen, client) {
|
|
1084
|
-
const position = await this.findInputIndexByLock(scriptLike, client);
|
|
1085
|
-
if (position === undefined) {
|
|
1086
|
-
return;
|
|
1282
|
+
if (!input.cellOutput.type?.eq(type)) {
|
|
1283
|
+
return;
|
|
1087
1284
|
}
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
return acc + input.cellOutput.capacity;
|
|
1099
|
-
}, (0, num_1.numFrom)(0));
|
|
1100
|
-
}
|
|
1101
|
-
getOutputsCapacity() {
|
|
1102
|
-
return this.outputs.reduce((acc, { capacity }) => acc + capacity, (0, num_1.numFrom)(0));
|
|
1103
|
-
}
|
|
1104
|
-
async getInputsUdtBalance(client, type) {
|
|
1105
|
-
return (0, utils_1.reduceAsync)(this.inputs, async (acc, input) => {
|
|
1106
|
-
await input.completeExtraInfos(client);
|
|
1107
|
-
if (!input.cellOutput || !input.outputData) {
|
|
1108
|
-
throw new Error("Unable to complete input");
|
|
1109
|
-
}
|
|
1110
|
-
if (!input.cellOutput.type?.eq(type)) {
|
|
1111
|
-
return;
|
|
1112
|
-
}
|
|
1113
|
-
return acc + udtBalanceFrom(input.outputData);
|
|
1114
|
-
}, (0, num_1.numFrom)(0));
|
|
1115
|
-
}
|
|
1116
|
-
getOutputsUdtBalance(type) {
|
|
1117
|
-
return this.outputs.reduce((acc, output, i) => {
|
|
1118
|
-
if (!output.type?.eq(type)) {
|
|
1119
|
-
return acc;
|
|
1120
|
-
}
|
|
1121
|
-
return acc + udtBalanceFrom(this.outputsData[i]);
|
|
1122
|
-
}, (0, num_1.numFrom)(0));
|
|
1123
|
-
}
|
|
1124
|
-
async completeInputs(from, filter, accumulator, init) {
|
|
1125
|
-
const scripts = (await from.getAddressObjs()).map(({ script }) => script);
|
|
1126
|
-
const collectedCells = [];
|
|
1127
|
-
let acc = init;
|
|
1128
|
-
for (const script of scripts) {
|
|
1129
|
-
for await (const cell of from.client.findCellsByCollectableSearchKey({
|
|
1130
|
-
script,
|
|
1131
|
-
scriptType: "lock",
|
|
1132
|
-
filter,
|
|
1133
|
-
scriptSearchMode: "exact",
|
|
1134
|
-
withData: true,
|
|
1135
|
-
})) {
|
|
1136
|
-
if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
|
|
1137
|
-
continue;
|
|
1138
|
-
}
|
|
1139
|
-
const i = collectedCells.push(cell);
|
|
1140
|
-
const next = await Promise.resolve(accumulator(acc, cell, i - 1, collectedCells));
|
|
1141
|
-
if (next === undefined) {
|
|
1142
|
-
this.inputs.push(...collectedCells.map(({ outPoint, outputData, cellOutput }) => CellInput.from({
|
|
1143
|
-
previousOutput: outPoint,
|
|
1144
|
-
since: 0,
|
|
1145
|
-
outputData,
|
|
1146
|
-
cellOutput,
|
|
1147
|
-
})));
|
|
1148
|
-
return collectedCells.length;
|
|
1149
|
-
}
|
|
1150
|
-
acc = next;
|
|
1151
|
-
}
|
|
1285
|
+
return acc + udtBalanceFrom(input.outputData);
|
|
1286
|
+
},
|
|
1287
|
+
(0, num_1.numFrom)(0),
|
|
1288
|
+
);
|
|
1289
|
+
}
|
|
1290
|
+
getOutputsUdtBalance(type) {
|
|
1291
|
+
return this.outputs.reduce(
|
|
1292
|
+
(acc, output, i) => {
|
|
1293
|
+
if (!output.type?.eq(type)) {
|
|
1294
|
+
return acc;
|
|
1152
1295
|
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1296
|
+
return acc + udtBalanceFrom(this.outputsData[i]);
|
|
1297
|
+
},
|
|
1298
|
+
(0, num_1.numFrom)(0),
|
|
1299
|
+
);
|
|
1300
|
+
}
|
|
1301
|
+
async completeInputs(from, filter, accumulator, init) {
|
|
1302
|
+
const scripts = (await from.getAddressObjs()).map(({ script }) => script);
|
|
1303
|
+
const collectedCells = [];
|
|
1304
|
+
let acc = init;
|
|
1305
|
+
for (const script of scripts) {
|
|
1306
|
+
for await (const cell of from.client.findCellsByCollectableSearchKey({
|
|
1307
|
+
script,
|
|
1308
|
+
scriptType: "lock",
|
|
1309
|
+
filter,
|
|
1310
|
+
scriptSearchMode: "exact",
|
|
1311
|
+
withData: true,
|
|
1312
|
+
})) {
|
|
1313
|
+
if (
|
|
1314
|
+
this.inputs.some(({ previousOutput }) =>
|
|
1315
|
+
previousOutput.eq(cell.outPoint),
|
|
1316
|
+
)
|
|
1317
|
+
) {
|
|
1318
|
+
continue;
|
|
1160
1319
|
}
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1320
|
+
const i = collectedCells.push(cell);
|
|
1321
|
+
const next = await Promise.resolve(
|
|
1322
|
+
accumulator(acc, cell, i - 1, collectedCells),
|
|
1323
|
+
);
|
|
1324
|
+
if (next === undefined) {
|
|
1325
|
+
this.inputs.push(
|
|
1326
|
+
...collectedCells.map(({ outPoint, outputData, cellOutput }) =>
|
|
1327
|
+
CellInput.from({
|
|
1328
|
+
previousOutput: outPoint,
|
|
1329
|
+
since: 0,
|
|
1330
|
+
outputData,
|
|
1331
|
+
cellOutput,
|
|
1332
|
+
}),
|
|
1333
|
+
),
|
|
1334
|
+
);
|
|
1335
|
+
return {
|
|
1336
|
+
addedCount: collectedCells.length,
|
|
1337
|
+
};
|
|
1174
1338
|
}
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1339
|
+
acc = next;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
return {
|
|
1343
|
+
addedCount: collectedCells.length,
|
|
1344
|
+
accumulated: acc,
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
async completeInputsByCapacity(from, capacityTweak, filter) {
|
|
1348
|
+
const exceptedCapacity =
|
|
1349
|
+
this.getOutputsCapacity() + (0, num_1.numFrom)(capacityTweak ?? 0);
|
|
1350
|
+
const inputsCapacity = await this.getInputsCapacity(from.client);
|
|
1351
|
+
if (inputsCapacity >= exceptedCapacity) {
|
|
1352
|
+
return 0;
|
|
1353
|
+
}
|
|
1354
|
+
const { addedCount, accumulated } = await this.completeInputs(
|
|
1355
|
+
from,
|
|
1356
|
+
filter ?? {
|
|
1357
|
+
scriptLenRange: [0, 1],
|
|
1358
|
+
outputDataLenRange: [0, 1],
|
|
1359
|
+
},
|
|
1360
|
+
(acc, { cellOutput: { capacity } }) => {
|
|
1361
|
+
const sum = acc + capacity;
|
|
1362
|
+
return sum >= exceptedCapacity ? undefined : sum;
|
|
1363
|
+
},
|
|
1364
|
+
inputsCapacity,
|
|
1365
|
+
);
|
|
1366
|
+
if (accumulated === undefined) {
|
|
1367
|
+
return addedCount;
|
|
1368
|
+
}
|
|
1369
|
+
throw new Error(
|
|
1370
|
+
`Insufficient CKB, need ${(0, fixedPoint_1.fixedPointToString)(exceptedCapacity - accumulated)} extra CKB`,
|
|
1371
|
+
);
|
|
1372
|
+
}
|
|
1373
|
+
async completeInputsAll(from, filter) {
|
|
1374
|
+
const { addedCount } = await this.completeInputs(
|
|
1375
|
+
from,
|
|
1376
|
+
filter ?? {
|
|
1377
|
+
scriptLenRange: [0, 1],
|
|
1378
|
+
outputDataLenRange: [0, 1],
|
|
1379
|
+
},
|
|
1380
|
+
(acc, { cellOutput: { capacity } }) => acc + capacity,
|
|
1381
|
+
fixedPoint_1.Zero,
|
|
1382
|
+
);
|
|
1383
|
+
return addedCount;
|
|
1384
|
+
}
|
|
1385
|
+
async completeInputsByUdt(from, type) {
|
|
1386
|
+
const exceptedBalance = this.getOutputsUdtBalance(type);
|
|
1387
|
+
const inputsBalance = await this.getInputsUdtBalance(from.client, type);
|
|
1388
|
+
if (inputsBalance >= exceptedBalance) {
|
|
1389
|
+
return 0;
|
|
1390
|
+
}
|
|
1391
|
+
const { addedCount, accumulated } = await this.completeInputs(
|
|
1392
|
+
from,
|
|
1393
|
+
{
|
|
1394
|
+
script: type,
|
|
1395
|
+
outputDataLenRange: [16, (0, num_1.numFrom)("0xffffffff")],
|
|
1396
|
+
},
|
|
1397
|
+
(acc, { outputData }) => {
|
|
1398
|
+
const balance = udtBalanceFrom(outputData);
|
|
1399
|
+
const sum = acc + balance;
|
|
1400
|
+
return sum >= exceptedBalance ? undefined : sum;
|
|
1401
|
+
},
|
|
1402
|
+
inputsBalance,
|
|
1403
|
+
);
|
|
1404
|
+
if (accumulated === undefined) {
|
|
1405
|
+
return addedCount;
|
|
1406
|
+
}
|
|
1407
|
+
throw new Error(
|
|
1408
|
+
`Insufficient coin, need ${exceptedBalance - accumulated} extra coin`,
|
|
1409
|
+
);
|
|
1410
|
+
}
|
|
1411
|
+
estimateFee(feeRate) {
|
|
1412
|
+
const txSize = this.toBytes().length + 4;
|
|
1413
|
+
return (
|
|
1414
|
+
((0, num_1.numFrom)(txSize) * (0, num_1.numFrom)(feeRate) +
|
|
1415
|
+
(0, num_1.numFrom)(1000)) /
|
|
1416
|
+
(0, num_1.numFrom)(1000)
|
|
1417
|
+
);
|
|
1418
|
+
}
|
|
1419
|
+
async completeFee(from, change, feeRate, filter) {
|
|
1420
|
+
// Complete all inputs extra infos for cache
|
|
1421
|
+
await this.getInputsCapacity(from.client);
|
|
1422
|
+
let leastFee = fixedPoint_1.Zero;
|
|
1423
|
+
let leastExtraCapacity = fixedPoint_1.Zero;
|
|
1424
|
+
while (true) {
|
|
1425
|
+
const prepared = await from.prepareTransaction(this.clone());
|
|
1426
|
+
const collected = await (async () => {
|
|
1427
|
+
try {
|
|
1428
|
+
return await prepared.completeInputsByCapacity(
|
|
1429
|
+
from,
|
|
1430
|
+
leastFee + leastExtraCapacity,
|
|
1431
|
+
filter,
|
|
1432
|
+
);
|
|
1433
|
+
} catch (err) {
|
|
1434
|
+
if (leastExtraCapacity !== fixedPoint_1.Zero) {
|
|
1435
|
+
throw new Error("Not enough capacity for the change cell");
|
|
1436
|
+
}
|
|
1437
|
+
throw err;
|
|
1228
1438
|
}
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1439
|
+
})();
|
|
1440
|
+
if (leastFee === fixedPoint_1.Zero) {
|
|
1441
|
+
// The initial fee is calculated based on prepared transaction
|
|
1442
|
+
leastFee = prepared.estimateFee(feeRate);
|
|
1443
|
+
}
|
|
1444
|
+
const extraCapacity =
|
|
1445
|
+
(await prepared.getInputsCapacity(from.client)) -
|
|
1446
|
+
prepared.getOutputsCapacity();
|
|
1447
|
+
// The extra capacity paid the fee without a change
|
|
1448
|
+
if (extraCapacity === leastFee) {
|
|
1449
|
+
this.copy(prepared);
|
|
1450
|
+
return [collected, false];
|
|
1451
|
+
}
|
|
1452
|
+
let changed = prepared.clone();
|
|
1453
|
+
const needed = (0, num_1.numFrom)(
|
|
1454
|
+
await Promise.resolve(change(changed, extraCapacity - leastFee)),
|
|
1455
|
+
);
|
|
1456
|
+
// No enough extra capacity to create new cells for change
|
|
1457
|
+
if (needed > fixedPoint_1.Zero) {
|
|
1458
|
+
leastExtraCapacity = needed;
|
|
1459
|
+
continue;
|
|
1460
|
+
}
|
|
1461
|
+
if (
|
|
1462
|
+
(await changed.getInputsCapacity(from.client)) -
|
|
1463
|
+
changed.getOutputsCapacity() !==
|
|
1464
|
+
leastFee
|
|
1465
|
+
) {
|
|
1466
|
+
throw new Error(
|
|
1467
|
+
"The change function doesn't use all available capacity",
|
|
1468
|
+
);
|
|
1469
|
+
}
|
|
1470
|
+
// New change cells created, update the fee
|
|
1471
|
+
await from.prepareTransaction(changed);
|
|
1472
|
+
const changedFee = changed.estimateFee(feeRate);
|
|
1473
|
+
if (leastFee > changedFee) {
|
|
1474
|
+
throw new Error("The change function removed existed transaction data");
|
|
1475
|
+
}
|
|
1476
|
+
// The fee has been paid
|
|
1477
|
+
if (leastFee === changedFee) {
|
|
1478
|
+
this.copy(changed);
|
|
1479
|
+
return [collected, true];
|
|
1480
|
+
}
|
|
1481
|
+
// The fee after changing is more than the original fee
|
|
1482
|
+
leastFee = changedFee;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
completeFeeChangeToLock(from, change, feeRate, filter) {
|
|
1486
|
+
const script = script_1.Script.from(change);
|
|
1487
|
+
return this.completeFee(
|
|
1488
|
+
from,
|
|
1489
|
+
(tx, capacity) => {
|
|
1490
|
+
const changeCell = CellOutput.from({ capacity: 0, lock: script });
|
|
1491
|
+
const occupiedCapacity = (0, fixedPoint_1.fixedPointFrom)(
|
|
1492
|
+
changeCell.occupiedSize,
|
|
1493
|
+
);
|
|
1494
|
+
if (capacity < occupiedCapacity) {
|
|
1495
|
+
return occupiedCapacity;
|
|
1251
1496
|
}
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1497
|
+
changeCell.capacity = capacity;
|
|
1498
|
+
tx.addOutput(changeCell);
|
|
1499
|
+
return 0;
|
|
1500
|
+
},
|
|
1501
|
+
feeRate,
|
|
1502
|
+
filter,
|
|
1503
|
+
);
|
|
1504
|
+
}
|
|
1505
|
+
async completeFeeBy(from, feeRate, filter) {
|
|
1506
|
+
const { script } = await from.getRecommendedAddressObj();
|
|
1507
|
+
return this.completeFeeChangeToLock(from, script, feeRate, filter);
|
|
1508
|
+
}
|
|
1509
|
+
completeFeeChangeToOutput(from, index, feeRate, filter) {
|
|
1510
|
+
const change = Number((0, num_1.numFrom)(index));
|
|
1511
|
+
if (!this.outputs[change]) {
|
|
1512
|
+
throw new Error("Non-existed output to change");
|
|
1513
|
+
}
|
|
1514
|
+
return this.completeFee(
|
|
1515
|
+
from,
|
|
1516
|
+
(tx, capacity) => {
|
|
1517
|
+
tx.outputs[change].capacity += capacity;
|
|
1518
|
+
return 0;
|
|
1519
|
+
},
|
|
1520
|
+
feeRate,
|
|
1521
|
+
filter,
|
|
1522
|
+
);
|
|
1523
|
+
}
|
|
1257
1524
|
}
|
|
1258
1525
|
exports.Transaction = Transaction;
|