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