@ckb-ccc/core 0.0.5-alpha.8 → 0.0.7-alpha.0
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/address/address.advanced.d.ts.map +1 -1
- package/dist/address/address.advanced.js +2 -5
- package/dist/address/index.d.ts +1 -1
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +2 -5
- package/dist/ckb/script.d.ts +43 -1
- package/dist/ckb/script.d.ts.map +1 -1
- package/dist/ckb/script.js +51 -0
- package/dist/ckb/transaction.d.ts +215 -27
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +472 -32
- package/dist/client/advanced.d.ts +2 -0
- package/dist/client/advanced.d.ts.map +1 -1
- package/dist/client/advanced.js +2 -0
- package/dist/client/client.advanced.d.ts +10 -0
- package/dist/client/client.advanced.d.ts.map +1 -0
- package/dist/client/client.advanced.js +80 -0
- package/dist/client/client.d.ts +50 -13
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +145 -15
- package/dist/client/clientPublicMainnet.advanced.d.ts +4 -2
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.advanced.js +103 -0
- package/dist/client/clientPublicMainnet.d.ts +4 -2
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.js +9 -1
- package/dist/client/clientPublicTestnet.advanced.d.ts +4 -2
- package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.advanced.js +155 -0
- package/dist/client/clientPublicTestnet.d.ts +4 -2
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.js +6 -1
- package/dist/client/clientTypes.advanced.d.ts +23 -0
- package/dist/client/clientTypes.advanced.d.ts.map +1 -0
- package/dist/client/clientTypes.advanced.js +4 -0
- package/dist/client/clientTypes.d.ts +22 -36
- package/dist/client/clientTypes.d.ts.map +1 -1
- package/dist/client/clientTypes.js +15 -12
- package/dist/client/jsonRpc/index.d.ts +3 -3
- package/dist/client/jsonRpc/index.d.ts.map +1 -1
- package/dist/client/jsonRpc/index.js +3 -3
- package/dist/client/jsonRpc/transformers.d.ts +5 -7
- package/dist/client/jsonRpc/transformers.d.ts.map +1 -1
- package/dist/client/jsonRpc/transformers.js +8 -6
- package/dist/hasher/index.d.ts +1 -1
- package/dist/hasher/index.d.ts.map +1 -1
- package/dist/hasher/index.js +4 -2
- package/dist/num/index.d.ts +12 -0
- package/dist/num/index.d.ts.map +1 -1
- package/dist/num/index.js +21 -0
- package/dist/signer/btc/signerBtc.d.ts.map +1 -1
- package/dist/signer/btc/signerBtc.js +6 -3
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts +0 -6
- package/dist/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist/signer/btc/signerBtcPublicKeyReadonly.js +0 -8
- package/dist/signer/btc/verify.d.ts.map +1 -1
- package/dist/signer/btc/verify.js +5 -2
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +0 -6
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist/signer/ckb/signerCkbScriptReadonly.js +0 -8
- package/dist/signer/dummy/alwaysError.d.ts +0 -1
- package/dist/signer/dummy/alwaysError.d.ts.map +1 -1
- package/dist/signer/dummy/alwaysError.js +0 -3
- package/dist/signer/dummy/openLink.d.ts +0 -1
- package/dist/signer/dummy/openLink.d.ts.map +1 -1
- package/dist/signer/dummy/openLink.js +0 -3
- package/dist/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist/signer/evm/signerEvm.js +7 -5
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts +0 -6
- package/dist/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist/signer/evm/signerEvmAddressReadonly.js +0 -8
- package/dist/signer/index.d.ts +1 -0
- package/dist/signer/index.d.ts.map +1 -1
- package/dist/signer/index.js +1 -0
- package/dist/signer/nostr/index.d.ts +2 -0
- package/dist/signer/nostr/index.d.ts.map +1 -0
- package/dist/signer/nostr/index.js +1 -0
- package/dist/signer/nostr/signerNostr.d.ts +66 -0
- package/dist/signer/nostr/signerNostr.d.ts.map +1 -0
- package/dist/signer/nostr/signerNostr.js +85 -0
- package/dist/signer/nostr/verify.d.ts +5 -0
- package/dist/signer/nostr/verify.d.ts.map +1 -0
- package/dist/signer/nostr/verify.js +48 -0
- package/dist/signer/signer/index.d.ts +7 -12
- package/dist/signer/signer/index.d.ts.map +1 -1
- package/dist/signer/signer/index.js +11 -4
- package/dist/utils/index.d.ts +59 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +17 -0
- package/dist.commonjs/address/address.advanced.d.ts +29 -24
- package/dist.commonjs/address/address.advanced.d.ts.map +1 -1
- package/dist.commonjs/address/address.advanced.js +115 -92
- 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.d.ts.map +1 -1
- package/dist.commonjs/address/index.js +84 -68
- 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 +123 -77
- package/dist.commonjs/ckb/script.d.ts.map +1 -1
- package/dist.commonjs/ckb/script.js +221 -126
- 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 +796 -533
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +1372 -702
- package/dist.commonjs/client/advanced.d.ts +3 -1
- package/dist.commonjs/client/advanced.d.ts.map +1 -1
- package/dist.commonjs/client/advanced.js +32 -14
- package/dist.commonjs/client/client.advanced.d.ts +27 -0
- package/dist.commonjs/client/client.advanced.d.ts.map +1 -0
- package/dist.commonjs/client/client.advanced.js +116 -0
- package/dist.commonjs/client/client.d.ts +118 -21
- package/dist.commonjs/client/client.d.ts.map +1 -1
- package/dist.commonjs/client/client.js +219 -55
- package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +9 -3
- package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicMainnet.advanced.js +144 -24
- package/dist.commonjs/client/clientPublicMainnet.d.ts +9 -5
- package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicMainnet.js +18 -8
- package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +8 -3
- package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicTestnet.advanced.js +204 -24
- package/dist.commonjs/client/clientPublicTestnet.d.ts +9 -5
- package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicTestnet.js +14 -9
- package/dist.commonjs/client/clientTypes.advanced.d.ts +23 -0
- package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -0
- package/dist.commonjs/client/clientTypes.advanced.js +8 -0
- package/dist.commonjs/client/clientTypes.d.ts +57 -46
- package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
- package/dist.commonjs/client/clientTypes.js +60 -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.d.ts.map +1 -1
- package/dist.commonjs/client/jsonRpc/index.js +135 -108
- package/dist.commonjs/client/jsonRpc/transformers.d.ts +76 -38
- package/dist.commonjs/client/jsonRpc/transformers.d.ts.map +1 -1
- package/dist.commonjs/client/jsonRpc/transformers.js +174 -150
- 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 +37 -37
- package/dist.commonjs/hasher/index.d.ts.map +1 -1
- package/dist.commonjs/hasher/index.js +55 -46
- 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 +13 -1
- package/dist.commonjs/num/index.d.ts.map +1 -1
- package/dist.commonjs/num/index.js +53 -19
- 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.d.ts.map +1 -1
- package/dist.commonjs/signer/btc/signerBtc.js +89 -63
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts +45 -51
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +52 -60
- package/dist.commonjs/signer/btc/verify.d.ts +6 -2
- package/dist.commonjs/signer/btc/verify.d.ts.map +1 -1
- package/dist.commonjs/signer/btc/verify.js +13 -3
- package/dist.commonjs/signer/ckb/index.d.ts +1 -1
- package/dist.commonjs/signer/ckb/index.js +30 -14
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +45 -51
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +56 -64
- 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 -5
- package/dist.commonjs/signer/dummy/alwaysError.d.ts.map +1 -1
- package/dist.commonjs/signer/dummy/alwaysError.js +7 -10
- 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 -5
- package/dist.commonjs/signer/dummy/openLink.d.ts.map +1 -1
- package/dist.commonjs/signer/dummy/openLink.js +7 -10
- 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 +42 -38
- package/dist.commonjs/signer/evm/signerEvm.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/signerEvm.js +106 -69
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts +37 -43
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +42 -50
- 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 +2 -1
- package/dist.commonjs/signer/index.d.ts.map +1 -1
- package/dist.commonjs/signer/index.js +31 -14
- package/dist.commonjs/signer/nostr/index.d.ts +2 -0
- package/dist.commonjs/signer/nostr/index.d.ts.map +1 -0
- package/dist.commonjs/signer/nostr/index.js +33 -0
- package/dist.commonjs/signer/nostr/signerNostr.d.ts +66 -0
- package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -0
- package/dist.commonjs/signer/nostr/signerNostr.js +118 -0
- package/dist.commonjs/signer/nostr/verify.d.ts +11 -0
- package/dist.commonjs/signer/nostr/verify.d.ts.map +1 -0
- package/dist.commonjs/signer/nostr/verify.js +64 -0
- package/dist.commonjs/signer/signer/index.d.ts +156 -155
- package/dist.commonjs/signer/signer/index.d.ts.map +1 -1
- package/dist.commonjs/signer/signer/index.js +209 -175
- package/dist.commonjs/utils/index.d.ts +95 -6
- package/dist.commonjs/utils/index.d.ts.map +1 -1
- package/dist.commonjs/utils/index.js +30 -6
- package/package.json +4 -3
- package/src/address/address.advanced.ts +2 -5
- package/src/address/index.ts +2 -5
- package/src/ckb/script.ts +62 -2
- package/src/ckb/transaction.ts +678 -58
- package/src/client/advanced.ts +2 -0
- package/src/client/client.advanced.ts +134 -0
- package/src/client/client.ts +236 -25
- package/src/client/clientPublicMainnet.advanced.ts +117 -2
- package/src/client/clientPublicMainnet.ts +14 -3
- package/src/client/clientPublicTestnet.advanced.ts +176 -2
- package/src/client/clientPublicTestnet.ts +9 -3
- package/src/client/clientTypes.advanced.ts +26 -0
- package/src/client/clientTypes.ts +41 -43
- package/src/client/jsonRpc/index.ts +4 -4
- package/src/client/jsonRpc/transformers.ts +13 -11
- package/src/hasher/index.ts +4 -2
- package/src/num/index.ts +22 -0
- package/src/signer/btc/signerBtc.ts +6 -8
- package/src/signer/btc/signerBtcPublicKeyReadonly.ts +0 -9
- package/src/signer/btc/verify.ts +6 -3
- package/src/signer/ckb/signerCkbScriptReadonly.ts +0 -9
- package/src/signer/dummy/alwaysError.ts +0 -4
- package/src/signer/dummy/openLink.ts +0 -4
- package/src/signer/evm/signerEvm.ts +11 -11
- package/src/signer/evm/signerEvmAddressReadonly.ts +0 -9
- package/src/signer/index.ts +1 -0
- package/src/signer/nostr/index.ts +1 -0
- package/src/signer/nostr/signerNostr.ts +135 -0
- package/src/signer/nostr/verify.ts +63 -0
- package/src/signer/signer/index.ts +15 -11
- package/src/utils/index.ts +108 -0
package/dist/ckb/transaction.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { bytesFrom } from "../bytes";
|
|
2
|
+
import { Zero, fixedPointFrom } from "../fixedPoint";
|
|
2
3
|
import { Hasher, ckbHash } from "../hasher";
|
|
3
4
|
import { hexFrom } from "../hex";
|
|
4
5
|
import { numFrom, numFromBytes, numToBytes, numToHex, } from "../num";
|
|
5
|
-
import { apply } from "../utils";
|
|
6
|
+
import { apply, reduceAsync } from "../utils";
|
|
6
7
|
import * as mol from "./molecule.advanced";
|
|
7
8
|
import { Script } from "./script";
|
|
8
9
|
import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced";
|
|
@@ -76,6 +77,19 @@ export class OutPoint {
|
|
|
76
77
|
this.txHash = txHash;
|
|
77
78
|
this.index = index;
|
|
78
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Clone an OutPoint.
|
|
82
|
+
*
|
|
83
|
+
* @returns A cloned OutPoint instance.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const outPoint1 = outPoint0.clone();
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
clone() {
|
|
91
|
+
return new OutPoint(this.txHash, this.index);
|
|
92
|
+
}
|
|
79
93
|
/**
|
|
80
94
|
* Creates an OutPoint instance from an OutPointLike object.
|
|
81
95
|
*
|
|
@@ -111,10 +125,10 @@ export class OutPoint {
|
|
|
111
125
|
*
|
|
112
126
|
* @example
|
|
113
127
|
* ```typescript
|
|
114
|
-
* const outPointBytes = outPoint.
|
|
128
|
+
* const outPointBytes = outPoint.toBytes();
|
|
115
129
|
* ```
|
|
116
130
|
*/
|
|
117
|
-
|
|
131
|
+
toBytes() {
|
|
118
132
|
return bytesFrom(mol.SerializeOutPoint(this._toMolData()));
|
|
119
133
|
}
|
|
120
134
|
/**
|
|
@@ -134,6 +148,21 @@ export class OutPoint {
|
|
|
134
148
|
: new mol.OutPoint(bytesFrom(bytes));
|
|
135
149
|
return new OutPoint(hexFrom(view.getTxHash().raw()), numFromBytes(view.getIndex().raw()));
|
|
136
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Compares the current OutPoint instance with another OutPointLike object for equality.
|
|
153
|
+
*
|
|
154
|
+
* @param val - The OutPointLike object to compare with.
|
|
155
|
+
* @returns True if the out points are equal, otherwise false.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const isEqual = outPoint.eq(anotherOutPoint);
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
eq(val) {
|
|
163
|
+
const outPoint = OutPoint.from(val);
|
|
164
|
+
return this.txHash === outPoint.txHash && this.index === outPoint.index;
|
|
165
|
+
}
|
|
137
166
|
}
|
|
138
167
|
export class CellOutput {
|
|
139
168
|
/**
|
|
@@ -148,6 +177,22 @@ export class CellOutput {
|
|
|
148
177
|
this.lock = lock;
|
|
149
178
|
this.type = type;
|
|
150
179
|
}
|
|
180
|
+
get occupiedSize() {
|
|
181
|
+
return 8 + this.lock.occupiedSize + (this.type?.occupiedSize ?? 0);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Clone a CellOutput.
|
|
185
|
+
*
|
|
186
|
+
* @returns A cloned CellOutput instance.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const cellOutput1 = cellOutput0.clone();
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
clone() {
|
|
194
|
+
return new CellOutput(this.capacity, this.lock.clone(), this.type?.clone());
|
|
195
|
+
}
|
|
151
196
|
/**
|
|
152
197
|
* Creates a CellOutput instance from a CellOutputLike object.
|
|
153
198
|
*
|
|
@@ -219,13 +264,11 @@ export class Cell {
|
|
|
219
264
|
* @param outPoint - The output point of the cell.
|
|
220
265
|
* @param cellOutput - The cell output of the cell.
|
|
221
266
|
* @param outputData - The output data of the cell.
|
|
222
|
-
* @param blockNumber - The block number of the cell.
|
|
223
267
|
*/
|
|
224
|
-
constructor(outPoint, cellOutput, outputData
|
|
268
|
+
constructor(outPoint, cellOutput, outputData) {
|
|
225
269
|
this.outPoint = outPoint;
|
|
226
270
|
this.cellOutput = cellOutput;
|
|
227
271
|
this.outputData = outputData;
|
|
228
|
-
this.blockNumber = blockNumber;
|
|
229
272
|
}
|
|
230
273
|
/**
|
|
231
274
|
* Creates a Cell instance from a CellLike object.
|
|
@@ -237,7 +280,20 @@ export class Cell {
|
|
|
237
280
|
if (cell instanceof Cell) {
|
|
238
281
|
return cell;
|
|
239
282
|
}
|
|
240
|
-
return new Cell(OutPoint.from(cell.outPoint), CellOutput.from(cell.cellOutput), hexFrom(cell.outputData)
|
|
283
|
+
return new Cell(OutPoint.from(cell.outPoint), CellOutput.from(cell.cellOutput), hexFrom(cell.outputData));
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Clone a Cell
|
|
287
|
+
*
|
|
288
|
+
* @returns A cloned Cell instance.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```typescript
|
|
292
|
+
* const cell1 = cell0.clone();
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
clone() {
|
|
296
|
+
return new Cell(this.outPoint.clone(), this.cellOutput.clone(), this.outputData);
|
|
241
297
|
}
|
|
242
298
|
}
|
|
243
299
|
export class CellInput {
|
|
@@ -248,14 +304,25 @@ export class CellInput {
|
|
|
248
304
|
* @param since - The since value of the cell input.
|
|
249
305
|
* @param cellOutput - The optional cell output associated with the cell input.
|
|
250
306
|
* @param outputData - The optional output data associated with the cell input.
|
|
251
|
-
* @param blockNumber - The optional block number associated with the cell input.
|
|
252
307
|
*/
|
|
253
|
-
constructor(previousOutput, since, cellOutput, outputData
|
|
308
|
+
constructor(previousOutput, since, cellOutput, outputData) {
|
|
254
309
|
this.previousOutput = previousOutput;
|
|
255
310
|
this.since = since;
|
|
256
311
|
this.cellOutput = cellOutput;
|
|
257
312
|
this.outputData = outputData;
|
|
258
|
-
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Clone a CellInput.
|
|
316
|
+
*
|
|
317
|
+
* @returns A cloned CellInput instance.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```typescript
|
|
321
|
+
* const cellInput1 = cellInput0.clone();
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
clone() {
|
|
325
|
+
return new CellInput(this.previousOutput.clone(), this.since, this.cellOutput?.clone(), this.outputData);
|
|
259
326
|
}
|
|
260
327
|
/**
|
|
261
328
|
* Creates a CellInput instance from a CellInputLike object.
|
|
@@ -275,31 +342,28 @@ export class CellInput {
|
|
|
275
342
|
if (cellInput instanceof CellInput) {
|
|
276
343
|
return cellInput;
|
|
277
344
|
}
|
|
278
|
-
return new CellInput(OutPoint.from(cellInput.previousOutput), numFrom(cellInput.since), apply(CellOutput.from, cellInput.cellOutput), apply(hexFrom, cellInput.outputData)
|
|
345
|
+
return new CellInput(OutPoint.from(cellInput.previousOutput), numFrom(cellInput.since ?? 0), apply(CellOutput.from, cellInput.cellOutput), apply(hexFrom, cellInput.outputData));
|
|
279
346
|
}
|
|
280
347
|
/**
|
|
281
348
|
* Complete extra infos in the input. Like the output of the out point.
|
|
282
349
|
* The instance will be modified.
|
|
283
350
|
*
|
|
284
|
-
* @returns
|
|
351
|
+
* @returns true if succeed.
|
|
285
352
|
* @example
|
|
286
353
|
* ```typescript
|
|
287
|
-
*
|
|
288
|
-
* await cellInput.completeExtraInfos();
|
|
289
|
-
* }
|
|
354
|
+
* await cellInput.completeExtraInfos();
|
|
290
355
|
* ```
|
|
291
356
|
*/
|
|
292
357
|
async completeExtraInfos(client) {
|
|
293
|
-
if (this.cellOutput && this.outputData
|
|
294
|
-
return
|
|
358
|
+
if (this.cellOutput && this.outputData) {
|
|
359
|
+
return;
|
|
295
360
|
}
|
|
296
361
|
const cell = await client.getCell(this.previousOutput);
|
|
297
|
-
if (cell) {
|
|
298
|
-
|
|
299
|
-
this.outputData = cell.outputData;
|
|
300
|
-
this.blockNumber = cell.blockNumber;
|
|
362
|
+
if (!cell) {
|
|
363
|
+
return;
|
|
301
364
|
}
|
|
302
|
-
|
|
365
|
+
this.cellOutput = cell.cellOutput;
|
|
366
|
+
this.outputData = cell.outputData;
|
|
303
367
|
}
|
|
304
368
|
/**
|
|
305
369
|
* Converts the CellInput instance to molecule data format.
|
|
@@ -354,6 +418,19 @@ export class CellDep {
|
|
|
354
418
|
this.outPoint = outPoint;
|
|
355
419
|
this.depType = depType;
|
|
356
420
|
}
|
|
421
|
+
/**
|
|
422
|
+
* Clone a CellDep.
|
|
423
|
+
*
|
|
424
|
+
* @returns A cloned CellDep instance.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* ```typescript
|
|
428
|
+
* const cellDep1 = cellDep0.clone();
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
clone() {
|
|
432
|
+
return new CellDep(this.outPoint.clone(), this.depType);
|
|
433
|
+
}
|
|
357
434
|
/**
|
|
358
435
|
* Creates a CellDep instance from a CellDepLike object.
|
|
359
436
|
*
|
|
@@ -413,6 +490,21 @@ export class CellDep {
|
|
|
413
490
|
const view = bytes instanceof mol.CellDep ? bytes : new mol.CellDep(bytesFrom(bytes));
|
|
414
491
|
return new CellDep(OutPoint.fromBytes(view.getOutPoint()), depTypeFromBytes([view.getDepType()]));
|
|
415
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Compares the current CellDep instance with another CellDepLike object for equality.
|
|
495
|
+
*
|
|
496
|
+
* @param val - The CellDepLike object to compare with.
|
|
497
|
+
* @returns True if the cell deps are equal, otherwise false.
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```typescript
|
|
501
|
+
* const isEqual = cellDep.eq(anotherCellDep);
|
|
502
|
+
* ```
|
|
503
|
+
*/
|
|
504
|
+
eq(val) {
|
|
505
|
+
const cellDep = CellDep.from(val);
|
|
506
|
+
return (this.outPoint.eq(cellDep.outPoint) && this.depType === cellDep.depType);
|
|
507
|
+
}
|
|
416
508
|
}
|
|
417
509
|
export class WitnessArgs {
|
|
418
510
|
/**
|
|
@@ -491,6 +583,13 @@ export class WitnessArgs {
|
|
|
491
583
|
return new WitnessArgs(apply(hexFrom, mol.molOptional(view.getLock())?.raw()), apply(hexFrom, mol.molOptional(view.getInputType())?.raw()), apply(hexFrom, mol.molOptional(view.getOutputType())?.raw()));
|
|
492
584
|
}
|
|
493
585
|
}
|
|
586
|
+
export function udtBalanceFrom(dataLike) {
|
|
587
|
+
const data = bytesFrom(dataLike).slice(0, 16);
|
|
588
|
+
if (data.length !== 16) {
|
|
589
|
+
throw new Error("Invalid UDT cell data");
|
|
590
|
+
}
|
|
591
|
+
return numFromBytes(data);
|
|
592
|
+
}
|
|
494
593
|
export class Transaction {
|
|
495
594
|
/**
|
|
496
595
|
* Creates an instance of Transaction.
|
|
@@ -525,6 +624,37 @@ export class Transaction {
|
|
|
525
624
|
static default() {
|
|
526
625
|
return new Transaction(0n, [], [], [], [], [], []);
|
|
527
626
|
}
|
|
627
|
+
/**
|
|
628
|
+
* Copy every properties from another transaction.
|
|
629
|
+
*
|
|
630
|
+
* @example
|
|
631
|
+
* ```typescript
|
|
632
|
+
* this.copy(Transaction.default());
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
copy(txLike) {
|
|
636
|
+
const tx = Transaction.from(txLike);
|
|
637
|
+
this.version = tx.version;
|
|
638
|
+
this.cellDeps = tx.cellDeps;
|
|
639
|
+
this.headerDeps = tx.headerDeps;
|
|
640
|
+
this.inputs = tx.inputs;
|
|
641
|
+
this.outputs = tx.outputs;
|
|
642
|
+
this.outputsData = tx.outputsData;
|
|
643
|
+
this.witnesses = tx.witnesses;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Clone a Transaction.
|
|
647
|
+
*
|
|
648
|
+
* @returns A cloned instance
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```typescript
|
|
652
|
+
* const tx1 = tx0.clone();
|
|
653
|
+
* ```
|
|
654
|
+
*/
|
|
655
|
+
clone() {
|
|
656
|
+
return new Transaction(0n, this.cellDeps.map((c) => c.clone()), [...this.headerDeps], this.inputs.map((i) => i.clone()), this.outputs.map((o) => o.clone()), [...this.outputsData], [...this.witnesses]);
|
|
657
|
+
}
|
|
528
658
|
/**
|
|
529
659
|
* Creates a Transaction instance from a TransactionLike object.
|
|
530
660
|
*
|
|
@@ -548,7 +678,20 @@ export class Transaction {
|
|
|
548
678
|
if (tx instanceof Transaction) {
|
|
549
679
|
return tx;
|
|
550
680
|
}
|
|
551
|
-
|
|
681
|
+
const outputs = tx.outputs?.map((output, i) => {
|
|
682
|
+
const o = CellOutput.from({
|
|
683
|
+
...output,
|
|
684
|
+
capacity: output.capacity ?? 0,
|
|
685
|
+
});
|
|
686
|
+
o.capacity = fixedPointFrom(o.occupiedSize + (apply(bytesFrom, tx.outputsData?.[i])?.length ?? 0));
|
|
687
|
+
return o;
|
|
688
|
+
}) ?? [];
|
|
689
|
+
const outputsData = outputs.map((_, i) => hexFrom(tx.outputsData?.[i] ?? "0x"));
|
|
690
|
+
if (tx.outputsData !== undefined &&
|
|
691
|
+
outputsData.length < tx.outputsData.length) {
|
|
692
|
+
outputsData.push(...tx.outputsData.slice(outputsData.length).map((d) => hexFrom(d)));
|
|
693
|
+
}
|
|
694
|
+
return new Transaction(numFrom(tx.version ?? 0), tx.cellDeps?.map((cellDep) => CellDep.from(cellDep)) ?? [], tx.headerDeps?.map(hexFrom) ?? [], tx.inputs?.map((input) => CellInput.from(input)) ?? [], outputs, outputsData, tx.witnesses?.map(hexFrom) ?? []);
|
|
552
695
|
}
|
|
553
696
|
/**
|
|
554
697
|
* Creates a Transaction instance from a Lumos skeleton.
|
|
@@ -612,6 +755,29 @@ export class Transaction {
|
|
|
612
755
|
outputsData: this.outputsData.map((header) => bytesFrom(header)),
|
|
613
756
|
}));
|
|
614
757
|
}
|
|
758
|
+
/**
|
|
759
|
+
* Converts the whole transaction data to bytes.
|
|
760
|
+
*
|
|
761
|
+
* @returns A Uint8Array containing the full transaction bytes.
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* ```typescript
|
|
765
|
+
* const txBytes = transaction.toBytes();
|
|
766
|
+
* ```
|
|
767
|
+
*/
|
|
768
|
+
toBytes() {
|
|
769
|
+
return bytesFrom(mol.SerializeTransaction({
|
|
770
|
+
raw: {
|
|
771
|
+
version: numToBytes(this.version, 4),
|
|
772
|
+
cellDeps: this.cellDeps.map((d) => d._toMolData()),
|
|
773
|
+
headerDeps: this.headerDeps.map((header) => bytesFrom(header)),
|
|
774
|
+
inputs: this.inputs.map((i) => i._toMolData()),
|
|
775
|
+
outputs: this.outputs.map((o) => o._toMolData()),
|
|
776
|
+
outputsData: this.outputsData.map((header) => bytesFrom(header)),
|
|
777
|
+
},
|
|
778
|
+
witnesses: this.witnesses.map((witness) => bytesFrom(witness)),
|
|
779
|
+
}));
|
|
780
|
+
}
|
|
615
781
|
/**
|
|
616
782
|
* Calculates the hash of the transaction.
|
|
617
783
|
*
|
|
@@ -664,10 +830,11 @@ export class Transaction {
|
|
|
664
830
|
const hasher = new Hasher();
|
|
665
831
|
hasher.update(this.hash());
|
|
666
832
|
for (let i = 0; i < this.witnesses.length; i += 1) {
|
|
667
|
-
|
|
668
|
-
|
|
833
|
+
const input = await this.inputs[i];
|
|
834
|
+
await input.completeExtraInfos(client);
|
|
835
|
+
if (input) {
|
|
669
836
|
if (!input.cellOutput) {
|
|
670
|
-
throw Error("Unable to
|
|
837
|
+
throw new Error("Unable to complete input");
|
|
671
838
|
}
|
|
672
839
|
if (!script.eq(input.cellOutput.lock)) {
|
|
673
840
|
continue;
|
|
@@ -704,15 +871,129 @@ export class Transaction {
|
|
|
704
871
|
async findInputIndexByLock(scriptLike, client) {
|
|
705
872
|
const script = Script.from(scriptLike);
|
|
706
873
|
for (let i = 0; i < this.inputs.length; i += 1) {
|
|
707
|
-
const input =
|
|
874
|
+
const input = this.inputs[i];
|
|
875
|
+
await input.completeExtraInfos(client);
|
|
708
876
|
if (!input.cellOutput) {
|
|
709
|
-
throw Error("Unable to
|
|
877
|
+
throw new Error("Unable to complete input");
|
|
710
878
|
}
|
|
711
879
|
if (script.eq(input.cellOutput.lock)) {
|
|
712
880
|
return i;
|
|
713
881
|
}
|
|
714
882
|
}
|
|
715
883
|
}
|
|
884
|
+
/**
|
|
885
|
+
* Find the last occurrence of a input with the specified lock
|
|
886
|
+
*
|
|
887
|
+
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
888
|
+
* @param client - The client for complete extra infos in the transaction.
|
|
889
|
+
* @returns A promise that resolves to the prepared transaction
|
|
890
|
+
*
|
|
891
|
+
* @example
|
|
892
|
+
* ```typescript
|
|
893
|
+
* const index = await tx.findLastInputIndexByLock(scriptLike, client);
|
|
894
|
+
* ```
|
|
895
|
+
*/
|
|
896
|
+
async findLastInputIndexByLock(scriptLike, client) {
|
|
897
|
+
const script = Script.from(scriptLike);
|
|
898
|
+
for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
|
|
899
|
+
const input = this.inputs[i];
|
|
900
|
+
await input.completeExtraInfos(client);
|
|
901
|
+
if (!input.cellOutput) {
|
|
902
|
+
throw new Error("Unable to complete input");
|
|
903
|
+
}
|
|
904
|
+
if (script.eq(input.cellOutput.lock)) {
|
|
905
|
+
return i;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Add cell deps if they are not existed
|
|
911
|
+
*
|
|
912
|
+
* @param cellDepLikes - The cell deps to add
|
|
913
|
+
*
|
|
914
|
+
* @example
|
|
915
|
+
* ```typescript
|
|
916
|
+
* tx.addCellDeps(cellDep);
|
|
917
|
+
* ```
|
|
918
|
+
*/
|
|
919
|
+
addCellDeps(...cellDepsLike) {
|
|
920
|
+
cellDepsLike.flat().forEach((cellDepLike) => {
|
|
921
|
+
const cellDep = CellDep.from(cellDepLike);
|
|
922
|
+
if (this.cellDeps.some((c) => c.eq(cellDep))) {
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
this.cellDeps.push(cellDep);
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* Add cell dep from infos if they are not existed
|
|
930
|
+
*
|
|
931
|
+
* @param client - A client for searching cell deps
|
|
932
|
+
* @param cellDepLikes - The cell dep infos to add
|
|
933
|
+
*
|
|
934
|
+
* @example
|
|
935
|
+
* ```typescript
|
|
936
|
+
* tx.addCellDepInfos(client, cellDepInfos);
|
|
937
|
+
* ```
|
|
938
|
+
*/
|
|
939
|
+
async addCellDepInfos(client, ...cellDepInfosLike) {
|
|
940
|
+
this.addCellDeps(await client.getCellDeps(...cellDepInfosLike));
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Add cell deps from known script
|
|
944
|
+
*
|
|
945
|
+
* @param client - The client for searching known script and cell deps
|
|
946
|
+
* @param scripts - The known scripts to add
|
|
947
|
+
*
|
|
948
|
+
* @example
|
|
949
|
+
* ```typescript
|
|
950
|
+
* tx.addCellDepsOfKnownScripts(client, KnownScript.OmniLock);
|
|
951
|
+
* ```
|
|
952
|
+
*/
|
|
953
|
+
async addCellDepsOfKnownScripts(client, ...scripts) {
|
|
954
|
+
await Promise.all(scripts
|
|
955
|
+
.flat()
|
|
956
|
+
.map(async (script) => this.addCellDepInfos(client, (await client.getKnownScript(script)).cellDeps)));
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Set output data at index.
|
|
960
|
+
*
|
|
961
|
+
* @param index - The index of the output data.
|
|
962
|
+
* @param witness - The data to set.
|
|
963
|
+
*
|
|
964
|
+
* @example
|
|
965
|
+
* ```typescript
|
|
966
|
+
* await tx.setOutputData(0, "0x00");
|
|
967
|
+
* ```
|
|
968
|
+
*/
|
|
969
|
+
setOutputDataAt(index, data) {
|
|
970
|
+
if (this.outputsData.length < index) {
|
|
971
|
+
this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
|
|
972
|
+
}
|
|
973
|
+
this.outputsData[index] = hexFrom(data);
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Add output
|
|
977
|
+
*
|
|
978
|
+
* @param output - The cell output to add
|
|
979
|
+
* @param data - optional output data
|
|
980
|
+
*
|
|
981
|
+
* @example
|
|
982
|
+
* ```typescript
|
|
983
|
+
* await tx.addOutput(cellOutput, "0xabcd");
|
|
984
|
+
* ```
|
|
985
|
+
*/
|
|
986
|
+
addOutput(outputLike, outputData = "0x") {
|
|
987
|
+
const output = CellOutput.from({
|
|
988
|
+
...outputLike,
|
|
989
|
+
capacity: outputLike.capacity ?? 0,
|
|
990
|
+
});
|
|
991
|
+
if (output.capacity === Zero) {
|
|
992
|
+
output.capacity = fixedPointFrom(output.occupiedSize + bytesFrom(outputData).length);
|
|
993
|
+
}
|
|
994
|
+
const i = this.outputs.push(output) - 1;
|
|
995
|
+
this.setOutputDataAt(i, outputData);
|
|
996
|
+
}
|
|
716
997
|
/**
|
|
717
998
|
* Get witness at index as WitnessArgs
|
|
718
999
|
*
|
|
@@ -735,7 +1016,6 @@ export class Transaction {
|
|
|
735
1016
|
*
|
|
736
1017
|
* @param index - The index of the witness.
|
|
737
1018
|
* @param witness - The WitnessArgs to set.
|
|
738
|
-
* @returns The transaction itself.
|
|
739
1019
|
*
|
|
740
1020
|
* @example
|
|
741
1021
|
* ```typescript
|
|
@@ -747,7 +1027,6 @@ export class Transaction {
|
|
|
747
1027
|
this.witnesses.push(...Array.from(new Array(index - this.witnesses.length), () => "0x"));
|
|
748
1028
|
}
|
|
749
1029
|
this.witnesses[index] = hexFrom(witness.toBytes());
|
|
750
|
-
return this;
|
|
751
1030
|
}
|
|
752
1031
|
/**
|
|
753
1032
|
* Prepare dummy witness for sighash all method
|
|
@@ -765,10 +1044,171 @@ export class Transaction {
|
|
|
765
1044
|
async prepareSighashAllWitness(scriptLike, lockLen, client) {
|
|
766
1045
|
const position = await this.findInputIndexByLock(scriptLike, client);
|
|
767
1046
|
if (position === undefined) {
|
|
768
|
-
return
|
|
1047
|
+
return;
|
|
769
1048
|
}
|
|
770
1049
|
const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
|
|
771
1050
|
witness.lock = hexFrom(Array.from(new Array(lockLen), () => 0));
|
|
772
|
-
|
|
1051
|
+
this.setWitnessArgsAt(position, witness);
|
|
1052
|
+
}
|
|
1053
|
+
async getInputsCapacity(client) {
|
|
1054
|
+
return reduceAsync(this.inputs, async (acc, input) => {
|
|
1055
|
+
await input.completeExtraInfos(client);
|
|
1056
|
+
if (!input.cellOutput) {
|
|
1057
|
+
throw new Error("Unable to complete input");
|
|
1058
|
+
}
|
|
1059
|
+
return acc + input.cellOutput.capacity;
|
|
1060
|
+
}, numFrom(0));
|
|
1061
|
+
}
|
|
1062
|
+
getOutputsCapacity() {
|
|
1063
|
+
return this.outputs.reduce((acc, { capacity }) => acc + capacity, numFrom(0));
|
|
1064
|
+
}
|
|
1065
|
+
async getInputsUdtBalance(client, type) {
|
|
1066
|
+
return reduceAsync(this.inputs, async (acc, input) => {
|
|
1067
|
+
await input.completeExtraInfos(client);
|
|
1068
|
+
if (!input.cellOutput || !input.outputData) {
|
|
1069
|
+
throw new Error("Unable to complete input");
|
|
1070
|
+
}
|
|
1071
|
+
if (!input.cellOutput.type?.eq(type)) {
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
return acc + udtBalanceFrom(input.outputData);
|
|
1075
|
+
}, numFrom(0));
|
|
1076
|
+
}
|
|
1077
|
+
getOutputsUdtBalance(type) {
|
|
1078
|
+
return this.outputs.reduce((acc, output, i) => {
|
|
1079
|
+
if (!output.type?.eq(type)) {
|
|
1080
|
+
return acc;
|
|
1081
|
+
}
|
|
1082
|
+
return acc + udtBalanceFrom(this.outputsData[i]);
|
|
1083
|
+
}, numFrom(0));
|
|
1084
|
+
}
|
|
1085
|
+
async completeInputs(from, filter, accumulator, init) {
|
|
1086
|
+
const scripts = (await from.getAddressObjs()).map(({ script }) => script);
|
|
1087
|
+
const collectedCells = [];
|
|
1088
|
+
let acc = init;
|
|
1089
|
+
for (const script of scripts) {
|
|
1090
|
+
for await (const cell of from.client.findCellsByCollectableSearchKey({
|
|
1091
|
+
script,
|
|
1092
|
+
scriptType: "lock",
|
|
1093
|
+
filter,
|
|
1094
|
+
scriptSearchMode: "exact",
|
|
1095
|
+
withData: true,
|
|
1096
|
+
})) {
|
|
1097
|
+
if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
const i = collectedCells.push(cell);
|
|
1101
|
+
const next = await Promise.resolve(accumulator(acc, cell, i - 1, collectedCells));
|
|
1102
|
+
if (next === undefined) {
|
|
1103
|
+
this.inputs.push(...collectedCells.map(({ outPoint, outputData, cellOutput }) => CellInput.from({
|
|
1104
|
+
previousOutput: outPoint,
|
|
1105
|
+
since: 0,
|
|
1106
|
+
outputData,
|
|
1107
|
+
cellOutput,
|
|
1108
|
+
})));
|
|
1109
|
+
return collectedCells.length;
|
|
1110
|
+
}
|
|
1111
|
+
acc = next;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
throw new Error("Failed to find enough cells for input");
|
|
1115
|
+
}
|
|
1116
|
+
async completeInputsByCapacity(from, capacityTweak, filter) {
|
|
1117
|
+
const exceptedCapacity = this.getOutputsCapacity() + numFrom(capacityTweak ?? 0);
|
|
1118
|
+
const inputsCapacity = await this.getInputsCapacity(from.client);
|
|
1119
|
+
if (inputsCapacity >= exceptedCapacity) {
|
|
1120
|
+
return 0;
|
|
1121
|
+
}
|
|
1122
|
+
return this.completeInputs(from, filter ?? {
|
|
1123
|
+
scriptLenRange: [0, 1],
|
|
1124
|
+
outputDataLenRange: [0, 1],
|
|
1125
|
+
}, (acc, { cellOutput: { capacity } }) => {
|
|
1126
|
+
const sum = acc + capacity;
|
|
1127
|
+
return sum > exceptedCapacity ? undefined : sum;
|
|
1128
|
+
}, inputsCapacity);
|
|
1129
|
+
}
|
|
1130
|
+
async completeInputsByUdt(from, type) {
|
|
1131
|
+
const exceptedBalance = this.getOutputsUdtBalance(type);
|
|
1132
|
+
const inputsBalance = await this.getInputsUdtBalance(from.client, type);
|
|
1133
|
+
if (inputsBalance >= exceptedBalance) {
|
|
1134
|
+
return 0;
|
|
1135
|
+
}
|
|
1136
|
+
return this.completeInputs(from, {
|
|
1137
|
+
script: type,
|
|
1138
|
+
outputDataLenRange: [16, numFrom("0xffffffff")],
|
|
1139
|
+
}, (acc, { outputData }) => {
|
|
1140
|
+
const balance = udtBalanceFrom(outputData);
|
|
1141
|
+
const sum = acc + balance;
|
|
1142
|
+
return sum > exceptedBalance ? undefined : sum;
|
|
1143
|
+
}, inputsBalance);
|
|
1144
|
+
}
|
|
1145
|
+
estimateFee(feeRate) {
|
|
1146
|
+
const txSize = this.toBytes().length + 4;
|
|
1147
|
+
return (numFrom(txSize) * numFrom(feeRate) + numFrom(1000)) / numFrom(1000);
|
|
1148
|
+
}
|
|
1149
|
+
async completeFee(from, change, feeRate, filter) {
|
|
1150
|
+
// Complete all inputs extra infos for cache
|
|
1151
|
+
await this.getInputsCapacity(from.client);
|
|
1152
|
+
let leastFee = this.estimateFee(feeRate);
|
|
1153
|
+
let leastExtraCapacity = Zero;
|
|
1154
|
+
while (true) {
|
|
1155
|
+
const prepared = await from.prepareTransaction(this.clone());
|
|
1156
|
+
const collected = await prepared.completeInputsByCapacity(from, leastFee + leastExtraCapacity, filter);
|
|
1157
|
+
const extraCapacity = (await prepared.getInputsCapacity(from.client)) -
|
|
1158
|
+
prepared.getOutputsCapacity();
|
|
1159
|
+
// The extra capacity paid the fee without a change
|
|
1160
|
+
if (extraCapacity === leastFee) {
|
|
1161
|
+
this.copy(prepared);
|
|
1162
|
+
return [collected, false];
|
|
1163
|
+
}
|
|
1164
|
+
let changed = prepared.clone();
|
|
1165
|
+
const needed = numFrom(await Promise.resolve(change(changed, extraCapacity - leastFee)));
|
|
1166
|
+
// No enough extra capacity to create new cells for change
|
|
1167
|
+
if (needed > Zero) {
|
|
1168
|
+
leastExtraCapacity = needed;
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
if ((await changed.getInputsCapacity(from.client)) -
|
|
1172
|
+
changed.getOutputsCapacity() !==
|
|
1173
|
+
leastFee) {
|
|
1174
|
+
throw new Error("The change function doesn't use all available capacity");
|
|
1175
|
+
}
|
|
1176
|
+
// New change cells created, update the fee
|
|
1177
|
+
await from.prepareTransaction(changed);
|
|
1178
|
+
const changedFee = changed.estimateFee(feeRate);
|
|
1179
|
+
if (leastFee > changedFee) {
|
|
1180
|
+
throw new Error("The change function removed existed transaction data");
|
|
1181
|
+
}
|
|
1182
|
+
// The fee has been paid
|
|
1183
|
+
if (leastFee === changedFee) {
|
|
1184
|
+
this.copy(changed);
|
|
1185
|
+
return [collected, true];
|
|
1186
|
+
}
|
|
1187
|
+
// The fee after changing is more than the original fee
|
|
1188
|
+
leastFee = changedFee;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
completeFeeChangeToLock(from, change, feeRate, filter) {
|
|
1192
|
+
const script = Script.from(change);
|
|
1193
|
+
return this.completeFee(from, (tx, capacity) => {
|
|
1194
|
+
const changeCell = CellOutput.from({ capacity: 0, lock: script });
|
|
1195
|
+
const occupiedCapacity = fixedPointFrom(changeCell.occupiedSize);
|
|
1196
|
+
if (capacity < occupiedCapacity) {
|
|
1197
|
+
return occupiedCapacity;
|
|
1198
|
+
}
|
|
1199
|
+
changeCell.capacity = capacity;
|
|
1200
|
+
tx.addOutput(changeCell);
|
|
1201
|
+
return 0;
|
|
1202
|
+
}, feeRate, filter);
|
|
1203
|
+
}
|
|
1204
|
+
completeFeeChangeToOutput(from, index, feeRate, filter) {
|
|
1205
|
+
const change = Number(numFrom(index));
|
|
1206
|
+
if (!this.outputs[change]) {
|
|
1207
|
+
throw new Error("Non-existed output to change");
|
|
1208
|
+
}
|
|
1209
|
+
return this.completeFee(from, (tx, capacity) => {
|
|
1210
|
+
tx.outputs[change].capacity += capacity;
|
|
1211
|
+
return 0;
|
|
1212
|
+
}, feeRate, filter);
|
|
773
1213
|
}
|
|
774
1214
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/client/advanced.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../../src/client/advanced.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC"}
|