@arkade-os/sdk 0.3.13 → 0.4.0-next.1

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 (270) hide show
  1. package/README.md +586 -54
  2. package/dist/cjs/asset/assetGroup.js +141 -0
  3. package/dist/cjs/asset/assetId.js +88 -0
  4. package/dist/cjs/asset/assetInput.js +204 -0
  5. package/dist/cjs/asset/assetOutput.js +159 -0
  6. package/dist/cjs/asset/assetRef.js +82 -0
  7. package/dist/cjs/asset/index.js +24 -0
  8. package/dist/cjs/asset/metadata.js +172 -0
  9. package/dist/cjs/asset/packet.js +164 -0
  10. package/dist/cjs/asset/types.js +25 -0
  11. package/dist/cjs/asset/utils.js +105 -0
  12. package/dist/cjs/bip322/index.js +270 -0
  13. package/dist/cjs/contracts/arkcontract.js +148 -0
  14. package/dist/cjs/contracts/contractManager.js +436 -0
  15. package/dist/cjs/contracts/contractWatcher.js +567 -0
  16. package/dist/cjs/contracts/handlers/default.js +85 -0
  17. package/dist/cjs/contracts/handlers/delegate.js +89 -0
  18. package/dist/cjs/contracts/handlers/helpers.js +105 -0
  19. package/dist/cjs/contracts/handlers/index.js +19 -0
  20. package/dist/cjs/contracts/handlers/registry.js +89 -0
  21. package/dist/cjs/contracts/handlers/vhtlc.js +193 -0
  22. package/dist/cjs/contracts/index.js +41 -0
  23. package/dist/cjs/contracts/types.js +2 -0
  24. package/dist/cjs/forfeit.js +12 -8
  25. package/dist/cjs/identity/index.js +1 -0
  26. package/dist/cjs/identity/seedIdentity.js +255 -0
  27. package/dist/cjs/index.js +72 -14
  28. package/dist/cjs/intent/index.js +47 -11
  29. package/dist/cjs/providers/ark.js +7 -0
  30. package/dist/cjs/providers/delegator.js +66 -0
  31. package/dist/cjs/providers/expoIndexer.js +5 -0
  32. package/dist/cjs/providers/indexer.js +68 -1
  33. package/dist/cjs/providers/utils.js +1 -0
  34. package/dist/cjs/repositories/contractRepository.js +0 -103
  35. package/dist/cjs/repositories/inMemory/contractRepository.js +55 -0
  36. package/dist/cjs/repositories/inMemory/walletRepository.js +80 -0
  37. package/dist/cjs/repositories/index.js +16 -0
  38. package/dist/cjs/repositories/indexedDB/contractRepository.js +187 -0
  39. package/dist/cjs/repositories/indexedDB/db.js +19 -0
  40. package/dist/cjs/repositories/indexedDB/manager.js +97 -0
  41. package/dist/cjs/repositories/indexedDB/schema.js +159 -0
  42. package/dist/cjs/repositories/indexedDB/walletRepository.js +338 -0
  43. package/dist/cjs/repositories/indexedDB/websqlAdapter.js +144 -0
  44. package/dist/cjs/repositories/migrations/contractRepositoryImpl.js +127 -0
  45. package/dist/cjs/repositories/migrations/fromStorageAdapter.js +66 -0
  46. package/dist/cjs/repositories/migrations/walletRepositoryImpl.js +180 -0
  47. package/dist/cjs/repositories/realm/contractRepository.js +120 -0
  48. package/dist/cjs/repositories/realm/index.js +9 -0
  49. package/dist/cjs/repositories/realm/schemas.js +108 -0
  50. package/dist/cjs/repositories/realm/types.js +7 -0
  51. package/dist/cjs/repositories/realm/walletRepository.js +273 -0
  52. package/dist/cjs/repositories/serialization.js +49 -0
  53. package/dist/cjs/repositories/sqlite/contractRepository.js +139 -0
  54. package/dist/cjs/repositories/sqlite/index.js +7 -0
  55. package/dist/cjs/repositories/sqlite/types.js +2 -0
  56. package/dist/cjs/repositories/sqlite/walletRepository.js +328 -0
  57. package/dist/cjs/repositories/walletRepository.js +0 -169
  58. package/dist/cjs/script/base.js +54 -0
  59. package/dist/cjs/script/delegate.js +49 -0
  60. package/dist/cjs/storage/asyncStorage.js +4 -1
  61. package/dist/cjs/storage/fileSystem.js +3 -0
  62. package/dist/cjs/storage/inMemory.js +3 -0
  63. package/dist/cjs/storage/indexedDB.js +5 -1
  64. package/dist/cjs/storage/localStorage.js +3 -0
  65. package/dist/cjs/utils/arkTransaction.js +16 -0
  66. package/dist/cjs/utils/transactionHistory.js +50 -0
  67. package/dist/cjs/wallet/asset-manager.js +338 -0
  68. package/dist/cjs/wallet/asset.js +117 -0
  69. package/dist/cjs/wallet/batch.js +1 -1
  70. package/dist/cjs/wallet/delegator.js +235 -0
  71. package/dist/cjs/wallet/expo/background.js +133 -0
  72. package/dist/cjs/wallet/expo/index.js +9 -0
  73. package/dist/cjs/wallet/expo/wallet.js +231 -0
  74. package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +568 -0
  75. package/dist/cjs/wallet/serviceWorker/wallet.js +383 -102
  76. package/dist/cjs/wallet/utils.js +58 -0
  77. package/dist/cjs/wallet/validation.js +151 -0
  78. package/dist/cjs/wallet/vtxo-manager.js +8 -1
  79. package/dist/cjs/wallet/wallet.js +702 -260
  80. package/dist/cjs/worker/browser/service-worker-manager.js +82 -0
  81. package/dist/cjs/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
  82. package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +78 -0
  83. package/dist/cjs/worker/expo/index.js +12 -0
  84. package/dist/cjs/worker/expo/processors/contractPollProcessor.js +61 -0
  85. package/dist/cjs/worker/expo/processors/index.js +6 -0
  86. package/dist/cjs/worker/expo/taskQueue.js +41 -0
  87. package/dist/cjs/worker/expo/taskRunner.js +57 -0
  88. package/dist/cjs/worker/messageBus.js +252 -0
  89. package/dist/esm/asset/assetGroup.js +137 -0
  90. package/dist/esm/asset/assetId.js +84 -0
  91. package/dist/esm/asset/assetInput.js +199 -0
  92. package/dist/esm/asset/assetOutput.js +154 -0
  93. package/dist/esm/asset/assetRef.js +78 -0
  94. package/dist/esm/asset/index.js +8 -0
  95. package/dist/esm/asset/metadata.js +167 -0
  96. package/dist/esm/asset/packet.js +159 -0
  97. package/dist/esm/asset/types.js +22 -0
  98. package/dist/esm/asset/utils.js +99 -0
  99. package/dist/esm/bip322/index.js +267 -0
  100. package/dist/esm/contracts/arkcontract.js +141 -0
  101. package/dist/esm/contracts/contractManager.js +432 -0
  102. package/dist/esm/contracts/contractWatcher.js +563 -0
  103. package/dist/esm/contracts/handlers/default.js +82 -0
  104. package/dist/esm/contracts/handlers/delegate.js +86 -0
  105. package/dist/esm/contracts/handlers/helpers.js +66 -0
  106. package/dist/esm/contracts/handlers/index.js +12 -0
  107. package/dist/esm/contracts/handlers/registry.js +86 -0
  108. package/dist/esm/contracts/handlers/vhtlc.js +190 -0
  109. package/dist/esm/contracts/index.js +13 -0
  110. package/dist/esm/contracts/types.js +1 -0
  111. package/dist/esm/forfeit.js +11 -8
  112. package/dist/esm/identity/index.js +1 -0
  113. package/dist/esm/identity/seedIdentity.js +249 -0
  114. package/dist/esm/index.js +28 -15
  115. package/dist/esm/intent/index.js +44 -9
  116. package/dist/esm/providers/ark.js +7 -0
  117. package/dist/esm/providers/delegator.js +62 -0
  118. package/dist/esm/providers/expoIndexer.js +5 -0
  119. package/dist/esm/providers/indexer.js +68 -1
  120. package/dist/esm/providers/utils.js +1 -0
  121. package/dist/esm/repositories/contractRepository.js +1 -101
  122. package/dist/esm/repositories/inMemory/contractRepository.js +51 -0
  123. package/dist/esm/repositories/inMemory/walletRepository.js +76 -0
  124. package/dist/esm/repositories/index.js +8 -0
  125. package/dist/esm/repositories/indexedDB/contractRepository.js +183 -0
  126. package/dist/esm/repositories/indexedDB/db.js +4 -0
  127. package/dist/esm/repositories/indexedDB/manager.js +92 -0
  128. package/dist/esm/repositories/indexedDB/schema.js +155 -0
  129. package/dist/esm/repositories/indexedDB/walletRepository.js +334 -0
  130. package/dist/esm/repositories/indexedDB/websqlAdapter.js +138 -0
  131. package/dist/esm/repositories/migrations/contractRepositoryImpl.js +121 -0
  132. package/dist/esm/repositories/migrations/fromStorageAdapter.js +58 -0
  133. package/dist/esm/repositories/migrations/walletRepositoryImpl.js +176 -0
  134. package/dist/esm/repositories/realm/contractRepository.js +116 -0
  135. package/dist/esm/repositories/realm/index.js +3 -0
  136. package/dist/esm/repositories/realm/schemas.js +105 -0
  137. package/dist/esm/repositories/realm/types.js +6 -0
  138. package/dist/esm/repositories/realm/walletRepository.js +269 -0
  139. package/dist/esm/repositories/serialization.js +40 -0
  140. package/dist/esm/repositories/sqlite/contractRepository.js +135 -0
  141. package/dist/esm/repositories/sqlite/index.js +2 -0
  142. package/dist/esm/repositories/sqlite/types.js +1 -0
  143. package/dist/esm/repositories/sqlite/walletRepository.js +324 -0
  144. package/dist/esm/repositories/walletRepository.js +1 -167
  145. package/dist/esm/script/base.js +21 -1
  146. package/dist/esm/script/delegate.js +46 -0
  147. package/dist/esm/storage/asyncStorage.js +4 -1
  148. package/dist/esm/storage/fileSystem.js +3 -0
  149. package/dist/esm/storage/inMemory.js +3 -0
  150. package/dist/esm/storage/indexedDB.js +5 -1
  151. package/dist/esm/storage/localStorage.js +3 -0
  152. package/dist/esm/utils/arkTransaction.js +15 -0
  153. package/dist/esm/utils/transactionHistory.js +50 -0
  154. package/dist/esm/wallet/asset-manager.js +333 -0
  155. package/dist/esm/wallet/asset.js +111 -0
  156. package/dist/esm/wallet/batch.js +1 -1
  157. package/dist/esm/wallet/delegator.js +231 -0
  158. package/dist/esm/wallet/expo/background.js +128 -0
  159. package/dist/esm/wallet/expo/index.js +2 -0
  160. package/dist/esm/wallet/expo/wallet.js +194 -0
  161. package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +564 -0
  162. package/dist/esm/wallet/serviceWorker/wallet.js +382 -101
  163. package/dist/esm/wallet/utils.js +54 -0
  164. package/dist/esm/wallet/validation.js +139 -0
  165. package/dist/esm/wallet/vtxo-manager.js +8 -1
  166. package/dist/esm/wallet/wallet.js +704 -229
  167. package/dist/esm/worker/browser/service-worker-manager.js +76 -0
  168. package/dist/esm/{wallet/serviceWorker → worker/browser}/utils.js +2 -1
  169. package/dist/esm/worker/expo/asyncStorageTaskQueue.js +74 -0
  170. package/dist/esm/worker/expo/index.js +4 -0
  171. package/dist/esm/worker/expo/processors/contractPollProcessor.js +58 -0
  172. package/dist/esm/worker/expo/processors/index.js +1 -0
  173. package/dist/esm/worker/expo/taskQueue.js +37 -0
  174. package/dist/esm/worker/expo/taskRunner.js +54 -0
  175. package/dist/esm/worker/messageBus.js +248 -0
  176. package/dist/types/asset/assetGroup.d.ts +28 -0
  177. package/dist/types/asset/assetId.d.ts +19 -0
  178. package/dist/types/asset/assetInput.d.ts +46 -0
  179. package/dist/types/asset/assetOutput.d.ts +39 -0
  180. package/dist/types/asset/assetRef.d.ts +25 -0
  181. package/dist/types/asset/index.d.ts +8 -0
  182. package/dist/types/asset/metadata.d.ts +37 -0
  183. package/dist/types/asset/packet.d.ts +27 -0
  184. package/dist/types/asset/types.d.ts +18 -0
  185. package/dist/types/asset/utils.d.ts +21 -0
  186. package/dist/types/bip322/index.d.ts +55 -0
  187. package/dist/types/contracts/arkcontract.d.ts +101 -0
  188. package/dist/types/contracts/contractManager.d.ts +331 -0
  189. package/dist/types/contracts/contractWatcher.d.ts +192 -0
  190. package/dist/types/contracts/handlers/default.d.ts +19 -0
  191. package/dist/types/contracts/handlers/delegate.d.ts +21 -0
  192. package/dist/types/contracts/handlers/helpers.d.ts +18 -0
  193. package/dist/types/contracts/handlers/index.d.ts +7 -0
  194. package/dist/types/contracts/handlers/registry.d.ts +65 -0
  195. package/dist/types/contracts/handlers/vhtlc.d.ts +32 -0
  196. package/dist/types/contracts/index.d.ts +14 -0
  197. package/dist/types/contracts/types.d.ts +222 -0
  198. package/dist/types/forfeit.d.ts +2 -1
  199. package/dist/types/identity/index.d.ts +1 -0
  200. package/dist/types/identity/seedIdentity.d.ts +128 -0
  201. package/dist/types/index.d.ts +22 -12
  202. package/dist/types/intent/index.d.ts +15 -1
  203. package/dist/types/providers/ark.d.ts +11 -2
  204. package/dist/types/providers/delegator.d.ts +29 -0
  205. package/dist/types/providers/indexer.d.ts +11 -1
  206. package/dist/types/repositories/contractRepository.d.ts +30 -19
  207. package/dist/types/repositories/inMemory/contractRepository.d.ts +17 -0
  208. package/dist/types/repositories/inMemory/walletRepository.d.ts +26 -0
  209. package/dist/types/repositories/index.d.ts +7 -0
  210. package/dist/types/repositories/indexedDB/contractRepository.d.ts +21 -0
  211. package/dist/types/repositories/indexedDB/db.d.ts +4 -0
  212. package/dist/types/repositories/indexedDB/manager.d.ts +22 -0
  213. package/dist/types/repositories/indexedDB/schema.d.ts +8 -0
  214. package/dist/types/repositories/indexedDB/walletRepository.d.ts +25 -0
  215. package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +49 -0
  216. package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +24 -0
  217. package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +19 -0
  218. package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +27 -0
  219. package/dist/types/repositories/realm/contractRepository.d.ts +24 -0
  220. package/dist/types/repositories/realm/index.d.ts +4 -0
  221. package/dist/types/repositories/realm/schemas.d.ts +208 -0
  222. package/dist/types/repositories/realm/types.d.ts +16 -0
  223. package/dist/types/repositories/realm/walletRepository.d.ts +31 -0
  224. package/dist/types/repositories/serialization.d.ts +40 -0
  225. package/dist/types/repositories/sqlite/contractRepository.d.ts +33 -0
  226. package/dist/types/repositories/sqlite/index.d.ts +3 -0
  227. package/dist/types/repositories/sqlite/types.d.ts +18 -0
  228. package/dist/types/repositories/sqlite/walletRepository.d.ts +40 -0
  229. package/dist/types/repositories/walletRepository.d.ts +13 -24
  230. package/dist/types/script/base.d.ts +1 -0
  231. package/dist/types/script/delegate.d.ts +36 -0
  232. package/dist/types/storage/asyncStorage.d.ts +4 -0
  233. package/dist/types/storage/fileSystem.d.ts +3 -0
  234. package/dist/types/storage/inMemory.d.ts +3 -0
  235. package/dist/types/storage/index.d.ts +3 -0
  236. package/dist/types/storage/indexedDB.d.ts +3 -0
  237. package/dist/types/storage/localStorage.d.ts +3 -0
  238. package/dist/types/utils/arkTransaction.d.ts +6 -0
  239. package/dist/types/wallet/asset-manager.d.ts +78 -0
  240. package/dist/types/wallet/asset.d.ts +21 -0
  241. package/dist/types/wallet/batch.d.ts +1 -1
  242. package/dist/types/wallet/delegator.d.ts +24 -0
  243. package/dist/types/wallet/expo/background.d.ts +66 -0
  244. package/dist/types/wallet/expo/index.d.ts +4 -0
  245. package/dist/types/wallet/expo/wallet.d.ts +97 -0
  246. package/dist/types/wallet/index.d.ts +75 -2
  247. package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +366 -0
  248. package/dist/types/wallet/serviceWorker/wallet.d.ts +20 -11
  249. package/dist/types/wallet/utils.d.ts +12 -1
  250. package/dist/types/wallet/validation.d.ts +24 -0
  251. package/dist/types/wallet/wallet.d.ts +111 -17
  252. package/dist/types/worker/browser/service-worker-manager.d.ts +21 -0
  253. package/dist/types/{wallet/serviceWorker → worker/browser}/utils.d.ts +2 -1
  254. package/dist/types/worker/expo/asyncStorageTaskQueue.d.ts +46 -0
  255. package/dist/types/worker/expo/index.d.ts +7 -0
  256. package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +14 -0
  257. package/dist/types/worker/expo/processors/index.d.ts +1 -0
  258. package/dist/types/worker/expo/taskQueue.d.ts +50 -0
  259. package/dist/types/worker/expo/taskRunner.d.ts +42 -0
  260. package/dist/types/worker/messageBus.d.ts +109 -0
  261. package/package.json +69 -11
  262. package/dist/cjs/wallet/serviceWorker/request.js +0 -78
  263. package/dist/cjs/wallet/serviceWorker/response.js +0 -222
  264. package/dist/cjs/wallet/serviceWorker/worker.js +0 -655
  265. package/dist/esm/wallet/serviceWorker/request.js +0 -75
  266. package/dist/esm/wallet/serviceWorker/response.js +0 -219
  267. package/dist/esm/wallet/serviceWorker/worker.js +0 -651
  268. package/dist/types/wallet/serviceWorker/request.d.ts +0 -74
  269. package/dist/types/wallet/serviceWorker/response.d.ts +0 -123
  270. package/dist/types/wallet/serviceWorker/worker.d.ts +0 -53
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssetGroup = void 0;
4
+ const base_1 = require("@scure/base");
5
+ const types_1 = require("./types");
6
+ const assetId_1 = require("./assetId");
7
+ const assetRef_1 = require("./assetRef");
8
+ const assetInput_1 = require("./assetInput");
9
+ const assetOutput_1 = require("./assetOutput");
10
+ const metadata_1 = require("./metadata");
11
+ const utils_1 = require("./utils");
12
+ /**
13
+ * An asset group contains inputs/outputs and all data related to a given asset id.
14
+ */
15
+ class AssetGroup {
16
+ constructor(assetId, controlAsset, inputs, outputs, metadata) {
17
+ this.assetId = assetId;
18
+ this.controlAsset = controlAsset;
19
+ this.inputs = inputs;
20
+ this.outputs = outputs;
21
+ this.metadataList = new metadata_1.MetadataList(metadata);
22
+ }
23
+ static create(assetId, controlAsset, inputs, outputs, metadata) {
24
+ const ag = new AssetGroup(assetId, controlAsset, inputs, outputs, metadata);
25
+ ag.validate();
26
+ return ag;
27
+ }
28
+ // from hex encoded
29
+ static fromString(s) {
30
+ let buf;
31
+ try {
32
+ buf = base_1.hex.decode(s);
33
+ }
34
+ catch {
35
+ throw new Error("invalid format, must be hex");
36
+ }
37
+ return AssetGroup.fromBytes(buf);
38
+ }
39
+ static fromBytes(buf) {
40
+ if (!buf || buf.length === 0) {
41
+ throw new Error("missing asset group");
42
+ }
43
+ const reader = new utils_1.BufferReader(buf);
44
+ return AssetGroup.fromReader(reader);
45
+ }
46
+ // an issuance is a group with null assetId
47
+ isIssuance() {
48
+ return this.assetId === null;
49
+ }
50
+ // a reissuance is a group that is not an issuance
51
+ // but where the sum of the outputs is greater than the sum of the inputs
52
+ isReissuance() {
53
+ const sumReducer = (s, { amount }) => s + amount;
54
+ const sumOutputs = this.outputs.reduce(sumReducer, 0n);
55
+ const sumInputs = this.inputs
56
+ .map((i) => ({
57
+ amount: i.input.type === types_1.AssetInputType.Local ? i.input.amount : 0n,
58
+ }))
59
+ .reduce(sumReducer, 0n);
60
+ return !this.isIssuance() && sumInputs < sumOutputs;
61
+ }
62
+ serialize() {
63
+ this.validate();
64
+ const writer = new utils_1.BufferWriter();
65
+ this.serializeTo(writer);
66
+ return writer.toBytes();
67
+ }
68
+ validate() {
69
+ if (this.inputs.length === 0 && this.outputs.length === 0) {
70
+ throw new Error("empty asset group");
71
+ }
72
+ if (this.isIssuance()) {
73
+ if (this.inputs.length !== 0) {
74
+ throw new Error("issuance must have no inputs");
75
+ }
76
+ }
77
+ else {
78
+ if (this.controlAsset !== null) {
79
+ throw new Error("only issuance can have a control asset");
80
+ }
81
+ }
82
+ }
83
+ toBatchLeafAssetGroup(intentTxid) {
84
+ const leafInput = assetInput_1.AssetInput.createIntent(base_1.hex.encode(intentTxid), 0, 0);
85
+ return new AssetGroup(this.assetId, this.controlAsset, [leafInput], this.outputs, this.metadataList.items);
86
+ }
87
+ toString() {
88
+ return base_1.hex.encode(this.serialize());
89
+ }
90
+ static fromReader(reader) {
91
+ const presence = reader.readByte();
92
+ let assetId = null;
93
+ let controlAsset = null;
94
+ let metadata = [];
95
+ if (presence & types_1.MASK_ASSET_ID) {
96
+ assetId = assetId_1.AssetId.fromReader(reader);
97
+ }
98
+ if (presence & types_1.MASK_CONTROL_ASSET) {
99
+ controlAsset = assetRef_1.AssetRef.fromReader(reader);
100
+ }
101
+ if (presence & types_1.MASK_METADATA) {
102
+ metadata = metadata_1.MetadataList.fromReader(reader).items;
103
+ }
104
+ const inputs = assetInput_1.AssetInputs.fromReader(reader);
105
+ const outputs = assetOutput_1.AssetOutputs.fromReader(reader);
106
+ const ag = new AssetGroup(assetId, controlAsset, inputs.inputs, outputs.outputs, metadata);
107
+ ag.validate();
108
+ return ag;
109
+ }
110
+ serializeTo(writer) {
111
+ let presence = 0;
112
+ if (this.assetId !== null) {
113
+ presence |= types_1.MASK_ASSET_ID;
114
+ }
115
+ if (this.controlAsset !== null) {
116
+ presence |= types_1.MASK_CONTROL_ASSET;
117
+ }
118
+ if (this.metadataList.length > 0) {
119
+ presence |= types_1.MASK_METADATA;
120
+ }
121
+ writer.writeByte(presence);
122
+ if (presence & types_1.MASK_ASSET_ID) {
123
+ this.assetId.serializeTo(writer);
124
+ }
125
+ if (presence & types_1.MASK_CONTROL_ASSET) {
126
+ this.controlAsset.serializeTo(writer);
127
+ }
128
+ if (presence & types_1.MASK_METADATA) {
129
+ this.metadataList.serializeTo(writer);
130
+ }
131
+ writer.writeVarUint(this.inputs.length);
132
+ for (const input of this.inputs) {
133
+ input.serializeTo(writer);
134
+ }
135
+ writer.writeVarUint(this.outputs.length);
136
+ for (const output of this.outputs) {
137
+ output.serializeTo(writer);
138
+ }
139
+ }
140
+ }
141
+ exports.AssetGroup = AssetGroup;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssetId = void 0;
4
+ const base_1 = require("@scure/base");
5
+ const types_1 = require("./types");
6
+ const utils_1 = require("./utils");
7
+ /**
8
+ * AssetId represents the id of an asset.
9
+ * @param txid - the genesis transaction id (decoded from hex)
10
+ * @param groupIndex - the asset group index in the genesis transaction
11
+ */
12
+ class AssetId {
13
+ constructor(txid, groupIndex) {
14
+ this.txid = txid;
15
+ this.groupIndex = groupIndex;
16
+ }
17
+ static create(txid, groupIndex) {
18
+ if (!txid) {
19
+ throw new Error("missing txid");
20
+ }
21
+ let buf;
22
+ try {
23
+ buf = base_1.hex.decode(txid);
24
+ }
25
+ catch {
26
+ throw new Error("invalid txid format, must be hex");
27
+ }
28
+ if (buf.length !== types_1.TX_HASH_SIZE) {
29
+ throw new Error(`invalid txid length: got ${buf.length} bytes, want ${types_1.TX_HASH_SIZE} bytes`);
30
+ }
31
+ const assetId = new AssetId(buf, groupIndex);
32
+ assetId.validate();
33
+ return assetId;
34
+ }
35
+ static fromString(s) {
36
+ let buf;
37
+ try {
38
+ buf = base_1.hex.decode(s);
39
+ }
40
+ catch {
41
+ throw new Error("invalid asset id format, must be hex");
42
+ }
43
+ return AssetId.fromBytes(buf);
44
+ }
45
+ static fromBytes(buf) {
46
+ if (!buf || buf.length === 0) {
47
+ throw new Error("missing asset id");
48
+ }
49
+ if (buf.length !== types_1.ASSET_ID_SIZE) {
50
+ throw new Error(`invalid asset id length: got ${buf.length} bytes, want ${types_1.ASSET_ID_SIZE} bytes`);
51
+ }
52
+ const reader = new utils_1.BufferReader(buf);
53
+ return AssetId.fromReader(reader);
54
+ }
55
+ serialize() {
56
+ const writer = new utils_1.BufferWriter();
57
+ this.serializeTo(writer);
58
+ return writer.toBytes();
59
+ }
60
+ toString() {
61
+ return base_1.hex.encode(this.serialize());
62
+ }
63
+ validate() {
64
+ if ((0, utils_1.isZeroBytes)(this.txid)) {
65
+ throw new Error("empty txid");
66
+ }
67
+ if (!Number.isInteger(this.groupIndex) ||
68
+ this.groupIndex < 0 ||
69
+ this.groupIndex > 0xffff) {
70
+ throw new Error(`invalid group index: ${this.groupIndex}, must be in range [0, 65535]`);
71
+ }
72
+ }
73
+ static fromReader(reader) {
74
+ if (reader.remaining() < types_1.ASSET_ID_SIZE) {
75
+ throw new Error(`invalid asset id length: got ${reader.remaining()}, want ${types_1.ASSET_ID_SIZE}`);
76
+ }
77
+ const txid = reader.readSlice(types_1.TX_HASH_SIZE);
78
+ const index = reader.readUint16LE();
79
+ const assetId = new AssetId(txid, index);
80
+ assetId.validate();
81
+ return assetId;
82
+ }
83
+ serializeTo(writer) {
84
+ writer.write(this.txid);
85
+ writer.writeUint16LE(this.groupIndex);
86
+ }
87
+ }
88
+ exports.AssetId = AssetId;
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssetInputs = exports.AssetInput = void 0;
4
+ const base_1 = require("@scure/base");
5
+ const types_1 = require("./types");
6
+ const utils_1 = require("./utils");
7
+ /**
8
+ * AssetInput represents an input of an asset group.
9
+ * a local input references a real transaction input and specify the amount in satoshis.
10
+ * an intent input references an external intent transaction. It is created by the server to handle batch leaf transaction.
11
+ */
12
+ class AssetInput {
13
+ constructor(input) {
14
+ this.input = input;
15
+ }
16
+ get vin() {
17
+ return this.input.vin;
18
+ }
19
+ get amount() {
20
+ return this.input.amount;
21
+ }
22
+ static create(vin, amount) {
23
+ const input = new AssetInput({
24
+ type: types_1.AssetInputType.Local,
25
+ vin,
26
+ amount: typeof amount === "number" ? BigInt(amount) : amount,
27
+ });
28
+ input.validate();
29
+ return input;
30
+ }
31
+ static createIntent(txid, vin, amount) {
32
+ if (!txid || txid.length === 0) {
33
+ throw new Error("missing input intent txid");
34
+ }
35
+ let buf;
36
+ try {
37
+ buf = base_1.hex.decode(txid);
38
+ }
39
+ catch {
40
+ throw new Error("invalid input intent txid format, must be hex");
41
+ }
42
+ if (buf.length !== types_1.TX_HASH_SIZE) {
43
+ throw new Error("invalid input intent txid length");
44
+ }
45
+ const input = new AssetInput({
46
+ type: types_1.AssetInputType.Intent,
47
+ txid: buf,
48
+ vin,
49
+ amount: typeof amount === "number" ? BigInt(amount) : amount,
50
+ });
51
+ input.validate();
52
+ return input;
53
+ }
54
+ static fromString(s) {
55
+ let buf;
56
+ try {
57
+ buf = base_1.hex.decode(s);
58
+ }
59
+ catch {
60
+ throw new Error("invalid format, must be hex");
61
+ }
62
+ return AssetInput.fromBytes(buf);
63
+ }
64
+ static fromBytes(buf) {
65
+ const reader = new utils_1.BufferReader(buf);
66
+ return AssetInput.fromReader(reader);
67
+ }
68
+ serialize() {
69
+ const writer = new utils_1.BufferWriter();
70
+ this.serializeTo(writer);
71
+ return writer.toBytes();
72
+ }
73
+ toString() {
74
+ return base_1.hex.encode(this.serialize());
75
+ }
76
+ validate() {
77
+ switch (this.input.type) {
78
+ case types_1.AssetInputType.Local:
79
+ break;
80
+ case types_1.AssetInputType.Intent:
81
+ if ((0, utils_1.isZeroBytes)(this.input.txid)) {
82
+ throw new Error("missing input intent txid");
83
+ }
84
+ break;
85
+ }
86
+ }
87
+ static fromReader(reader) {
88
+ const type = reader.readByte();
89
+ let input;
90
+ switch (type) {
91
+ case types_1.AssetInputType.Local: {
92
+ const vin = reader.readUint16LE();
93
+ const amount = reader.readVarUint();
94
+ input = new AssetInput({
95
+ type: types_1.AssetInputType.Local,
96
+ vin,
97
+ amount,
98
+ });
99
+ break;
100
+ }
101
+ case types_1.AssetInputType.Intent: {
102
+ if (reader.remaining() < types_1.TX_HASH_SIZE) {
103
+ throw new Error("invalid input intent txid length");
104
+ }
105
+ const txid = reader.readSlice(types_1.TX_HASH_SIZE);
106
+ const vin = reader.readUint16LE();
107
+ const amount = reader.readVarUint();
108
+ input = new AssetInput({
109
+ type: types_1.AssetInputType.Intent,
110
+ txid: new Uint8Array(txid),
111
+ vin,
112
+ amount,
113
+ });
114
+ break;
115
+ }
116
+ case types_1.AssetInputType.Unspecified:
117
+ throw new Error("asset input type unspecified");
118
+ default:
119
+ throw new Error(`asset input type ${type} unknown`);
120
+ }
121
+ input.validate();
122
+ return input;
123
+ }
124
+ serializeTo(writer) {
125
+ writer.writeByte(this.input.type);
126
+ if (this.input.type === types_1.AssetInputType.Intent) {
127
+ writer.write(this.input.txid);
128
+ }
129
+ writer.writeUint16LE(this.input.vin);
130
+ writer.writeVarUint(this.input.amount);
131
+ }
132
+ }
133
+ exports.AssetInput = AssetInput;
134
+ /**
135
+ * AssetInputs represents a list of asset inputs.
136
+ */
137
+ class AssetInputs {
138
+ constructor(inputs) {
139
+ this.inputs = inputs;
140
+ }
141
+ static create(inputs) {
142
+ const list = new AssetInputs(inputs);
143
+ list.validate();
144
+ return list;
145
+ }
146
+ static fromString(s) {
147
+ if (!s || s.length === 0) {
148
+ throw new Error("missing asset inputs");
149
+ }
150
+ let buf;
151
+ try {
152
+ buf = base_1.hex.decode(s);
153
+ }
154
+ catch {
155
+ throw new Error("invalid asset inputs format, must be hex");
156
+ }
157
+ const reader = new utils_1.BufferReader(buf);
158
+ return AssetInputs.fromReader(reader);
159
+ }
160
+ serialize() {
161
+ const writer = new utils_1.BufferWriter();
162
+ this.serializeTo(writer);
163
+ return writer.toBytes();
164
+ }
165
+ toString() {
166
+ return base_1.hex.encode(this.serialize());
167
+ }
168
+ validate() {
169
+ const seen = new Set();
170
+ let listType = types_1.AssetInputType.Unspecified;
171
+ for (const assetInput of this.inputs) {
172
+ assetInput.validate();
173
+ if (listType === types_1.AssetInputType.Unspecified) {
174
+ listType = assetInput.input.type;
175
+ }
176
+ else if (listType !== assetInput.input.type) {
177
+ throw new Error("all inputs must be of the same type");
178
+ }
179
+ // verify the same input vin is not duplicated
180
+ if (assetInput.input.type === types_1.AssetInputType.Local) {
181
+ if (seen.has(assetInput.input.vin)) {
182
+ throw new Error(`duplicated input vin ${assetInput.input.vin}`);
183
+ }
184
+ seen.add(assetInput.input.vin);
185
+ continue;
186
+ }
187
+ }
188
+ }
189
+ static fromReader(reader) {
190
+ const count = Number(reader.readVarUint());
191
+ const inputs = [];
192
+ for (let i = 0; i < count; i++) {
193
+ inputs.push(AssetInput.fromReader(reader));
194
+ }
195
+ return AssetInputs.create(inputs);
196
+ }
197
+ serializeTo(writer) {
198
+ writer.writeVarUint(this.inputs.length);
199
+ for (const input of this.inputs) {
200
+ input.serializeTo(writer);
201
+ }
202
+ }
203
+ }
204
+ exports.AssetInputs = AssetInputs;
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssetOutputs = exports.AssetOutput = void 0;
4
+ const base_1 = require("@scure/base");
5
+ const utils_1 = require("./utils");
6
+ /**
7
+ * AssetOutput references a real transaction output and specify the amount in satoshis.
8
+ * it must be present in an AssetGroup.
9
+ *
10
+ * @param vout - the output index in the transaction
11
+ * @param amount - asset amount in satoshis
12
+ */
13
+ class AssetOutput {
14
+ constructor(vout, amount) {
15
+ this.vout = vout;
16
+ this.amount = amount;
17
+ }
18
+ static create(vout, amount) {
19
+ const output = new AssetOutput(vout, typeof amount === "number" ? BigInt(amount) : amount);
20
+ output.validate();
21
+ return output;
22
+ }
23
+ static fromString(s) {
24
+ let buf;
25
+ try {
26
+ buf = base_1.hex.decode(s);
27
+ }
28
+ catch {
29
+ throw new Error("invalid asset output format, must be hex");
30
+ }
31
+ return AssetOutput.fromBytes(buf);
32
+ }
33
+ static fromBytes(buf) {
34
+ if (!buf || buf.length === 0) {
35
+ throw new Error("missing asset output");
36
+ }
37
+ const reader = new utils_1.BufferReader(buf);
38
+ const output = AssetOutput.fromReader(reader);
39
+ output.validate();
40
+ return output;
41
+ }
42
+ serialize() {
43
+ const writer = new utils_1.BufferWriter();
44
+ this.serializeTo(writer);
45
+ return writer.toBytes();
46
+ }
47
+ toString() {
48
+ return base_1.hex.encode(this.serialize());
49
+ }
50
+ validate() {
51
+ if (!Number.isInteger(this.vout) ||
52
+ this.vout < 0 ||
53
+ this.vout > 0xffff) {
54
+ throw new Error("asset output vout must be an integer in range [0, 65535]");
55
+ }
56
+ if (this.amount <= 0n) {
57
+ throw new Error("asset output amount must be greater than 0");
58
+ }
59
+ }
60
+ static fromReader(reader) {
61
+ if (reader.remaining() < 2) {
62
+ throw new Error("invalid asset output vout length");
63
+ }
64
+ const type = reader.readByte();
65
+ if (type !== AssetOutput.TYPE_LOCAL) {
66
+ if (type === 0x00) {
67
+ throw new Error("output type unspecified");
68
+ }
69
+ throw new Error("unknown asset output type");
70
+ }
71
+ let vout;
72
+ try {
73
+ vout = reader.readUint16LE();
74
+ }
75
+ catch {
76
+ throw new Error("invalid asset output vout length");
77
+ }
78
+ const amount = reader.readVarUint();
79
+ return new AssetOutput(vout, amount);
80
+ }
81
+ serializeTo(writer) {
82
+ writer.writeByte(0x01);
83
+ writer.writeUint16LE(this.vout);
84
+ writer.writeVarUint(this.amount);
85
+ }
86
+ }
87
+ exports.AssetOutput = AssetOutput;
88
+ // 0x01 means local output, there is only 1 local output type currently
89
+ // however we serialize it for future upgrades
90
+ AssetOutput.TYPE_LOCAL = 0x01;
91
+ /**
92
+ * AssetOutputs is a list of AssetOutput references.
93
+ * it must be present in an AssetGroup.
94
+ *
95
+ * @param outputs - the list of asset outputs
96
+ */
97
+ class AssetOutputs {
98
+ constructor(outputs) {
99
+ this.outputs = outputs;
100
+ }
101
+ static create(outputs) {
102
+ const list = new AssetOutputs(outputs);
103
+ list.validate();
104
+ return list;
105
+ }
106
+ static fromString(s) {
107
+ if (!s || s.length === 0) {
108
+ throw new Error("missing asset outputs");
109
+ }
110
+ let buf;
111
+ try {
112
+ buf = base_1.hex.decode(s);
113
+ }
114
+ catch {
115
+ throw new Error("invalid asset outputs format, must be hex");
116
+ }
117
+ const reader = new utils_1.BufferReader(buf);
118
+ return AssetOutputs.fromReader(reader);
119
+ }
120
+ serialize() {
121
+ const writer = new utils_1.BufferWriter();
122
+ this.serializeTo(writer);
123
+ return writer.toBytes();
124
+ }
125
+ toString() {
126
+ return base_1.hex.encode(this.serialize());
127
+ }
128
+ validate() {
129
+ const seen = new Set();
130
+ for (const output of this.outputs) {
131
+ output.validate();
132
+ if (seen.has(output.vout)) {
133
+ throw new Error(`duplicated output vout ${output.vout}`);
134
+ }
135
+ seen.add(output.vout);
136
+ }
137
+ }
138
+ static fromReader(reader) {
139
+ const count = Number(reader.readVarUint());
140
+ if (count === 0) {
141
+ return new AssetOutputs([]);
142
+ }
143
+ const outputs = [];
144
+ for (let i = 0; i < count; i++) {
145
+ outputs.push(AssetOutput.fromReader(reader));
146
+ }
147
+ const result = new AssetOutputs(outputs);
148
+ result.validate();
149
+ return result;
150
+ }
151
+ serializeTo(writer) {
152
+ this.validate();
153
+ writer.writeVarUint(this.outputs.length);
154
+ for (const output of this.outputs) {
155
+ output.serializeTo(writer);
156
+ }
157
+ }
158
+ }
159
+ exports.AssetOutputs = AssetOutputs;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssetRef = void 0;
4
+ const base_1 = require("@scure/base");
5
+ const types_1 = require("./types");
6
+ const assetId_1 = require("./assetId");
7
+ const utils_1 = require("./utils");
8
+ class AssetRef {
9
+ constructor(ref) {
10
+ this.ref = ref;
11
+ }
12
+ get type() {
13
+ return this.ref.type;
14
+ }
15
+ static fromId(assetId) {
16
+ return new AssetRef({ type: types_1.AssetRefType.ByID, assetId });
17
+ }
18
+ static fromGroupIndex(groupIndex) {
19
+ return new AssetRef({ type: types_1.AssetRefType.ByGroup, groupIndex });
20
+ }
21
+ static fromString(s) {
22
+ let buf;
23
+ try {
24
+ buf = base_1.hex.decode(s);
25
+ }
26
+ catch {
27
+ throw new Error("invalid asset ref format, must be hex");
28
+ }
29
+ return AssetRef.fromBytes(buf);
30
+ }
31
+ static fromBytes(buf) {
32
+ if (!buf || buf.length === 0) {
33
+ throw new Error("missing asset ref");
34
+ }
35
+ const reader = new utils_1.BufferReader(buf);
36
+ return AssetRef.fromReader(reader);
37
+ }
38
+ serialize() {
39
+ const writer = new utils_1.BufferWriter();
40
+ this.serializeTo(writer);
41
+ return writer.toBytes();
42
+ }
43
+ toString() {
44
+ return base_1.hex.encode(this.serialize());
45
+ }
46
+ static fromReader(reader) {
47
+ const type = reader.readByte();
48
+ let ref;
49
+ switch (type) {
50
+ case types_1.AssetRefType.ByID: {
51
+ const assetId = assetId_1.AssetId.fromReader(reader);
52
+ ref = new AssetRef({ type: types_1.AssetRefType.ByID, assetId });
53
+ break;
54
+ }
55
+ case types_1.AssetRefType.ByGroup: {
56
+ if (reader.remaining() < 2) {
57
+ throw new Error("invalid asset ref length");
58
+ }
59
+ const groupIndex = reader.readUint16LE();
60
+ ref = new AssetRef({ type: types_1.AssetRefType.ByGroup, groupIndex });
61
+ break;
62
+ }
63
+ case types_1.AssetRefType.Unspecified:
64
+ throw new Error("asset ref type unspecified");
65
+ default:
66
+ throw new Error(`asset ref type unknown ${type}`);
67
+ }
68
+ return ref;
69
+ }
70
+ serializeTo(writer) {
71
+ writer.writeByte(this.ref.type);
72
+ switch (this.ref.type) {
73
+ case types_1.AssetRefType.ByID:
74
+ this.ref.assetId.serializeTo(writer);
75
+ break;
76
+ case types_1.AssetRefType.ByGroup:
77
+ writer.writeUint16LE(this.ref.groupIndex);
78
+ break;
79
+ }
80
+ }
81
+ }
82
+ exports.AssetRef = AssetRef;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssetPacketNotFoundError = exports.Packet = exports.AssetGroup = exports.MetadataList = exports.Metadata = exports.AssetOutputs = exports.AssetOutput = exports.AssetInputs = exports.AssetInput = exports.AssetRef = exports.AssetId = exports.AssetRefType = exports.AssetInputType = void 0;
4
+ var types_1 = require("./types");
5
+ Object.defineProperty(exports, "AssetInputType", { enumerable: true, get: function () { return types_1.AssetInputType; } });
6
+ Object.defineProperty(exports, "AssetRefType", { enumerable: true, get: function () { return types_1.AssetRefType; } });
7
+ var assetId_1 = require("./assetId");
8
+ Object.defineProperty(exports, "AssetId", { enumerable: true, get: function () { return assetId_1.AssetId; } });
9
+ var assetRef_1 = require("./assetRef");
10
+ Object.defineProperty(exports, "AssetRef", { enumerable: true, get: function () { return assetRef_1.AssetRef; } });
11
+ var assetInput_1 = require("./assetInput");
12
+ Object.defineProperty(exports, "AssetInput", { enumerable: true, get: function () { return assetInput_1.AssetInput; } });
13
+ Object.defineProperty(exports, "AssetInputs", { enumerable: true, get: function () { return assetInput_1.AssetInputs; } });
14
+ var assetOutput_1 = require("./assetOutput");
15
+ Object.defineProperty(exports, "AssetOutput", { enumerable: true, get: function () { return assetOutput_1.AssetOutput; } });
16
+ Object.defineProperty(exports, "AssetOutputs", { enumerable: true, get: function () { return assetOutput_1.AssetOutputs; } });
17
+ var metadata_1 = require("./metadata");
18
+ Object.defineProperty(exports, "Metadata", { enumerable: true, get: function () { return metadata_1.Metadata; } });
19
+ Object.defineProperty(exports, "MetadataList", { enumerable: true, get: function () { return metadata_1.MetadataList; } });
20
+ var assetGroup_1 = require("./assetGroup");
21
+ Object.defineProperty(exports, "AssetGroup", { enumerable: true, get: function () { return assetGroup_1.AssetGroup; } });
22
+ var packet_1 = require("./packet");
23
+ Object.defineProperty(exports, "Packet", { enumerable: true, get: function () { return packet_1.Packet; } });
24
+ Object.defineProperty(exports, "AssetPacketNotFoundError", { enumerable: true, get: function () { return packet_1.AssetPacketNotFoundError; } });