@arkade-os/sdk 0.4.14 → 0.4.16
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/README.md +287 -215
- package/dist/cjs/arkfee/estimator.js +1 -1
- package/dist/cjs/arkfee/types.js +2 -1
- package/dist/cjs/arknote/index.js +43 -4
- package/dist/cjs/bip322/index.js +1 -1
- package/dist/cjs/contracts/arkcontract.js +1 -1
- package/dist/cjs/contracts/contractManager.js +40 -24
- package/dist/cjs/contracts/contractWatcher.js +29 -22
- package/dist/cjs/contracts/handlers/default.js +1 -1
- package/dist/cjs/contracts/handlers/delegate.js +1 -1
- package/dist/cjs/contracts/handlers/helpers.js +25 -1
- package/dist/cjs/contracts/handlers/vhtlc.js +2 -4
- package/dist/cjs/extension/asset/assetGroup.js +92 -5
- package/dist/cjs/extension/asset/assetId.js +67 -3
- package/dist/cjs/extension/asset/assetInput.js +18 -0
- package/dist/cjs/extension/asset/assetOutput.js +15 -0
- package/dist/cjs/extension/asset/assetRef.js +66 -0
- package/dist/cjs/extension/asset/metadata.js +15 -0
- package/dist/cjs/extension/asset/packet.js +4 -1
- package/dist/cjs/extension/index.js +1 -1
- package/dist/cjs/forfeit.js +14 -0
- package/dist/cjs/identity/index.js +6 -0
- package/dist/cjs/identity/seedIdentity.js +5 -5
- package/dist/cjs/identity/singleKey.js +4 -0
- package/dist/cjs/index.js +5 -3
- package/dist/cjs/intent/index.js +28 -12
- package/dist/cjs/providers/ark.js +3 -2
- package/dist/cjs/providers/delegator.js +20 -1
- package/dist/cjs/providers/expoArk.js +2 -2
- package/dist/cjs/providers/indexer.js +2 -2
- package/dist/cjs/providers/onchain.js +2 -1
- package/dist/cjs/repositories/realm/schemas.js +2 -2
- package/dist/cjs/repositories/realm/types.js +1 -1
- package/dist/cjs/script/address.js +37 -6
- package/dist/cjs/script/base.js +70 -1
- package/dist/cjs/script/default.js +3 -0
- package/dist/cjs/script/delegate.js +4 -0
- package/dist/cjs/script/tapscript.js +25 -4
- package/dist/cjs/script/vhtlc.js +35 -27
- package/dist/cjs/storage/fileSystem.js +1 -1
- package/dist/cjs/storage/inMemory.js +1 -1
- package/dist/cjs/storage/indexedDB.js +1 -1
- package/dist/cjs/storage/localStorage.js +1 -1
- package/dist/cjs/tree/validation.js +1 -1
- package/dist/cjs/utils/arkTransaction.js +5 -5
- package/dist/cjs/utils/bip21.js +16 -3
- package/dist/cjs/utils/syncCursors.js +4 -4
- package/dist/cjs/utils/transaction.js +1 -1
- package/dist/cjs/utils/transactionHistory.js +11 -11
- package/dist/cjs/utils/unknownFields.js +3 -3
- package/dist/cjs/wallet/asset-manager.js +4 -4
- package/dist/cjs/wallet/batch.js +5 -5
- package/dist/cjs/wallet/delegator.js +9 -8
- package/dist/cjs/wallet/expo/background.js +3 -3
- package/dist/cjs/wallet/expo/wallet.js +7 -7
- package/dist/cjs/wallet/index.js +43 -0
- package/dist/cjs/wallet/onchain.js +43 -5
- package/dist/cjs/wallet/ramps.js +44 -14
- package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +22 -22
- package/dist/cjs/wallet/serviceWorker/wallet.js +28 -24
- package/dist/cjs/wallet/unroll.js +12 -8
- package/dist/cjs/wallet/utils.js +1 -1
- package/dist/cjs/wallet/vtxo-manager.js +123 -82
- package/dist/cjs/wallet/wallet.js +231 -98
- package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +1 -1
- package/dist/cjs/worker/expo/processors/contractPollProcessor.js +2 -2
- package/dist/cjs/worker/expo/taskRunner.js +3 -3
- package/dist/cjs/worker/messageBus.js +3 -0
- package/dist/esm/arkfee/estimator.js +1 -1
- package/dist/esm/arkfee/types.js +2 -1
- package/dist/esm/arknote/index.js +43 -4
- package/dist/esm/bip322/index.js +1 -1
- package/dist/esm/contracts/arkcontract.js +1 -1
- package/dist/esm/contracts/contractManager.js +40 -24
- package/dist/esm/contracts/contractWatcher.js +29 -22
- package/dist/esm/contracts/handlers/default.js +1 -1
- package/dist/esm/contracts/handlers/delegate.js +1 -1
- package/dist/esm/contracts/handlers/helpers.js +24 -1
- package/dist/esm/contracts/handlers/vhtlc.js +3 -5
- package/dist/esm/extension/asset/assetGroup.js +92 -5
- package/dist/esm/extension/asset/assetId.js +67 -3
- package/dist/esm/extension/asset/assetInput.js +18 -0
- package/dist/esm/extension/asset/assetOutput.js +15 -0
- package/dist/esm/extension/asset/assetRef.js +66 -0
- package/dist/esm/extension/asset/metadata.js +15 -0
- package/dist/esm/extension/asset/packet.js +4 -1
- package/dist/esm/extension/index.js +1 -1
- package/dist/esm/forfeit.js +14 -0
- package/dist/esm/identity/index.js +5 -0
- package/dist/esm/identity/seedIdentity.js +5 -5
- package/dist/esm/identity/singleKey.js +4 -0
- package/dist/esm/index.js +3 -2
- package/dist/esm/intent/index.js +28 -12
- package/dist/esm/providers/ark.js +3 -2
- package/dist/esm/providers/delegator.js +20 -1
- package/dist/esm/providers/expoArk.js +2 -2
- package/dist/esm/providers/indexer.js +2 -2
- package/dist/esm/providers/onchain.js +2 -1
- package/dist/esm/repositories/realm/schemas.js +2 -2
- package/dist/esm/repositories/realm/types.js +1 -1
- package/dist/esm/script/address.js +37 -6
- package/dist/esm/script/base.js +70 -1
- package/dist/esm/script/default.js +3 -0
- package/dist/esm/script/delegate.js +4 -0
- package/dist/esm/script/tapscript.js +25 -4
- package/dist/esm/script/vhtlc.js +35 -27
- package/dist/esm/storage/fileSystem.js +1 -1
- package/dist/esm/storage/inMemory.js +1 -1
- package/dist/esm/storage/indexedDB.js +1 -1
- package/dist/esm/storage/localStorage.js +1 -1
- package/dist/esm/tree/validation.js +1 -1
- package/dist/esm/utils/arkTransaction.js +5 -5
- package/dist/esm/utils/bip21.js +16 -3
- package/dist/esm/utils/syncCursors.js +4 -4
- package/dist/esm/utils/transaction.js +1 -1
- package/dist/esm/utils/transactionHistory.js +11 -11
- package/dist/esm/utils/unknownFields.js +3 -3
- package/dist/esm/wallet/asset-manager.js +4 -4
- package/dist/esm/wallet/batch.js +5 -5
- package/dist/esm/wallet/delegator.js +9 -8
- package/dist/esm/wallet/expo/background.js +3 -3
- package/dist/esm/wallet/expo/wallet.js +7 -7
- package/dist/esm/wallet/index.js +43 -0
- package/dist/esm/wallet/onchain.js +43 -5
- package/dist/esm/wallet/ramps.js +44 -14
- package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +22 -22
- package/dist/esm/wallet/serviceWorker/wallet.js +28 -24
- package/dist/esm/wallet/unroll.js +12 -8
- package/dist/esm/wallet/utils.js +1 -1
- package/dist/esm/wallet/vtxo-manager.js +122 -81
- package/dist/esm/wallet/wallet.js +232 -99
- package/dist/esm/worker/expo/asyncStorageTaskQueue.js +1 -1
- package/dist/esm/worker/expo/processors/contractPollProcessor.js +2 -2
- package/dist/esm/worker/expo/taskRunner.js +3 -3
- package/dist/esm/worker/messageBus.js +3 -0
- package/dist/types/arkfee/estimator.d.ts +1 -1
- package/dist/types/arkfee/types.d.ts +2 -1
- package/dist/types/arknote/index.d.ts +44 -4
- package/dist/types/bip322/index.d.ts +1 -1
- package/dist/types/contracts/arkcontract.d.ts +1 -1
- package/dist/types/contracts/contractManager.d.ts +40 -63
- package/dist/types/contracts/contractWatcher.d.ts +39 -18
- package/dist/types/contracts/handlers/default.d.ts +1 -1
- package/dist/types/contracts/handlers/delegate.d.ts +1 -1
- package/dist/types/contracts/handlers/helpers.d.ts +11 -1
- package/dist/types/contracts/types.d.ts +36 -26
- package/dist/types/extension/asset/assetGroup.d.ts +92 -1
- package/dist/types/extension/asset/assetId.d.ts +67 -3
- package/dist/types/extension/asset/assetInput.d.ts +18 -0
- package/dist/types/extension/asset/assetOutput.d.ts +15 -0
- package/dist/types/extension/asset/assetRef.d.ts +66 -0
- package/dist/types/extension/asset/metadata.d.ts +15 -0
- package/dist/types/extension/asset/packet.d.ts +4 -1
- package/dist/types/extension/index.d.ts +1 -1
- package/dist/types/forfeit.d.ts +14 -0
- package/dist/types/identity/index.d.ts +36 -0
- package/dist/types/identity/seedIdentity.d.ts +10 -8
- package/dist/types/identity/singleKey.d.ts +4 -0
- package/dist/types/index.d.ts +3 -3
- package/dist/types/intent/index.d.ts +19 -6
- package/dist/types/providers/ark.d.ts +40 -2
- package/dist/types/providers/delegator.d.ts +54 -1
- package/dist/types/providers/expoArk.d.ts +2 -2
- package/dist/types/providers/indexer.d.ts +105 -2
- package/dist/types/providers/onchain.d.ts +62 -1
- package/dist/types/repositories/realm/schemas.d.ts +2 -2
- package/dist/types/repositories/realm/types.d.ts +2 -2
- package/dist/types/repositories/walletRepository.d.ts +16 -0
- package/dist/types/script/address.d.ts +35 -2
- package/dist/types/script/base.d.ts +66 -1
- package/dist/types/script/default.d.ts +3 -0
- package/dist/types/script/delegate.d.ts +4 -0
- package/dist/types/script/tapscript.d.ts +17 -2
- package/dist/types/script/vhtlc.d.ts +35 -27
- package/dist/types/storage/fileSystem.d.ts +1 -1
- package/dist/types/storage/inMemory.d.ts +1 -1
- package/dist/types/storage/index.d.ts +1 -1
- package/dist/types/storage/indexedDB.d.ts +1 -1
- package/dist/types/storage/localStorage.d.ts +1 -1
- package/dist/types/utils/arkTransaction.d.ts +3 -3
- package/dist/types/utils/bip21.d.ts +17 -0
- package/dist/types/utils/syncCursors.d.ts +4 -4
- package/dist/types/utils/transaction.d.ts +1 -1
- package/dist/types/utils/transactionHistory.d.ts +3 -3
- package/dist/types/utils/unknownFields.d.ts +5 -5
- package/dist/types/wallet/asset-manager.d.ts +3 -3
- package/dist/types/wallet/batch.d.ts +27 -7
- package/dist/types/wallet/delegator.d.ts +10 -0
- package/dist/types/wallet/expo/background.d.ts +4 -4
- package/dist/types/wallet/expo/wallet.d.ts +10 -10
- package/dist/types/wallet/index.d.ts +457 -25
- package/dist/types/wallet/onchain.d.ts +42 -4
- package/dist/types/wallet/ramps.d.ts +40 -10
- package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +4 -4
- package/dist/types/wallet/serviceWorker/wallet.d.ts +71 -33
- package/dist/types/wallet/unroll.d.ts +8 -6
- package/dist/types/wallet/vtxo-manager.d.ts +146 -93
- package/dist/types/wallet/wallet.d.ts +91 -33
- package/dist/types/worker/expo/asyncStorageTaskQueue.d.ts +1 -1
- package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +1 -1
- package/dist/types/worker/expo/taskRunner.d.ts +6 -6
- package/dist/types/worker/messageBus.d.ts +5 -3
- package/package.json +18 -10
|
@@ -10,9 +10,25 @@ const assetOutput_1 = require("./assetOutput");
|
|
|
10
10
|
const metadata_1 = require("./metadata");
|
|
11
11
|
const utils_1 = require("./utils");
|
|
12
12
|
/**
|
|
13
|
-
* An asset group contains inputs
|
|
13
|
+
* An asset group contains inputs, outputs, and all data related to a given asset id.
|
|
14
|
+
*
|
|
15
|
+
* @see Packet
|
|
16
|
+
* @see AssetId
|
|
17
|
+
* @see AssetRef
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const group = AssetGroup.create(
|
|
22
|
+
* null, // asset ID: null for new issuance
|
|
23
|
+
* null, // control asset ID: null when reissuance not needed
|
|
24
|
+
* [], // asset inputs: empty for new issuance
|
|
25
|
+
* [AssetOutput.create(0, 1000)], // asset outputs: 1000 units at vout index 0
|
|
26
|
+
* [] // metadata: can be empty
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
14
29
|
*/
|
|
15
30
|
class AssetGroup {
|
|
31
|
+
/** @see create */
|
|
16
32
|
constructor(assetId, controlAsset, inputs, outputs, metadata) {
|
|
17
33
|
this.assetId = assetId;
|
|
18
34
|
this.controlAsset = controlAsset;
|
|
@@ -20,12 +36,31 @@ class AssetGroup {
|
|
|
20
36
|
this.outputs = outputs;
|
|
21
37
|
this.metadataList = new metadata_1.MetadataList(metadata);
|
|
22
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Create and validate an asset group.
|
|
41
|
+
*
|
|
42
|
+
* @param assetId - Asset id for this group, or `null` for fresh issuance
|
|
43
|
+
* @param controlAsset - Optional control asset reference for (re) issuance
|
|
44
|
+
* @param inputs - Asset inputs in the group
|
|
45
|
+
* @param outputs - Asset outputs in the group
|
|
46
|
+
* @param metadata - Metadata entries associated with the group
|
|
47
|
+
* @returns A validated asset group
|
|
48
|
+
* @throws Error if the group fails validation
|
|
49
|
+
* @see validate
|
|
50
|
+
*/
|
|
23
51
|
static create(assetId, controlAsset, inputs, outputs, metadata) {
|
|
24
52
|
const ag = new AssetGroup(assetId, controlAsset, inputs, outputs, metadata);
|
|
25
53
|
ag.validate();
|
|
26
54
|
return ag;
|
|
27
55
|
}
|
|
28
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Decode an asset group from its hex string form.
|
|
58
|
+
*
|
|
59
|
+
* @param s - Hex-encoded asset group
|
|
60
|
+
* @returns Decoded asset group
|
|
61
|
+
* @throws Error if the string is not valid hex or does not encode a valid asset group
|
|
62
|
+
* @see toString
|
|
63
|
+
*/
|
|
29
64
|
static fromString(s) {
|
|
30
65
|
let buf;
|
|
31
66
|
try {
|
|
@@ -36,6 +71,13 @@ class AssetGroup {
|
|
|
36
71
|
}
|
|
37
72
|
return AssetGroup.fromBytes(buf);
|
|
38
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Decode an asset group from its serialized bytes.
|
|
76
|
+
*
|
|
77
|
+
* @param buf - Serialized asset group bytes
|
|
78
|
+
* @returns Decoded asset group
|
|
79
|
+
* @throws Error if the buffer is empty or malformed
|
|
80
|
+
*/
|
|
39
81
|
static fromBytes(buf) {
|
|
40
82
|
if (!buf || buf.length === 0) {
|
|
41
83
|
throw new Error("missing asset group");
|
|
@@ -43,12 +85,21 @@ class AssetGroup {
|
|
|
43
85
|
const reader = new utils_1.BufferReader(buf);
|
|
44
86
|
return AssetGroup.fromReader(reader);
|
|
45
87
|
}
|
|
46
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Return true when the group represents an issuance.
|
|
90
|
+
*
|
|
91
|
+
* @returns `true` when the group has no asset id
|
|
92
|
+
*/
|
|
47
93
|
isIssuance() {
|
|
48
94
|
return this.assetId === null;
|
|
49
95
|
}
|
|
50
|
-
|
|
51
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Return true when the group represents a reissuance.
|
|
98
|
+
*
|
|
99
|
+
* @returns `true` when the group has an asset id and outputs exceed local inputs
|
|
100
|
+
* @remarks
|
|
101
|
+
* Only local inputs contribute to the comparison; intent-backed inputs contribute `0` here.
|
|
102
|
+
*/
|
|
52
103
|
isReissuance() {
|
|
53
104
|
const sumReducer = (s, { amount }) => s + amount;
|
|
54
105
|
const sumOutputs = this.outputs.reduce(sumReducer, 0n);
|
|
@@ -59,12 +110,23 @@ class AssetGroup {
|
|
|
59
110
|
.reduce(sumReducer, 0n);
|
|
60
111
|
return !this.isIssuance() && sumInputs < sumOutputs;
|
|
61
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Serialize the asset group to raw bytes.
|
|
115
|
+
*
|
|
116
|
+
* @returns Serialized asset group bytes
|
|
117
|
+
* @see fromBytes
|
|
118
|
+
*/
|
|
62
119
|
serialize() {
|
|
63
120
|
this.validate();
|
|
64
121
|
const writer = new utils_1.BufferWriter();
|
|
65
122
|
this.serializeTo(writer);
|
|
66
123
|
return writer.toBytes();
|
|
67
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Validate the asset group and its child structures.
|
|
127
|
+
*
|
|
128
|
+
* @throws Error if the group is empty or violates issuance invariants
|
|
129
|
+
*/
|
|
68
130
|
validate() {
|
|
69
131
|
if (this.inputs.length === 0 && this.outputs.length === 0) {
|
|
70
132
|
throw new Error("empty asset group");
|
|
@@ -80,13 +142,33 @@ class AssetGroup {
|
|
|
80
142
|
}
|
|
81
143
|
}
|
|
82
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* Convert the group into its batch-leaf representation for the given intent txid.
|
|
147
|
+
*
|
|
148
|
+
* @param intentTxid - Intent transaction id used to build the leaf input reference
|
|
149
|
+
* @returns Batch-leaf asset group
|
|
150
|
+
* @see AssetInput.createIntent
|
|
151
|
+
*/
|
|
83
152
|
toBatchLeafAssetGroup(intentTxid) {
|
|
84
153
|
const leafInput = assetInput_1.AssetInput.createIntent(base_1.hex.encode(intentTxid), 0, 0);
|
|
85
154
|
return new AssetGroup(this.assetId, this.controlAsset, [leafInput], this.outputs, this.metadataList.items);
|
|
86
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Encode the asset group to a hex string.
|
|
158
|
+
*
|
|
159
|
+
* @returns Hex-encoded asset group
|
|
160
|
+
* @see fromString
|
|
161
|
+
*/
|
|
87
162
|
toString() {
|
|
88
163
|
return base_1.hex.encode(this.serialize());
|
|
89
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Decode an asset group from a binary reader.
|
|
167
|
+
*
|
|
168
|
+
* @param reader - Reader positioned at an asset group
|
|
169
|
+
* @returns Decoded asset group
|
|
170
|
+
* @throws Error if the encoded group is malformed
|
|
171
|
+
*/
|
|
90
172
|
static fromReader(reader) {
|
|
91
173
|
const presence = reader.readByte();
|
|
92
174
|
let assetId = null;
|
|
@@ -107,6 +189,11 @@ class AssetGroup {
|
|
|
107
189
|
ag.validate();
|
|
108
190
|
return ag;
|
|
109
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Serialize the asset group into an existing binary writer.
|
|
194
|
+
*
|
|
195
|
+
* @param writer - Writer to append the asset group to
|
|
196
|
+
*/
|
|
110
197
|
serializeTo(writer) {
|
|
111
198
|
let presence = 0;
|
|
112
199
|
if (this.assetId !== null) {
|
|
@@ -5,15 +5,34 @@ const base_1 = require("@scure/base");
|
|
|
5
5
|
const types_1 = require("./types");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
7
|
/**
|
|
8
|
-
* AssetId
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
8
|
+
* AssetId identifies a specific asset.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Asset ids are derived from the genesis transaction id plus the asset group index.
|
|
12
|
+
*
|
|
13
|
+
* @see AssetRef
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const assetId = AssetId.create('00'.repeat(32), 0)
|
|
18
|
+
* const encoded = assetId.toString()
|
|
19
|
+
* const decoded = AssetId.fromString(encoded)
|
|
20
|
+
* ```
|
|
11
21
|
*/
|
|
12
22
|
class AssetId {
|
|
13
23
|
constructor(txid, groupIndex) {
|
|
14
24
|
this.txid = txid;
|
|
15
25
|
this.groupIndex = groupIndex;
|
|
16
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Create an asset id from a genesis transaction id and group index.
|
|
29
|
+
*
|
|
30
|
+
* @param txid - Hex-encoded genesis transaction id
|
|
31
|
+
* @param groupIndex - Asset group index within the genesis transaction
|
|
32
|
+
* @returns A validated asset id
|
|
33
|
+
* @throws Error if the txid is missing, malformed, or not 32 bytes long
|
|
34
|
+
* @see fromString
|
|
35
|
+
*/
|
|
17
36
|
static create(txid, groupIndex) {
|
|
18
37
|
if (!txid) {
|
|
19
38
|
throw new Error("missing txid");
|
|
@@ -32,6 +51,14 @@ class AssetId {
|
|
|
32
51
|
assetId.validate();
|
|
33
52
|
return assetId;
|
|
34
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Decode an asset id from its hex string representation.
|
|
56
|
+
*
|
|
57
|
+
* @param s - Hex-encoded asset id
|
|
58
|
+
* @returns Decoded asset id
|
|
59
|
+
* @throws Error if the string is not valid hex or does not encode a valid asset id
|
|
60
|
+
* @see toString
|
|
61
|
+
*/
|
|
35
62
|
static fromString(s) {
|
|
36
63
|
let buf;
|
|
37
64
|
try {
|
|
@@ -42,6 +69,13 @@ class AssetId {
|
|
|
42
69
|
}
|
|
43
70
|
return AssetId.fromBytes(buf);
|
|
44
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Decode an asset id from its serialized bytes.
|
|
74
|
+
*
|
|
75
|
+
* @param buf - Serialized asset id bytes
|
|
76
|
+
* @returns Decoded asset id
|
|
77
|
+
* @throws Error if the buffer length is invalid
|
|
78
|
+
*/
|
|
45
79
|
static fromBytes(buf) {
|
|
46
80
|
if (!buf || buf.length === 0) {
|
|
47
81
|
throw new Error("missing asset id");
|
|
@@ -52,14 +86,31 @@ class AssetId {
|
|
|
52
86
|
const reader = new utils_1.BufferReader(buf);
|
|
53
87
|
return AssetId.fromReader(reader);
|
|
54
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Serialize the asset id to raw bytes.
|
|
91
|
+
*
|
|
92
|
+
* @returns Serialized asset id bytes
|
|
93
|
+
* @see fromBytes
|
|
94
|
+
*/
|
|
55
95
|
serialize() {
|
|
56
96
|
const writer = new utils_1.BufferWriter();
|
|
57
97
|
this.serializeTo(writer);
|
|
58
98
|
return writer.toBytes();
|
|
59
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Encode the asset id to a hex string.
|
|
102
|
+
*
|
|
103
|
+
* @returns Hex-encoded asset id
|
|
104
|
+
* @see fromString
|
|
105
|
+
*/
|
|
60
106
|
toString() {
|
|
61
107
|
return base_1.hex.encode(this.serialize());
|
|
62
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Validate the asset id fields.
|
|
111
|
+
*
|
|
112
|
+
* @throws Error if the txid is empty or the group index is out of range
|
|
113
|
+
*/
|
|
63
114
|
validate() {
|
|
64
115
|
if ((0, utils_1.isZeroBytes)(this.txid)) {
|
|
65
116
|
throw new Error("empty txid");
|
|
@@ -70,6 +121,13 @@ class AssetId {
|
|
|
70
121
|
throw new Error(`invalid group index: ${this.groupIndex}, must be in range [0, 65535]`);
|
|
71
122
|
}
|
|
72
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Decode an asset id from a binary reader.
|
|
126
|
+
*
|
|
127
|
+
* @param reader - Reader positioned at an asset id
|
|
128
|
+
* @returns Decoded asset id
|
|
129
|
+
* @throws Error if the reader does not contain enough bytes
|
|
130
|
+
*/
|
|
73
131
|
static fromReader(reader) {
|
|
74
132
|
if (reader.remaining() < types_1.ASSET_ID_SIZE) {
|
|
75
133
|
throw new Error(`invalid asset id length: got ${reader.remaining()}, want ${types_1.ASSET_ID_SIZE}`);
|
|
@@ -80,6 +138,12 @@ class AssetId {
|
|
|
80
138
|
assetId.validate();
|
|
81
139
|
return assetId;
|
|
82
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Serialize the asset id into an existing binary writer.
|
|
143
|
+
*
|
|
144
|
+
* @param writer - Writer to append the asset id to
|
|
145
|
+
* @see serialize
|
|
146
|
+
*/
|
|
83
147
|
serializeTo(writer) {
|
|
84
148
|
writer.write(this.txid);
|
|
85
149
|
writer.writeUint16LE(this.groupIndex);
|
|
@@ -13,12 +13,15 @@ class AssetInput {
|
|
|
13
13
|
constructor(input) {
|
|
14
14
|
this.input = input;
|
|
15
15
|
}
|
|
16
|
+
/** Gets the transaction input index for an asset input, e.g. 0 */
|
|
16
17
|
get vin() {
|
|
17
18
|
return this.input.vin;
|
|
18
19
|
}
|
|
20
|
+
/** Gets the amount for an input (in most cases, 330 sats) */
|
|
19
21
|
get amount() {
|
|
20
22
|
return this.input.amount;
|
|
21
23
|
}
|
|
24
|
+
/** Create a local asset input that points at a transaction input index. */
|
|
22
25
|
static create(vin, amount) {
|
|
23
26
|
const input = new AssetInput({
|
|
24
27
|
type: types_1.AssetInputType.Local,
|
|
@@ -28,6 +31,7 @@ class AssetInput {
|
|
|
28
31
|
input.validate();
|
|
29
32
|
return input;
|
|
30
33
|
}
|
|
34
|
+
/** Create an intent-backed asset input referencing an external intent transaction. */
|
|
31
35
|
static createIntent(txid, vin, amount) {
|
|
32
36
|
if (!txid || txid.length === 0) {
|
|
33
37
|
throw new Error("missing input intent txid");
|
|
@@ -51,6 +55,7 @@ class AssetInput {
|
|
|
51
55
|
input.validate();
|
|
52
56
|
return input;
|
|
53
57
|
}
|
|
58
|
+
/** Decode an asset input from its hex string form. */
|
|
54
59
|
static fromString(s) {
|
|
55
60
|
let buf;
|
|
56
61
|
try {
|
|
@@ -61,18 +66,22 @@ class AssetInput {
|
|
|
61
66
|
}
|
|
62
67
|
return AssetInput.fromBytes(buf);
|
|
63
68
|
}
|
|
69
|
+
/** Decode an asset input from its serialized bytes. */
|
|
64
70
|
static fromBytes(buf) {
|
|
65
71
|
const reader = new utils_1.BufferReader(buf);
|
|
66
72
|
return AssetInput.fromReader(reader);
|
|
67
73
|
}
|
|
74
|
+
/** Serialize the asset input to raw bytes. */
|
|
68
75
|
serialize() {
|
|
69
76
|
const writer = new utils_1.BufferWriter();
|
|
70
77
|
this.serializeTo(writer);
|
|
71
78
|
return writer.toBytes();
|
|
72
79
|
}
|
|
80
|
+
/** Encode the asset input to a hex string. */
|
|
73
81
|
toString() {
|
|
74
82
|
return base_1.hex.encode(this.serialize());
|
|
75
83
|
}
|
|
84
|
+
/** Validate the asset input fields. */
|
|
76
85
|
validate() {
|
|
77
86
|
switch (this.input.type) {
|
|
78
87
|
case types_1.AssetInputType.Local:
|
|
@@ -84,6 +93,7 @@ class AssetInput {
|
|
|
84
93
|
break;
|
|
85
94
|
}
|
|
86
95
|
}
|
|
96
|
+
/** Decode an asset input from a buffer reader. */
|
|
87
97
|
static fromReader(reader) {
|
|
88
98
|
const type = reader.readByte();
|
|
89
99
|
let input;
|
|
@@ -121,6 +131,7 @@ class AssetInput {
|
|
|
121
131
|
input.validate();
|
|
122
132
|
return input;
|
|
123
133
|
}
|
|
134
|
+
/** Serialize the asset input into an existing buffer writer. */
|
|
124
135
|
serializeTo(writer) {
|
|
125
136
|
writer.writeByte(this.input.type);
|
|
126
137
|
if (this.input.type === types_1.AssetInputType.Intent) {
|
|
@@ -138,11 +149,13 @@ class AssetInputs {
|
|
|
138
149
|
constructor(inputs) {
|
|
139
150
|
this.inputs = inputs;
|
|
140
151
|
}
|
|
152
|
+
/** Create a validated list of asset inputs. */
|
|
141
153
|
static create(inputs) {
|
|
142
154
|
const list = new AssetInputs(inputs);
|
|
143
155
|
list.validate();
|
|
144
156
|
return list;
|
|
145
157
|
}
|
|
158
|
+
/** Decode an asset input list from its hex string form. */
|
|
146
159
|
static fromString(s) {
|
|
147
160
|
if (!s || s.length === 0) {
|
|
148
161
|
throw new Error("missing asset inputs");
|
|
@@ -157,14 +170,17 @@ class AssetInputs {
|
|
|
157
170
|
const reader = new utils_1.BufferReader(buf);
|
|
158
171
|
return AssetInputs.fromReader(reader);
|
|
159
172
|
}
|
|
173
|
+
/** Serialize the asset input list to raw bytes. */
|
|
160
174
|
serialize() {
|
|
161
175
|
const writer = new utils_1.BufferWriter();
|
|
162
176
|
this.serializeTo(writer);
|
|
163
177
|
return writer.toBytes();
|
|
164
178
|
}
|
|
179
|
+
/** Encode the asset input list to a hex string. */
|
|
165
180
|
toString() {
|
|
166
181
|
return base_1.hex.encode(this.serialize());
|
|
167
182
|
}
|
|
183
|
+
/** Validate the asset input list. */
|
|
168
184
|
validate() {
|
|
169
185
|
const seen = new Set();
|
|
170
186
|
let listType = types_1.AssetInputType.Unspecified;
|
|
@@ -186,6 +202,7 @@ class AssetInputs {
|
|
|
186
202
|
}
|
|
187
203
|
}
|
|
188
204
|
}
|
|
205
|
+
/** Decode an asset input list from a buffer reader. */
|
|
189
206
|
static fromReader(reader) {
|
|
190
207
|
const count = Number(reader.readVarUint());
|
|
191
208
|
const inputs = [];
|
|
@@ -194,6 +211,7 @@ class AssetInputs {
|
|
|
194
211
|
}
|
|
195
212
|
return AssetInputs.create(inputs);
|
|
196
213
|
}
|
|
214
|
+
/** Serialize the asset input list into an existing buffer writer. */
|
|
197
215
|
serializeTo(writer) {
|
|
198
216
|
writer.writeVarUint(this.inputs.length);
|
|
199
217
|
for (const input of this.inputs) {
|
|
@@ -15,11 +15,13 @@ class AssetOutput {
|
|
|
15
15
|
this.vout = vout;
|
|
16
16
|
this.amount = amount;
|
|
17
17
|
}
|
|
18
|
+
/** Create a local asset output referencing a transaction output index. */
|
|
18
19
|
static create(vout, amount) {
|
|
19
20
|
const output = new AssetOutput(vout, typeof amount === "number" ? BigInt(amount) : amount);
|
|
20
21
|
output.validate();
|
|
21
22
|
return output;
|
|
22
23
|
}
|
|
24
|
+
/** Decode an asset output from its hex string form. */
|
|
23
25
|
static fromString(s) {
|
|
24
26
|
let buf;
|
|
25
27
|
try {
|
|
@@ -30,6 +32,7 @@ class AssetOutput {
|
|
|
30
32
|
}
|
|
31
33
|
return AssetOutput.fromBytes(buf);
|
|
32
34
|
}
|
|
35
|
+
/** Decode an asset output from its serialized bytes. */
|
|
33
36
|
static fromBytes(buf) {
|
|
34
37
|
if (!buf || buf.length === 0) {
|
|
35
38
|
throw new Error("missing asset output");
|
|
@@ -39,14 +42,17 @@ class AssetOutput {
|
|
|
39
42
|
output.validate();
|
|
40
43
|
return output;
|
|
41
44
|
}
|
|
45
|
+
/** Serialize the asset output to raw bytes. */
|
|
42
46
|
serialize() {
|
|
43
47
|
const writer = new utils_1.BufferWriter();
|
|
44
48
|
this.serializeTo(writer);
|
|
45
49
|
return writer.toBytes();
|
|
46
50
|
}
|
|
51
|
+
/** Encode the asset output to a hex string. */
|
|
47
52
|
toString() {
|
|
48
53
|
return base_1.hex.encode(this.serialize());
|
|
49
54
|
}
|
|
55
|
+
/** Validate the asset output fields. */
|
|
50
56
|
validate() {
|
|
51
57
|
if (!Number.isInteger(this.vout) ||
|
|
52
58
|
this.vout < 0 ||
|
|
@@ -57,6 +63,7 @@ class AssetOutput {
|
|
|
57
63
|
throw new Error("asset output amount must be greater than 0");
|
|
58
64
|
}
|
|
59
65
|
}
|
|
66
|
+
/** Decode an asset output from a buffer reader. */
|
|
60
67
|
static fromReader(reader) {
|
|
61
68
|
if (reader.remaining() < 2) {
|
|
62
69
|
throw new Error("invalid asset output vout length");
|
|
@@ -78,6 +85,7 @@ class AssetOutput {
|
|
|
78
85
|
const amount = reader.readVarUint();
|
|
79
86
|
return new AssetOutput(vout, amount);
|
|
80
87
|
}
|
|
88
|
+
/** Serialize the asset output into an existing buffer writer. */
|
|
81
89
|
serializeTo(writer) {
|
|
82
90
|
writer.writeByte(0x01);
|
|
83
91
|
writer.writeUint16LE(this.vout);
|
|
@@ -98,11 +106,13 @@ class AssetOutputs {
|
|
|
98
106
|
constructor(outputs) {
|
|
99
107
|
this.outputs = outputs;
|
|
100
108
|
}
|
|
109
|
+
/** Create a validated list of asset outputs. */
|
|
101
110
|
static create(outputs) {
|
|
102
111
|
const list = new AssetOutputs(outputs);
|
|
103
112
|
list.validate();
|
|
104
113
|
return list;
|
|
105
114
|
}
|
|
115
|
+
/** Decode an asset output list from its hex string form. */
|
|
106
116
|
static fromString(s) {
|
|
107
117
|
if (!s || s.length === 0) {
|
|
108
118
|
throw new Error("missing asset outputs");
|
|
@@ -117,14 +127,17 @@ class AssetOutputs {
|
|
|
117
127
|
const reader = new utils_1.BufferReader(buf);
|
|
118
128
|
return AssetOutputs.fromReader(reader);
|
|
119
129
|
}
|
|
130
|
+
/** Serialize the asset output list to raw bytes. */
|
|
120
131
|
serialize() {
|
|
121
132
|
const writer = new utils_1.BufferWriter();
|
|
122
133
|
this.serializeTo(writer);
|
|
123
134
|
return writer.toBytes();
|
|
124
135
|
}
|
|
136
|
+
/** Encode the asset output list to a hex string. */
|
|
125
137
|
toString() {
|
|
126
138
|
return base_1.hex.encode(this.serialize());
|
|
127
139
|
}
|
|
140
|
+
/** Validate the asset output list. */
|
|
128
141
|
validate() {
|
|
129
142
|
const seen = new Set();
|
|
130
143
|
for (const output of this.outputs) {
|
|
@@ -135,6 +148,7 @@ class AssetOutputs {
|
|
|
135
148
|
seen.add(output.vout);
|
|
136
149
|
}
|
|
137
150
|
}
|
|
151
|
+
/** Decode an asset output list from a buffer reader. */
|
|
138
152
|
static fromReader(reader) {
|
|
139
153
|
const count = Number(reader.readVarUint());
|
|
140
154
|
if (count === 0) {
|
|
@@ -148,6 +162,7 @@ class AssetOutputs {
|
|
|
148
162
|
result.validate();
|
|
149
163
|
return result;
|
|
150
164
|
}
|
|
165
|
+
/** Serialize the asset output list into an existing buffer writer. */
|
|
151
166
|
serializeTo(writer) {
|
|
152
167
|
this.validate();
|
|
153
168
|
writer.writeVarUint(this.outputs.length);
|
|
@@ -5,19 +5,53 @@ const base_1 = require("@scure/base");
|
|
|
5
5
|
const types_1 = require("./types");
|
|
6
6
|
const assetId_1 = require("./assetId");
|
|
7
7
|
const utils_1 = require("./utils");
|
|
8
|
+
/**
|
|
9
|
+
* Reference to either an explicit asset id or another asset group in the same packet.
|
|
10
|
+
*
|
|
11
|
+
* @see AssetId
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const refById = AssetRef.fromId(assetId)
|
|
16
|
+
* const refByGroup = AssetRef.fromGroupIndex(0)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
8
19
|
class AssetRef {
|
|
9
20
|
constructor(ref) {
|
|
10
21
|
this.ref = ref;
|
|
11
22
|
}
|
|
23
|
+
/** Reference type discriminator. */
|
|
12
24
|
get type() {
|
|
13
25
|
return this.ref.type;
|
|
14
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Create an asset reference that points to a specific asset id.
|
|
29
|
+
*
|
|
30
|
+
* @param assetId - Asset id referenced by this pointer
|
|
31
|
+
* @returns Asset reference by id
|
|
32
|
+
* @see fromGroupIndex
|
|
33
|
+
*/
|
|
15
34
|
static fromId(assetId) {
|
|
16
35
|
return new AssetRef({ type: types_1.AssetRefType.ByID, assetId });
|
|
17
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Create an asset reference that points to another asset group by index.
|
|
39
|
+
*
|
|
40
|
+
* @param groupIndex - Zero-based asset group index in the packet
|
|
41
|
+
* @returns Asset reference by group index
|
|
42
|
+
* @see fromId
|
|
43
|
+
*/
|
|
18
44
|
static fromGroupIndex(groupIndex) {
|
|
19
45
|
return new AssetRef({ type: types_1.AssetRefType.ByGroup, groupIndex });
|
|
20
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Decode an asset reference from its hex string form.
|
|
49
|
+
*
|
|
50
|
+
* @param s - Hex-encoded asset reference
|
|
51
|
+
* @returns Decoded asset reference
|
|
52
|
+
* @throws Error if the string is not valid hex or does not encode a valid asset reference
|
|
53
|
+
* @see toString
|
|
54
|
+
*/
|
|
21
55
|
static fromString(s) {
|
|
22
56
|
let buf;
|
|
23
57
|
try {
|
|
@@ -28,6 +62,13 @@ class AssetRef {
|
|
|
28
62
|
}
|
|
29
63
|
return AssetRef.fromBytes(buf);
|
|
30
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Decode an asset reference from its serialized bytes.
|
|
67
|
+
*
|
|
68
|
+
* @param buf - Serialized asset reference bytes
|
|
69
|
+
* @returns Decoded asset reference
|
|
70
|
+
* @throws Error if the buffer is empty or malformed
|
|
71
|
+
*/
|
|
31
72
|
static fromBytes(buf) {
|
|
32
73
|
if (!buf || buf.length === 0) {
|
|
33
74
|
throw new Error("missing asset ref");
|
|
@@ -35,14 +76,33 @@ class AssetRef {
|
|
|
35
76
|
const reader = new utils_1.BufferReader(buf);
|
|
36
77
|
return AssetRef.fromReader(reader);
|
|
37
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Serialize the asset reference to raw bytes.
|
|
81
|
+
*
|
|
82
|
+
* @returns Serialized asset reference bytes
|
|
83
|
+
* @see fromBytes
|
|
84
|
+
*/
|
|
38
85
|
serialize() {
|
|
39
86
|
const writer = new utils_1.BufferWriter();
|
|
40
87
|
this.serializeTo(writer);
|
|
41
88
|
return writer.toBytes();
|
|
42
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Encode the asset reference to a hex string.
|
|
92
|
+
*
|
|
93
|
+
* @returns Hex-encoded asset reference
|
|
94
|
+
* @see fromString
|
|
95
|
+
*/
|
|
43
96
|
toString() {
|
|
44
97
|
return base_1.hex.encode(this.serialize());
|
|
45
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Decode an asset reference from a binary reader.
|
|
101
|
+
*
|
|
102
|
+
* @param reader - Reader positioned at an asset reference
|
|
103
|
+
* @returns Decoded asset reference
|
|
104
|
+
* @throws Error if the type is unknown or the reader does not contain enough bytes
|
|
105
|
+
*/
|
|
46
106
|
static fromReader(reader) {
|
|
47
107
|
const type = reader.readByte();
|
|
48
108
|
let ref;
|
|
@@ -67,6 +127,12 @@ class AssetRef {
|
|
|
67
127
|
}
|
|
68
128
|
return ref;
|
|
69
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Serialize the asset reference into an existing binary writer.
|
|
132
|
+
*
|
|
133
|
+
* @param writer - Writer to append the asset reference to
|
|
134
|
+
* @see serialize
|
|
135
|
+
*/
|
|
70
136
|
serializeTo(writer) {
|
|
71
137
|
writer.writeByte(this.ref.type);
|
|
72
138
|
switch (this.ref.type) {
|