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