@rialo/ts-cdk 0.2.0-alpha.0 → 0.2.0-alpha.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/dist/index.d.mts +785 -13
- package/dist/index.d.ts +785 -13
- package/dist/index.js +1844 -252
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1815 -253
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var chacha20poly1305 = require('@hpke/chacha20poly1305');
|
|
4
|
+
var core = require('@hpke/core');
|
|
5
|
+
|
|
3
6
|
var __create = Object.create;
|
|
4
7
|
var __defProp = Object.defineProperty;
|
|
5
8
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -5480,6 +5483,302 @@ var RialoError = class _RialoError extends Error {
|
|
|
5480
5483
|
}
|
|
5481
5484
|
};
|
|
5482
5485
|
|
|
5486
|
+
// src/rex/errors.ts
|
|
5487
|
+
var HpkeErrorCode = /* @__PURE__ */ ((HpkeErrorCode2) => {
|
|
5488
|
+
HpkeErrorCode2["INVALID_KEY_LENGTH"] = "INVALID_KEY_LENGTH";
|
|
5489
|
+
HpkeErrorCode2["CIPHERTEXT_TOO_SHORT"] = "CIPHERTEXT_TOO_SHORT";
|
|
5490
|
+
HpkeErrorCode2["ENCRYPTION_FAILED"] = "ENCRYPTION_FAILED";
|
|
5491
|
+
HpkeErrorCode2["BORSH_DESERIALIZE_FAILED"] = "BORSH_DESERIALIZE_FAILED";
|
|
5492
|
+
HpkeErrorCode2["INVALID_ORACLE_VALUE"] = "INVALID_ORACLE_VALUE";
|
|
5493
|
+
return HpkeErrorCode2;
|
|
5494
|
+
})(HpkeErrorCode || {});
|
|
5495
|
+
var HpkeError = class _HpkeError extends Error {
|
|
5496
|
+
code;
|
|
5497
|
+
cause;
|
|
5498
|
+
constructor(code, message, cause) {
|
|
5499
|
+
super(message);
|
|
5500
|
+
this.name = "HpkeError";
|
|
5501
|
+
this.code = code;
|
|
5502
|
+
this.cause = cause;
|
|
5503
|
+
if (Error.captureStackTrace) {
|
|
5504
|
+
Error.captureStackTrace(this, _HpkeError);
|
|
5505
|
+
}
|
|
5506
|
+
}
|
|
5507
|
+
/**
|
|
5508
|
+
* Create an error for invalid key length.
|
|
5509
|
+
*
|
|
5510
|
+
* @param expected - Expected key length in bytes
|
|
5511
|
+
* @param actual - Actual key length in bytes
|
|
5512
|
+
* @param keyType - Description of the key type (e.g., "REX public key")
|
|
5513
|
+
*/
|
|
5514
|
+
static invalidKeyLength(expected, actual, keyType) {
|
|
5515
|
+
return new _HpkeError(
|
|
5516
|
+
"INVALID_KEY_LENGTH" /* INVALID_KEY_LENGTH */,
|
|
5517
|
+
`Invalid ${keyType} length: expected ${expected} bytes, got ${actual}`
|
|
5518
|
+
);
|
|
5519
|
+
}
|
|
5520
|
+
/**
|
|
5521
|
+
* Create an error for ciphertext that is too short.
|
|
5522
|
+
*
|
|
5523
|
+
* @param minLength - Minimum required length
|
|
5524
|
+
* @param actual - Actual length
|
|
5525
|
+
*/
|
|
5526
|
+
static ciphertextTooShort(minLength, actual) {
|
|
5527
|
+
return new _HpkeError(
|
|
5528
|
+
"CIPHERTEXT_TOO_SHORT" /* CIPHERTEXT_TOO_SHORT */,
|
|
5529
|
+
`Ciphertext too short: minimum ${minLength} bytes required, got ${actual}`
|
|
5530
|
+
);
|
|
5531
|
+
}
|
|
5532
|
+
/**
|
|
5533
|
+
* Create an error for encryption failure.
|
|
5534
|
+
*
|
|
5535
|
+
* @param cause - The underlying error
|
|
5536
|
+
*/
|
|
5537
|
+
static encryptionFailed(cause) {
|
|
5538
|
+
return new _HpkeError(
|
|
5539
|
+
"ENCRYPTION_FAILED" /* ENCRYPTION_FAILED */,
|
|
5540
|
+
`HPKE encryption failed: ${cause.message}`,
|
|
5541
|
+
cause
|
|
5542
|
+
);
|
|
5543
|
+
}
|
|
5544
|
+
/**
|
|
5545
|
+
* Create an error for Borsh deserialization failure.
|
|
5546
|
+
*
|
|
5547
|
+
* @param cause - The underlying error
|
|
5548
|
+
*/
|
|
5549
|
+
static borshDeserializeFailed(cause) {
|
|
5550
|
+
return new _HpkeError(
|
|
5551
|
+
"BORSH_DESERIALIZE_FAILED" /* BORSH_DESERIALIZE_FAILED */,
|
|
5552
|
+
`Borsh deserialization failed: ${cause.message}`,
|
|
5553
|
+
cause
|
|
5554
|
+
);
|
|
5555
|
+
}
|
|
5556
|
+
/**
|
|
5557
|
+
* Create an error for invalid RexValue variant.
|
|
5558
|
+
*
|
|
5559
|
+
* @param variant - The invalid variant byte
|
|
5560
|
+
*/
|
|
5561
|
+
static invalidRexValue(variant) {
|
|
5562
|
+
return new _HpkeError(
|
|
5563
|
+
"INVALID_ORACLE_VALUE" /* INVALID_ORACLE_VALUE */,
|
|
5564
|
+
`Invalid RexValue variant: ${variant}`
|
|
5565
|
+
);
|
|
5566
|
+
}
|
|
5567
|
+
};
|
|
5568
|
+
|
|
5569
|
+
// src/rex/constants.ts
|
|
5570
|
+
var USER_SECRET_AAD = new TextEncoder().encode("rex-secret-v1");
|
|
5571
|
+
var SECRET_SHARING_HPKE_INFO = new TextEncoder().encode(
|
|
5572
|
+
"rialo/tee/secret-sharing-hpke/v1"
|
|
5573
|
+
);
|
|
5574
|
+
var X25519_PUBLIC_KEY_LENGTH = 32;
|
|
5575
|
+
var ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
5576
|
+
var HPKE_ENC_LENGTH = 32;
|
|
5577
|
+
var CHACHA20_POLY1305_TAG_LENGTH = 16;
|
|
5578
|
+
var HPKE_OVERHEAD_LENGTH = HPKE_ENC_LENGTH + CHACHA20_POLY1305_TAG_LENGTH;
|
|
5579
|
+
|
|
5580
|
+
// src/rex/rex-value.ts
|
|
5581
|
+
var RexValueVariant = /* @__PURE__ */ ((RexValueVariant2) => {
|
|
5582
|
+
RexValueVariant2[RexValueVariant2["Plain"] = 0] = "Plain";
|
|
5583
|
+
RexValueVariant2[RexValueVariant2["Encrypted"] = 1] = "Encrypted";
|
|
5584
|
+
return RexValueVariant2;
|
|
5585
|
+
})(RexValueVariant || {});
|
|
5586
|
+
var RexValue = class _RexValue {
|
|
5587
|
+
variant;
|
|
5588
|
+
data;
|
|
5589
|
+
constructor(variant, data) {
|
|
5590
|
+
this.variant = variant;
|
|
5591
|
+
this.data = data;
|
|
5592
|
+
}
|
|
5593
|
+
/**
|
|
5594
|
+
* Create a plain (unencrypted) RexValue from raw bytes.
|
|
5595
|
+
*
|
|
5596
|
+
* @param data - The raw byte data
|
|
5597
|
+
* @returns A new RexValue with Plain variant
|
|
5598
|
+
*/
|
|
5599
|
+
static plain(data) {
|
|
5600
|
+
return new _RexValue(0 /* Plain */, data);
|
|
5601
|
+
}
|
|
5602
|
+
/**
|
|
5603
|
+
* Create a plain (unencrypted) RexValue from a UTF-8 string.
|
|
5604
|
+
*
|
|
5605
|
+
* @param s - The string to encode
|
|
5606
|
+
* @returns A new RexValue with Plain variant
|
|
5607
|
+
*/
|
|
5608
|
+
static plainString(s) {
|
|
5609
|
+
return new _RexValue(
|
|
5610
|
+
0 /* Plain */,
|
|
5611
|
+
new TextEncoder().encode(s)
|
|
5612
|
+
);
|
|
5613
|
+
}
|
|
5614
|
+
/**
|
|
5615
|
+
* Create an encrypted RexValue from HPKE ciphertext.
|
|
5616
|
+
*
|
|
5617
|
+
* @param ciphertext - The HPKE-encrypted ciphertext (enc || ct || tag)
|
|
5618
|
+
* @returns A new RexValue with Encrypted variant
|
|
5619
|
+
*/
|
|
5620
|
+
static encrypted(ciphertext) {
|
|
5621
|
+
return new _RexValue(1 /* Encrypted */, ciphertext);
|
|
5622
|
+
}
|
|
5623
|
+
/**
|
|
5624
|
+
* Check if this is a plain (unencrypted) value.
|
|
5625
|
+
*/
|
|
5626
|
+
isPlain() {
|
|
5627
|
+
return this.variant === 0 /* Plain */;
|
|
5628
|
+
}
|
|
5629
|
+
/**
|
|
5630
|
+
* Check if this is an encrypted value.
|
|
5631
|
+
*/
|
|
5632
|
+
isEncrypted() {
|
|
5633
|
+
return this.variant === 1 /* Encrypted */;
|
|
5634
|
+
}
|
|
5635
|
+
/**
|
|
5636
|
+
* Get the variant type.
|
|
5637
|
+
*/
|
|
5638
|
+
getVariant() {
|
|
5639
|
+
return this.variant;
|
|
5640
|
+
}
|
|
5641
|
+
/**
|
|
5642
|
+
* Get the raw bytes (plaintext or ciphertext).
|
|
5643
|
+
*
|
|
5644
|
+
* For Plain values, returns the plaintext.
|
|
5645
|
+
* For Encrypted values, returns the ciphertext.
|
|
5646
|
+
*/
|
|
5647
|
+
asBytes() {
|
|
5648
|
+
return this.data;
|
|
5649
|
+
}
|
|
5650
|
+
/**
|
|
5651
|
+
* Try to decode the plain value as a UTF-8 string.
|
|
5652
|
+
*
|
|
5653
|
+
* @returns The decoded string, or null if encrypted or not valid UTF-8
|
|
5654
|
+
*/
|
|
5655
|
+
asString() {
|
|
5656
|
+
if (!this.isPlain()) {
|
|
5657
|
+
return null;
|
|
5658
|
+
}
|
|
5659
|
+
try {
|
|
5660
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(this.data);
|
|
5661
|
+
} catch {
|
|
5662
|
+
return null;
|
|
5663
|
+
}
|
|
5664
|
+
}
|
|
5665
|
+
/**
|
|
5666
|
+
* Serialize to Borsh format.
|
|
5667
|
+
*
|
|
5668
|
+
* Format: `[variant: u8] [length: u32 LE] [data bytes]`
|
|
5669
|
+
*
|
|
5670
|
+
* @returns The Borsh-serialized bytes
|
|
5671
|
+
*/
|
|
5672
|
+
toBorsh() {
|
|
5673
|
+
const result = new Uint8Array(1 + 4 + this.data.length);
|
|
5674
|
+
result[0] = this.variant;
|
|
5675
|
+
const dataView = new DataView(result.buffer);
|
|
5676
|
+
dataView.setUint32(1, this.data.length, true);
|
|
5677
|
+
result.set(this.data, 5);
|
|
5678
|
+
return result;
|
|
5679
|
+
}
|
|
5680
|
+
/**
|
|
5681
|
+
* Deserialize from Borsh format.
|
|
5682
|
+
*
|
|
5683
|
+
* @param data - The Borsh-serialized bytes
|
|
5684
|
+
* @returns A new RexValue
|
|
5685
|
+
* @throws {HpkeError} If deserialization fails
|
|
5686
|
+
*/
|
|
5687
|
+
static fromBorsh(data) {
|
|
5688
|
+
if (data.length < 5) {
|
|
5689
|
+
throw HpkeError.borshDeserializeFailed(
|
|
5690
|
+
new Error(`Buffer too short: expected at least 5 bytes, got ${data.length}`)
|
|
5691
|
+
);
|
|
5692
|
+
}
|
|
5693
|
+
const variant = data[0];
|
|
5694
|
+
if (variant !== 0 /* Plain */ && variant !== 1 /* Encrypted */) {
|
|
5695
|
+
throw HpkeError.invalidRexValue(variant);
|
|
5696
|
+
}
|
|
5697
|
+
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
5698
|
+
const length = dataView.getUint32(1, true);
|
|
5699
|
+
if (data.length < 5 + length) {
|
|
5700
|
+
throw HpkeError.borshDeserializeFailed(
|
|
5701
|
+
new Error(`Buffer too short: expected ${5 + length} bytes, got ${data.length}`)
|
|
5702
|
+
);
|
|
5703
|
+
}
|
|
5704
|
+
const payload = data.slice(5, 5 + length);
|
|
5705
|
+
return new _RexValue(variant, payload);
|
|
5706
|
+
}
|
|
5707
|
+
};
|
|
5708
|
+
var hpkeSuite = new core.CipherSuite({
|
|
5709
|
+
kem: new core.DhkemX25519HkdfSha256(),
|
|
5710
|
+
kdf: new core.HkdfSha256(),
|
|
5711
|
+
aead: new chacha20poly1305.Chacha20Poly1305()
|
|
5712
|
+
});
|
|
5713
|
+
function buildAad(senderPubkey) {
|
|
5714
|
+
const aad = new Uint8Array(USER_SECRET_AAD.length + senderPubkey.length);
|
|
5715
|
+
aad.set(USER_SECRET_AAD, 0);
|
|
5716
|
+
aad.set(senderPubkey, USER_SECRET_AAD.length);
|
|
5717
|
+
return aad;
|
|
5718
|
+
}
|
|
5719
|
+
async function hpkeEncrypt(rexPubkey, data, senderPubkey) {
|
|
5720
|
+
if (rexPubkey.length !== X25519_PUBLIC_KEY_LENGTH) {
|
|
5721
|
+
throw HpkeError.invalidKeyLength(
|
|
5722
|
+
X25519_PUBLIC_KEY_LENGTH,
|
|
5723
|
+
rexPubkey.length,
|
|
5724
|
+
"REX public key"
|
|
5725
|
+
);
|
|
5726
|
+
}
|
|
5727
|
+
if (senderPubkey.length !== ED25519_PUBLIC_KEY_LENGTH) {
|
|
5728
|
+
throw HpkeError.invalidKeyLength(
|
|
5729
|
+
ED25519_PUBLIC_KEY_LENGTH,
|
|
5730
|
+
senderPubkey.length,
|
|
5731
|
+
"sender public key"
|
|
5732
|
+
);
|
|
5733
|
+
}
|
|
5734
|
+
try {
|
|
5735
|
+
const recipientKey = await hpkeSuite.kem.importKey(
|
|
5736
|
+
"raw",
|
|
5737
|
+
rexPubkey.buffer.slice(
|
|
5738
|
+
rexPubkey.byteOffset,
|
|
5739
|
+
rexPubkey.byteOffset + rexPubkey.byteLength
|
|
5740
|
+
)
|
|
5741
|
+
);
|
|
5742
|
+
const sender = await hpkeSuite.createSenderContext({
|
|
5743
|
+
recipientPublicKey: recipientKey,
|
|
5744
|
+
info: SECRET_SHARING_HPKE_INFO.buffer.slice(
|
|
5745
|
+
SECRET_SHARING_HPKE_INFO.byteOffset,
|
|
5746
|
+
SECRET_SHARING_HPKE_INFO.byteOffset + SECRET_SHARING_HPKE_INFO.byteLength
|
|
5747
|
+
)
|
|
5748
|
+
});
|
|
5749
|
+
const aad = buildAad(senderPubkey);
|
|
5750
|
+
const ciphertext = await sender.seal(
|
|
5751
|
+
data.buffer.slice(
|
|
5752
|
+
data.byteOffset,
|
|
5753
|
+
data.byteOffset + data.byteLength
|
|
5754
|
+
),
|
|
5755
|
+
aad.buffer.slice(
|
|
5756
|
+
aad.byteOffset,
|
|
5757
|
+
aad.byteOffset + aad.byteLength
|
|
5758
|
+
)
|
|
5759
|
+
);
|
|
5760
|
+
const enc = new Uint8Array(sender.enc);
|
|
5761
|
+
const result = new Uint8Array(enc.length + ciphertext.byteLength);
|
|
5762
|
+
result.set(enc, 0);
|
|
5763
|
+
result.set(new Uint8Array(ciphertext), enc.length);
|
|
5764
|
+
return result;
|
|
5765
|
+
} catch (error) {
|
|
5766
|
+
throw HpkeError.encryptionFailed(
|
|
5767
|
+
error instanceof Error ? error : new Error(String(error))
|
|
5768
|
+
);
|
|
5769
|
+
}
|
|
5770
|
+
}
|
|
5771
|
+
async function encryptForRex(rexPubkey, data, senderPubkey) {
|
|
5772
|
+
const ciphertext = await hpkeEncrypt(rexPubkey, data, senderPubkey);
|
|
5773
|
+
return RexValue.encrypted(ciphertext);
|
|
5774
|
+
}
|
|
5775
|
+
function getCiphertextLength(plaintextLength) {
|
|
5776
|
+
return HPKE_OVERHEAD_LENGTH + plaintextLength;
|
|
5777
|
+
}
|
|
5778
|
+
function isValidCiphertextLength(ciphertext) {
|
|
5779
|
+
return ciphertext.length >= HPKE_OVERHEAD_LENGTH;
|
|
5780
|
+
}
|
|
5781
|
+
|
|
5483
5782
|
// src/rpc/errors.ts
|
|
5484
5783
|
var RpcErrorCode = /* @__PURE__ */ ((RpcErrorCode2) => {
|
|
5485
5784
|
RpcErrorCode2["REQUEST_TIMEOUT"] = "REQUEST_TIMEOUT";
|
|
@@ -5738,9 +6037,7 @@ var QueryRpcClient = class extends BaseRpcClient {
|
|
|
5738
6037
|
* ```
|
|
5739
6038
|
*/
|
|
5740
6039
|
async getAccountInfo(pubkey) {
|
|
5741
|
-
const result = await this.call("getAccountInfo", [
|
|
5742
|
-
{ address: pubkey.toString(), config: { encoding: "base64" } }
|
|
5743
|
-
]);
|
|
6040
|
+
const result = await this.call("getAccountInfo", [{ address: pubkey.toString() }]);
|
|
5744
6041
|
if (!result.value) {
|
|
5745
6042
|
return null;
|
|
5746
6043
|
}
|
|
@@ -6028,10 +6325,9 @@ var QueryRpcClient = class extends BaseRpcClient {
|
|
|
6028
6325
|
owner: owner.toString(),
|
|
6029
6326
|
filter,
|
|
6030
6327
|
config: config ? {
|
|
6031
|
-
encoding: config.encoding ?? "base64",
|
|
6032
6328
|
limit: config.limit,
|
|
6033
6329
|
after: config.after
|
|
6034
|
-
} :
|
|
6330
|
+
} : void 0
|
|
6035
6331
|
}
|
|
6036
6332
|
]);
|
|
6037
6333
|
return {
|
|
@@ -6170,6 +6466,64 @@ var QueryRpcClient = class extends BaseRpcClient {
|
|
|
6170
6466
|
blockNumber: BigInt(tx.block_number)
|
|
6171
6467
|
}));
|
|
6172
6468
|
}
|
|
6469
|
+
/**
|
|
6470
|
+
* Retrieve the REX X25519 public key for secret sharing encryption.
|
|
6471
|
+
*
|
|
6472
|
+
* This key is used for HPKE encryption when sending encrypted data
|
|
6473
|
+
* that should only be decryptable within the REX execution environment.
|
|
6474
|
+
*
|
|
6475
|
+
* @returns The REX X25519 public key as a 32-byte Uint8Array
|
|
6476
|
+
*
|
|
6477
|
+
* @example
|
|
6478
|
+
* ```typescript
|
|
6479
|
+
* import { encryptForRex } from "@rialo/ts-cdk";
|
|
6480
|
+
*
|
|
6481
|
+
* // Get the REX public key
|
|
6482
|
+
* const rexPubkey = await client.getSecretSharingPubkey();
|
|
6483
|
+
*
|
|
6484
|
+
* // Use it for HPKE encryption
|
|
6485
|
+
* const encrypted = await encryptForRex(
|
|
6486
|
+
* rexPubkey,
|
|
6487
|
+
* new TextEncoder().encode("secret data"),
|
|
6488
|
+
* keypair.publicKey.toBytes()
|
|
6489
|
+
* );
|
|
6490
|
+
* ```
|
|
6491
|
+
*/
|
|
6492
|
+
async getSecretSharingPubkey() {
|
|
6493
|
+
const result = await this.call(
|
|
6494
|
+
"getSecretSharingPubkey",
|
|
6495
|
+
[]
|
|
6496
|
+
);
|
|
6497
|
+
const hexString = result.public_key;
|
|
6498
|
+
const bytes = new Uint8Array(hexString.length / 2);
|
|
6499
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
6500
|
+
bytes[i] = Number.parseInt(hexString.slice(i * 2, i * 2 + 2), 16);
|
|
6501
|
+
}
|
|
6502
|
+
return bytes;
|
|
6503
|
+
}
|
|
6504
|
+
/**
|
|
6505
|
+
* Get the config hash prefix for replay protection.
|
|
6506
|
+
*
|
|
6507
|
+
* Returns the first 64 bits of the config hash, which is used
|
|
6508
|
+
* for transaction replay protection across chains.
|
|
6509
|
+
*
|
|
6510
|
+
* @returns The config hash prefix as a bigint
|
|
6511
|
+
*
|
|
6512
|
+
* @example
|
|
6513
|
+
* ```typescript
|
|
6514
|
+
* const configHashPrefix = await client.getConfigHashPrefix();
|
|
6515
|
+
* const tx = TransactionBuilder.create()
|
|
6516
|
+
* .setPayer(payer)
|
|
6517
|
+
* .setValidFrom(validFrom)
|
|
6518
|
+
* .setConfigHashPrefix(configHashPrefix)
|
|
6519
|
+
* .addInstruction(instruction)
|
|
6520
|
+
* .build();
|
|
6521
|
+
* ```
|
|
6522
|
+
*/
|
|
6523
|
+
async getConfigHashPrefix() {
|
|
6524
|
+
const result = await this.call("getRecentValidatorConfigHash", [{}]);
|
|
6525
|
+
return BigInt(result.config_hash_prefix);
|
|
6526
|
+
}
|
|
6173
6527
|
};
|
|
6174
6528
|
|
|
6175
6529
|
// src/rpc/clients/transaction-client.ts
|
|
@@ -6798,189 +7152,219 @@ function getDefaultRialoClientConfig(network) {
|
|
|
6798
7152
|
}
|
|
6799
7153
|
}
|
|
6800
7154
|
|
|
6801
|
-
// src/
|
|
6802
|
-
var
|
|
6803
|
-
|
|
6804
|
-
|
|
7155
|
+
// src/serialization/bincode/reader.ts
|
|
7156
|
+
var BincodeReader = class {
|
|
7157
|
+
view;
|
|
7158
|
+
offset;
|
|
7159
|
+
bytes;
|
|
7160
|
+
constructor(data) {
|
|
7161
|
+
this.bytes = data;
|
|
7162
|
+
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
7163
|
+
this.offset = 0;
|
|
7164
|
+
}
|
|
7165
|
+
// ========== Position Management ==========
|
|
7166
|
+
getOffset() {
|
|
7167
|
+
return this.offset;
|
|
7168
|
+
}
|
|
7169
|
+
remaining() {
|
|
7170
|
+
return this.bytes.length - this.offset;
|
|
7171
|
+
}
|
|
7172
|
+
isExhausted() {
|
|
7173
|
+
return this.offset >= this.bytes.length;
|
|
7174
|
+
}
|
|
7175
|
+
skip(bytes) {
|
|
7176
|
+
this.ensureAvailable(bytes);
|
|
7177
|
+
this.offset += bytes;
|
|
7178
|
+
return this;
|
|
6805
7179
|
}
|
|
6806
7180
|
/**
|
|
6807
|
-
*
|
|
7181
|
+
* Peek at bytes without advancing the offset
|
|
6808
7182
|
*/
|
|
6809
|
-
|
|
6810
|
-
|
|
7183
|
+
peek(length) {
|
|
7184
|
+
this.ensureAvailable(length);
|
|
7185
|
+
return this.bytes.slice(this.offset, this.offset + length);
|
|
6811
7186
|
}
|
|
6812
7187
|
/**
|
|
6813
|
-
*
|
|
7188
|
+
* Reset reader to beginning
|
|
6814
7189
|
*/
|
|
6815
|
-
|
|
6816
|
-
|
|
7190
|
+
reset() {
|
|
7191
|
+
this.offset = 0;
|
|
7192
|
+
return this;
|
|
6817
7193
|
}
|
|
6818
|
-
};
|
|
6819
|
-
|
|
6820
|
-
// src/transaction/account-meta-table.ts
|
|
6821
|
-
var AccountMetaTable = class {
|
|
6822
|
-
accounts = /* @__PURE__ */ new Map();
|
|
6823
|
-
accountOrder = [];
|
|
6824
7194
|
/**
|
|
6825
|
-
*
|
|
6826
|
-
* If account already exists, merges signer/writable flags (OR operation).
|
|
7195
|
+
* Seek to specific offset
|
|
6827
7196
|
*/
|
|
6828
|
-
|
|
6829
|
-
|
|
6830
|
-
|
|
6831
|
-
|
|
6832
|
-
|
|
6833
|
-
existing.isWritable ||= meta.isWritable;
|
|
6834
|
-
} else {
|
|
6835
|
-
this.accounts.set(key, {
|
|
6836
|
-
pubkey: meta.pubkey,
|
|
6837
|
-
isSigner: meta.isSigner,
|
|
6838
|
-
isWritable: meta.isWritable
|
|
6839
|
-
});
|
|
6840
|
-
this.accountOrder.push(key);
|
|
7197
|
+
seek(offset) {
|
|
7198
|
+
if (offset < 0 || offset > this.bytes.length) {
|
|
7199
|
+
throw new Error(
|
|
7200
|
+
`Invalid seek offset: ${offset}, buffer length: ${this.bytes.length}`
|
|
7201
|
+
);
|
|
6841
7202
|
}
|
|
7203
|
+
this.offset = offset;
|
|
7204
|
+
return this;
|
|
6842
7205
|
}
|
|
6843
|
-
|
|
6844
|
-
|
|
6845
|
-
|
|
6846
|
-
|
|
6847
|
-
|
|
6848
|
-
|
|
7206
|
+
// ========== Primitive Types ==========
|
|
7207
|
+
readU8() {
|
|
7208
|
+
this.ensureAvailable(1);
|
|
7209
|
+
const value = this.view.getUint8(this.offset);
|
|
7210
|
+
this.offset += 1;
|
|
7211
|
+
return value;
|
|
7212
|
+
}
|
|
7213
|
+
readU16() {
|
|
7214
|
+
this.ensureAvailable(2);
|
|
7215
|
+
const value = this.view.getUint16(this.offset, true);
|
|
7216
|
+
this.offset += 2;
|
|
7217
|
+
return value;
|
|
7218
|
+
}
|
|
7219
|
+
readU32() {
|
|
7220
|
+
this.ensureAvailable(4);
|
|
7221
|
+
const value = this.view.getUint32(this.offset, true);
|
|
7222
|
+
this.offset += 4;
|
|
7223
|
+
return value;
|
|
7224
|
+
}
|
|
7225
|
+
readU64() {
|
|
7226
|
+
this.ensureAvailable(8);
|
|
7227
|
+
const low = this.view.getUint32(this.offset, true);
|
|
7228
|
+
const high = this.view.getUint32(this.offset + 4, true);
|
|
7229
|
+
this.offset += 8;
|
|
7230
|
+
return BigInt(low) | BigInt(high) << 32n;
|
|
7231
|
+
}
|
|
7232
|
+
readU128() {
|
|
7233
|
+
this.ensureAvailable(16);
|
|
7234
|
+
const low = this.readU64();
|
|
7235
|
+
const high = this.readU64();
|
|
7236
|
+
return low | high << 64n;
|
|
7237
|
+
}
|
|
7238
|
+
readI8() {
|
|
7239
|
+
this.ensureAvailable(1);
|
|
7240
|
+
const value = this.view.getInt8(this.offset);
|
|
7241
|
+
this.offset += 1;
|
|
7242
|
+
return value;
|
|
7243
|
+
}
|
|
7244
|
+
readI16() {
|
|
7245
|
+
this.ensureAvailable(2);
|
|
7246
|
+
const value = this.view.getInt16(this.offset, true);
|
|
7247
|
+
this.offset += 2;
|
|
7248
|
+
return value;
|
|
7249
|
+
}
|
|
7250
|
+
readI32() {
|
|
7251
|
+
this.ensureAvailable(4);
|
|
7252
|
+
const value = this.view.getInt32(this.offset, true);
|
|
7253
|
+
this.offset += 4;
|
|
7254
|
+
return value;
|
|
7255
|
+
}
|
|
7256
|
+
readI64() {
|
|
7257
|
+
this.ensureAvailable(8);
|
|
7258
|
+
const low = this.view.getUint32(this.offset, true);
|
|
7259
|
+
const high = this.view.getInt32(this.offset + 4, true);
|
|
7260
|
+
this.offset += 8;
|
|
7261
|
+
return BigInt(low) | BigInt(high) << 32n;
|
|
7262
|
+
}
|
|
7263
|
+
readI128() {
|
|
7264
|
+
const unsigned = this.readU128();
|
|
7265
|
+
const signBit = 1n << 127n;
|
|
7266
|
+
if ((unsigned & signBit) !== 0n) {
|
|
7267
|
+
return unsigned - (1n << 128n);
|
|
6849
7268
|
}
|
|
7269
|
+
return unsigned;
|
|
6850
7270
|
}
|
|
6851
|
-
|
|
6852
|
-
|
|
6853
|
-
|
|
6854
|
-
|
|
6855
|
-
|
|
6856
|
-
|
|
6857
|
-
|
|
7271
|
+
readF32() {
|
|
7272
|
+
this.ensureAvailable(4);
|
|
7273
|
+
const value = this.view.getFloat32(this.offset, true);
|
|
7274
|
+
this.offset += 4;
|
|
7275
|
+
return value;
|
|
7276
|
+
}
|
|
7277
|
+
readF64() {
|
|
7278
|
+
this.ensureAvailable(8);
|
|
7279
|
+
const value = this.view.getFloat64(this.offset, true);
|
|
7280
|
+
this.offset += 8;
|
|
7281
|
+
return value;
|
|
7282
|
+
}
|
|
7283
|
+
readBool() {
|
|
7284
|
+
const value = this.readU8();
|
|
7285
|
+
if (value > 1) {
|
|
7286
|
+
throw new Error(`Invalid boolean value: ${value}`);
|
|
7287
|
+
}
|
|
7288
|
+
return value === 1;
|
|
7289
|
+
}
|
|
7290
|
+
// ========== Compound Types ==========
|
|
7291
|
+
readBytes(length) {
|
|
7292
|
+
this.ensureAvailable(length);
|
|
7293
|
+
const bytes = this.bytes.slice(this.offset, this.offset + length);
|
|
7294
|
+
this.offset += length;
|
|
7295
|
+
return bytes;
|
|
7296
|
+
}
|
|
7297
|
+
readFixedArray(length) {
|
|
7298
|
+
return this.readBytes(length);
|
|
7299
|
+
}
|
|
7300
|
+
readVecBytes() {
|
|
7301
|
+
const length = this.readLength();
|
|
7302
|
+
return this.readBytes(length);
|
|
7303
|
+
}
|
|
7304
|
+
readString() {
|
|
7305
|
+
const bytes = this.readVecBytes();
|
|
7306
|
+
return new TextDecoder().decode(bytes);
|
|
7307
|
+
}
|
|
7308
|
+
readVariant() {
|
|
7309
|
+
return this.readU32();
|
|
7310
|
+
}
|
|
7311
|
+
readOption(readValue2) {
|
|
7312
|
+
const hasValue = this.readU8();
|
|
7313
|
+
if (hasValue === 0) {
|
|
7314
|
+
return null;
|
|
7315
|
+
}
|
|
7316
|
+
if (hasValue !== 1) {
|
|
7317
|
+
throw new Error(`Invalid option discriminant: ${hasValue}`);
|
|
7318
|
+
}
|
|
7319
|
+
return readValue2();
|
|
7320
|
+
}
|
|
7321
|
+
readVec(readElement) {
|
|
7322
|
+
const length = this.readLength();
|
|
7323
|
+
const result = [];
|
|
7324
|
+
for (let i = 0; i < length; i++) {
|
|
7325
|
+
result.push(readElement());
|
|
7326
|
+
}
|
|
7327
|
+
return result;
|
|
6858
7328
|
}
|
|
6859
7329
|
/**
|
|
6860
|
-
*
|
|
7330
|
+
* Read a tuple of fixed size with heterogeneous types
|
|
6861
7331
|
*/
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
6867
|
-
|
|
6868
|
-
|
|
6869
|
-
|
|
6870
|
-
const
|
|
6871
|
-
|
|
6872
|
-
|
|
6873
|
-
|
|
6874
|
-
return aBytes[i] - bBytes[i];
|
|
6875
|
-
}
|
|
6876
|
-
}
|
|
6877
|
-
return 0;
|
|
6878
|
-
});
|
|
7332
|
+
readTuple(readers) {
|
|
7333
|
+
return readers.map((read) => read());
|
|
7334
|
+
}
|
|
7335
|
+
readMap(readKey, readValue2) {
|
|
7336
|
+
const length = this.readLength();
|
|
7337
|
+
const map = /* @__PURE__ */ new Map();
|
|
7338
|
+
for (let i = 0; i < length; i++) {
|
|
7339
|
+
const key = readKey();
|
|
7340
|
+
const value = readValue2();
|
|
7341
|
+
map.set(key, value);
|
|
7342
|
+
}
|
|
7343
|
+
return map;
|
|
6879
7344
|
}
|
|
7345
|
+
// ========== Helpers ==========
|
|
6880
7346
|
/**
|
|
6881
|
-
*
|
|
7347
|
+
* Read length as u64 but validate it's within safe integer range
|
|
6882
7348
|
*/
|
|
6883
|
-
|
|
6884
|
-
const
|
|
6885
|
-
|
|
6886
|
-
|
|
6887
|
-
let numReadonlyUnsignedAccounts = 0;
|
|
6888
|
-
for (const entry of sorted) {
|
|
6889
|
-
if (entry.isSigner) {
|
|
6890
|
-
numRequiredSignatures++;
|
|
6891
|
-
if (!entry.isWritable) {
|
|
6892
|
-
numReadonlySignedAccounts++;
|
|
6893
|
-
}
|
|
6894
|
-
} else if (!entry.isWritable) {
|
|
6895
|
-
numReadonlyUnsignedAccounts++;
|
|
6896
|
-
}
|
|
7349
|
+
readLength() {
|
|
7350
|
+
const length = this.readU64();
|
|
7351
|
+
if (length > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
7352
|
+
throw new Error(`Length ${length} exceeds maximum safe integer`);
|
|
6897
7353
|
}
|
|
6898
|
-
|
|
6899
|
-
|
|
6900
|
-
|
|
6901
|
-
|
|
6902
|
-
|
|
6903
|
-
|
|
6904
|
-
|
|
6905
|
-
* Get sorted public keys.
|
|
6906
|
-
*/
|
|
6907
|
-
getPublicKeys() {
|
|
6908
|
-
return this.getSortedAccounts().map((entry) => entry.pubkey);
|
|
6909
|
-
}
|
|
6910
|
-
/**
|
|
6911
|
-
* Get number of accounts in table.
|
|
6912
|
-
*/
|
|
6913
|
-
size() {
|
|
6914
|
-
return this.accounts.size;
|
|
6915
|
-
}
|
|
6916
|
-
};
|
|
6917
|
-
|
|
6918
|
-
// src/transaction/errors.ts
|
|
6919
|
-
var TransactionErrorCode = /* @__PURE__ */ ((TransactionErrorCode2) => {
|
|
6920
|
-
TransactionErrorCode2["INVALID_INSTRUCTION"] = "INVALID_INSTRUCTION";
|
|
6921
|
-
TransactionErrorCode2["INVALID_ACCOUNT_META"] = "INVALID_ACCOUNT_META";
|
|
6922
|
-
TransactionErrorCode2["NO_INSTRUCTIONS"] = "NO_INSTRUCTIONS";
|
|
6923
|
-
TransactionErrorCode2["DUPLICATE_ACCOUNT"] = "DUPLICATE_ACCOUNT";
|
|
6924
|
-
TransactionErrorCode2["MISSING_SIGNATURE"] = "MISSING_SIGNATURE";
|
|
6925
|
-
TransactionErrorCode2["INVALID_SIGNATURE"] = "INVALID_SIGNATURE";
|
|
6926
|
-
TransactionErrorCode2["SIGNATURE_OUT_OF_BOUNDS"] = "SIGNATURE_OUT_OF_BOUNDS";
|
|
6927
|
-
TransactionErrorCode2["SIGNER_NOT_FOUND"] = "SIGNER_NOT_FOUND";
|
|
6928
|
-
TransactionErrorCode2["ALREADY_SIGNED"] = "ALREADY_SIGNED";
|
|
6929
|
-
TransactionErrorCode2["INVALID_MESSAGE_FORMAT"] = "INVALID_MESSAGE_FORMAT";
|
|
6930
|
-
TransactionErrorCode2["INSUFFICIENT_DATA"] = "INSUFFICIENT_DATA";
|
|
6931
|
-
TransactionErrorCode2["SERIALIZATION_FAILED"] = "SERIALIZATION_FAILED";
|
|
6932
|
-
TransactionErrorCode2["SIMULATION_FAILED"] = "SIMULATION_FAILED";
|
|
6933
|
-
TransactionErrorCode2["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
|
|
6934
|
-
TransactionErrorCode2["BLOCKHASH_EXPIRED"] = "BLOCKHASH_EXPIRED";
|
|
6935
|
-
return TransactionErrorCode2;
|
|
6936
|
-
})(TransactionErrorCode || {});
|
|
6937
|
-
var TransactionError = class _TransactionError extends Error {
|
|
6938
|
-
constructor(code, message, details) {
|
|
6939
|
-
super(message);
|
|
6940
|
-
this.code = code;
|
|
6941
|
-
this.details = details;
|
|
6942
|
-
this.name = "TransactionError";
|
|
6943
|
-
}
|
|
6944
|
-
static noInstructions() {
|
|
6945
|
-
return new _TransactionError(
|
|
6946
|
-
"NO_INSTRUCTIONS" /* NO_INSTRUCTIONS */,
|
|
6947
|
-
"Transaction must contain at least one instruction. Use addInstruction() to add instructions."
|
|
6948
|
-
);
|
|
6949
|
-
}
|
|
6950
|
-
static signerNotFound(pubkey) {
|
|
6951
|
-
return new _TransactionError(
|
|
6952
|
-
"SIGNER_NOT_FOUND" /* SIGNER_NOT_FOUND */,
|
|
6953
|
-
`Public key ${pubkey} is not a required signer for this transaction. Check that the account is marked as a signer in the instruction.`,
|
|
6954
|
-
{ pubkey }
|
|
6955
|
-
);
|
|
6956
|
-
}
|
|
6957
|
-
static missingSignature(index, pubkey) {
|
|
6958
|
-
return new _TransactionError(
|
|
6959
|
-
"MISSING_SIGNATURE" /* MISSING_SIGNATURE */,
|
|
6960
|
-
`Missing signature for signer ${index} (${pubkey}). Transaction requires ${index + 1} signatures.`,
|
|
6961
|
-
{ index, pubkey }
|
|
6962
|
-
);
|
|
6963
|
-
}
|
|
6964
|
-
static simulationFailed(logs, error) {
|
|
6965
|
-
return new _TransactionError(
|
|
6966
|
-
"SIMULATION_FAILED" /* SIMULATION_FAILED */,
|
|
6967
|
-
`Transaction simulation failed: ${error}`,
|
|
6968
|
-
{ logs, error }
|
|
6969
|
-
);
|
|
6970
|
-
}
|
|
6971
|
-
static insufficientFunds(required, available) {
|
|
6972
|
-
return new _TransactionError(
|
|
6973
|
-
"INSUFFICIENT_FUNDS" /* INSUFFICIENT_FUNDS */,
|
|
6974
|
-
`Insufficient funds: need ${required}, have ${available}`,
|
|
6975
|
-
{ required: required.toString(), available: available.toString() }
|
|
6976
|
-
);
|
|
7354
|
+
const numLength = Number(length);
|
|
7355
|
+
if (numLength > this.remaining()) {
|
|
7356
|
+
throw new Error(
|
|
7357
|
+
`Length ${numLength} exceeds remaining bytes ${this.remaining()}`
|
|
7358
|
+
);
|
|
7359
|
+
}
|
|
7360
|
+
return numLength;
|
|
6977
7361
|
}
|
|
6978
|
-
|
|
6979
|
-
|
|
6980
|
-
|
|
6981
|
-
|
|
6982
|
-
|
|
6983
|
-
}
|
|
7362
|
+
ensureAvailable(bytes) {
|
|
7363
|
+
if (this.offset + bytes > this.bytes.length) {
|
|
7364
|
+
throw new Error(
|
|
7365
|
+
`Unexpected end of data: need ${bytes} bytes at offset ${this.offset}, but only ${this.bytes.length - this.offset} available`
|
|
7366
|
+
);
|
|
7367
|
+
}
|
|
6984
7368
|
}
|
|
6985
7369
|
};
|
|
6986
7370
|
|
|
@@ -7142,12 +7526,12 @@ var BincodeWriter = class {
|
|
|
7142
7526
|
writeVariant(index) {
|
|
7143
7527
|
return this.writeU32(index);
|
|
7144
7528
|
}
|
|
7145
|
-
writeOption(value,
|
|
7529
|
+
writeOption(value, writeValue2) {
|
|
7146
7530
|
if (value === null || value === void 0) {
|
|
7147
7531
|
this.writeU8(0);
|
|
7148
7532
|
} else {
|
|
7149
7533
|
this.writeU8(1);
|
|
7150
|
-
|
|
7534
|
+
writeValue2(this, value);
|
|
7151
7535
|
}
|
|
7152
7536
|
return this;
|
|
7153
7537
|
}
|
|
@@ -7170,11 +7554,11 @@ var BincodeWriter = class {
|
|
|
7170
7554
|
}
|
|
7171
7555
|
return this;
|
|
7172
7556
|
}
|
|
7173
|
-
writeMap(map, writeKey,
|
|
7557
|
+
writeMap(map, writeKey, writeValue2) {
|
|
7174
7558
|
this.writeU64(BigInt(map.size));
|
|
7175
7559
|
for (const [key, value] of map) {
|
|
7176
7560
|
writeKey(this, key);
|
|
7177
|
-
|
|
7561
|
+
writeValue2(this, value);
|
|
7178
7562
|
}
|
|
7179
7563
|
return this;
|
|
7180
7564
|
}
|
|
@@ -7227,11 +7611,366 @@ var BincodeWriter = class {
|
|
|
7227
7611
|
}
|
|
7228
7612
|
};
|
|
7229
7613
|
|
|
7614
|
+
// src/serialization/bincode/schema.ts
|
|
7615
|
+
var Schema = {
|
|
7616
|
+
u8: () => ({ type: "u8" }),
|
|
7617
|
+
u16: () => ({ type: "u16" }),
|
|
7618
|
+
u32: () => ({ type: "u32" }),
|
|
7619
|
+
u64: () => ({ type: "u64" }),
|
|
7620
|
+
u128: () => ({ type: "u128" }),
|
|
7621
|
+
i8: () => ({ type: "i8" }),
|
|
7622
|
+
i16: () => ({ type: "i16" }),
|
|
7623
|
+
i32: () => ({ type: "i32" }),
|
|
7624
|
+
i64: () => ({ type: "i64" }),
|
|
7625
|
+
i128: () => ({ type: "i128" }),
|
|
7626
|
+
f32: () => ({ type: "f32" }),
|
|
7627
|
+
f64: () => ({ type: "f64" }),
|
|
7628
|
+
bool: () => ({ type: "bool" }),
|
|
7629
|
+
string: () => ({ type: "string" }),
|
|
7630
|
+
bytes: () => ({ type: "bytes" }),
|
|
7631
|
+
unit: () => ({ type: "unit" }),
|
|
7632
|
+
fixedArray: (length) => ({
|
|
7633
|
+
type: "fixedArray",
|
|
7634
|
+
length
|
|
7635
|
+
}),
|
|
7636
|
+
vec: (element) => ({
|
|
7637
|
+
type: "vec",
|
|
7638
|
+
element
|
|
7639
|
+
}),
|
|
7640
|
+
option: (inner) => ({
|
|
7641
|
+
type: "option",
|
|
7642
|
+
inner
|
|
7643
|
+
}),
|
|
7644
|
+
tuple: (...elements) => ({
|
|
7645
|
+
type: "tuple",
|
|
7646
|
+
elements
|
|
7647
|
+
}),
|
|
7648
|
+
/**
|
|
7649
|
+
* Define a struct with ordered fields
|
|
7650
|
+
* @example
|
|
7651
|
+
* Schema.struct([
|
|
7652
|
+
* ["id", Schema.u64()],
|
|
7653
|
+
* ["name", Schema.string()],
|
|
7654
|
+
* ["active", Schema.bool()],
|
|
7655
|
+
* ])
|
|
7656
|
+
*/
|
|
7657
|
+
struct: (fields) => ({
|
|
7658
|
+
type: "struct",
|
|
7659
|
+
fields: fields.map(([name, schema]) => ({ name, schema }))
|
|
7660
|
+
}),
|
|
7661
|
+
/**
|
|
7662
|
+
* Define an enum with ordered variants
|
|
7663
|
+
* @example
|
|
7664
|
+
* Schema.enum([
|
|
7665
|
+
* ["None", null],
|
|
7666
|
+
* ["Some", Schema.u64()],
|
|
7667
|
+
* ["Error", Schema.string()],
|
|
7668
|
+
* ])
|
|
7669
|
+
*/
|
|
7670
|
+
enum: (variants) => ({
|
|
7671
|
+
type: "enum",
|
|
7672
|
+
variants: variants.map(([name, schema]) => ({ name, schema }))
|
|
7673
|
+
}),
|
|
7674
|
+
map: (key, value) => ({
|
|
7675
|
+
type: "map",
|
|
7676
|
+
key,
|
|
7677
|
+
value
|
|
7678
|
+
}),
|
|
7679
|
+
pubkey: () => ({ type: "pubkey" })
|
|
7680
|
+
};
|
|
7681
|
+
function serialize(schema, value) {
|
|
7682
|
+
const writer = new BincodeWriter();
|
|
7683
|
+
writeValue(writer, schema, value);
|
|
7684
|
+
return writer.toBytes();
|
|
7685
|
+
}
|
|
7686
|
+
function deserialize(schema, data) {
|
|
7687
|
+
const reader = new BincodeReader(data);
|
|
7688
|
+
const result = readValue(reader, schema);
|
|
7689
|
+
if (!reader.isExhausted()) {
|
|
7690
|
+
console.warn(
|
|
7691
|
+
`Bincode deserialize: ${reader.remaining()} bytes remaining after parsing`
|
|
7692
|
+
);
|
|
7693
|
+
}
|
|
7694
|
+
return result;
|
|
7695
|
+
}
|
|
7696
|
+
function deserializeStrict(schema, data) {
|
|
7697
|
+
const reader = new BincodeReader(data);
|
|
7698
|
+
const result = readValue(reader, schema);
|
|
7699
|
+
if (!reader.isExhausted()) {
|
|
7700
|
+
throw new Error(
|
|
7701
|
+
`Unexpected data: ${reader.remaining()} bytes remaining after parsing`
|
|
7702
|
+
);
|
|
7703
|
+
}
|
|
7704
|
+
return result;
|
|
7705
|
+
}
|
|
7706
|
+
function writeValue(writer, schema, value) {
|
|
7707
|
+
switch (schema.type) {
|
|
7708
|
+
case "u8":
|
|
7709
|
+
writer.writeU8(value);
|
|
7710
|
+
break;
|
|
7711
|
+
case "u16":
|
|
7712
|
+
writer.writeU16(value);
|
|
7713
|
+
break;
|
|
7714
|
+
case "u32":
|
|
7715
|
+
writer.writeU32(value);
|
|
7716
|
+
break;
|
|
7717
|
+
case "u64":
|
|
7718
|
+
writer.writeU64(value);
|
|
7719
|
+
break;
|
|
7720
|
+
case "u128":
|
|
7721
|
+
writer.writeU128(value);
|
|
7722
|
+
break;
|
|
7723
|
+
case "i8":
|
|
7724
|
+
writer.writeI8(value);
|
|
7725
|
+
break;
|
|
7726
|
+
case "i16":
|
|
7727
|
+
writer.writeI16(value);
|
|
7728
|
+
break;
|
|
7729
|
+
case "i32":
|
|
7730
|
+
writer.writeI32(value);
|
|
7731
|
+
break;
|
|
7732
|
+
case "i64":
|
|
7733
|
+
writer.writeI64(value);
|
|
7734
|
+
break;
|
|
7735
|
+
case "i128":
|
|
7736
|
+
writer.writeI128(value);
|
|
7737
|
+
break;
|
|
7738
|
+
case "f32":
|
|
7739
|
+
writer.writeF32(value);
|
|
7740
|
+
break;
|
|
7741
|
+
case "f64":
|
|
7742
|
+
writer.writeF64(value);
|
|
7743
|
+
break;
|
|
7744
|
+
case "bool":
|
|
7745
|
+
writer.writeBool(value);
|
|
7746
|
+
break;
|
|
7747
|
+
case "string":
|
|
7748
|
+
writer.writeString(value);
|
|
7749
|
+
break;
|
|
7750
|
+
case "bytes":
|
|
7751
|
+
writer.writeVecBytes(value);
|
|
7752
|
+
break;
|
|
7753
|
+
case "unit":
|
|
7754
|
+
break;
|
|
7755
|
+
case "fixedArray": {
|
|
7756
|
+
const arr = value;
|
|
7757
|
+
if (arr.length !== schema.length) {
|
|
7758
|
+
throw new Error(
|
|
7759
|
+
`Fixed array length mismatch: expected ${schema.length}, got ${arr.length}`
|
|
7760
|
+
);
|
|
7761
|
+
}
|
|
7762
|
+
writer.writeFixedArray(arr);
|
|
7763
|
+
break;
|
|
7764
|
+
}
|
|
7765
|
+
case "pubkey": {
|
|
7766
|
+
const arr = value;
|
|
7767
|
+
if (arr.length !== 32) {
|
|
7768
|
+
throw new Error(`Pubkey must be exactly 32 bytes, got ${arr.length}`);
|
|
7769
|
+
}
|
|
7770
|
+
writer.writeFixedArray(arr);
|
|
7771
|
+
break;
|
|
7772
|
+
}
|
|
7773
|
+
case "vec": {
|
|
7774
|
+
const arr = value;
|
|
7775
|
+
writer.writeU64(BigInt(arr.length));
|
|
7776
|
+
for (const item of arr) {
|
|
7777
|
+
writeValue(writer, schema.element, item);
|
|
7778
|
+
}
|
|
7779
|
+
break;
|
|
7780
|
+
}
|
|
7781
|
+
case "option": {
|
|
7782
|
+
if (value === null || value === void 0) {
|
|
7783
|
+
writer.writeU8(0);
|
|
7784
|
+
} else {
|
|
7785
|
+
writer.writeU8(1);
|
|
7786
|
+
writeValue(writer, schema.inner, value);
|
|
7787
|
+
}
|
|
7788
|
+
break;
|
|
7789
|
+
}
|
|
7790
|
+
case "tuple": {
|
|
7791
|
+
const arr = value;
|
|
7792
|
+
if (arr.length !== schema.elements.length) {
|
|
7793
|
+
throw new Error(
|
|
7794
|
+
`Tuple length mismatch: expected ${schema.elements.length}, got ${arr.length}`
|
|
7795
|
+
);
|
|
7796
|
+
}
|
|
7797
|
+
for (let i = 0; i < schema.elements.length; i++) {
|
|
7798
|
+
writeValue(writer, schema.elements[i], arr[i]);
|
|
7799
|
+
}
|
|
7800
|
+
break;
|
|
7801
|
+
}
|
|
7802
|
+
case "struct": {
|
|
7803
|
+
const obj = value;
|
|
7804
|
+
for (const field2 of schema.fields) {
|
|
7805
|
+
if (!(field2.name in obj)) {
|
|
7806
|
+
throw new Error(`Missing struct field: ${field2.name}`);
|
|
7807
|
+
}
|
|
7808
|
+
writeValue(writer, field2.schema, obj[field2.name]);
|
|
7809
|
+
}
|
|
7810
|
+
break;
|
|
7811
|
+
}
|
|
7812
|
+
case "enum": {
|
|
7813
|
+
const enumValue = value;
|
|
7814
|
+
const variantIndex = schema.variants.findIndex(
|
|
7815
|
+
(v) => v.name === enumValue.variant
|
|
7816
|
+
);
|
|
7817
|
+
if (variantIndex === -1) {
|
|
7818
|
+
const validVariants = schema.variants.map((v) => v.name).join(", ");
|
|
7819
|
+
throw new Error(
|
|
7820
|
+
`Unknown variant: ${enumValue.variant}. Valid variants: ${validVariants}`
|
|
7821
|
+
);
|
|
7822
|
+
}
|
|
7823
|
+
writer.writeVariant(variantIndex);
|
|
7824
|
+
const variantSchema = schema.variants[variantIndex].schema;
|
|
7825
|
+
if (variantSchema !== null) {
|
|
7826
|
+
writeValue(writer, variantSchema, enumValue.value);
|
|
7827
|
+
}
|
|
7828
|
+
break;
|
|
7829
|
+
}
|
|
7830
|
+
case "map": {
|
|
7831
|
+
const map = value;
|
|
7832
|
+
writer.writeU64(BigInt(map.size));
|
|
7833
|
+
for (const [k, v] of map) {
|
|
7834
|
+
writeValue(writer, schema.key, k);
|
|
7835
|
+
writeValue(writer, schema.value, v);
|
|
7836
|
+
}
|
|
7837
|
+
break;
|
|
7838
|
+
}
|
|
7839
|
+
default: {
|
|
7840
|
+
throw new Error(`Unknown schema type: ${JSON.stringify(schema)}`);
|
|
7841
|
+
}
|
|
7842
|
+
}
|
|
7843
|
+
}
|
|
7844
|
+
function readValue(reader, schema) {
|
|
7845
|
+
switch (schema.type) {
|
|
7846
|
+
case "u8":
|
|
7847
|
+
return reader.readU8();
|
|
7848
|
+
case "u16":
|
|
7849
|
+
return reader.readU16();
|
|
7850
|
+
case "u32":
|
|
7851
|
+
return reader.readU32();
|
|
7852
|
+
case "u64":
|
|
7853
|
+
return reader.readU64();
|
|
7854
|
+
case "u128":
|
|
7855
|
+
return reader.readU128();
|
|
7856
|
+
case "i8":
|
|
7857
|
+
return reader.readI8();
|
|
7858
|
+
case "i16":
|
|
7859
|
+
return reader.readI16();
|
|
7860
|
+
case "i32":
|
|
7861
|
+
return reader.readI32();
|
|
7862
|
+
case "i64":
|
|
7863
|
+
return reader.readI64();
|
|
7864
|
+
case "i128":
|
|
7865
|
+
return reader.readI128();
|
|
7866
|
+
case "f32":
|
|
7867
|
+
return reader.readF32();
|
|
7868
|
+
case "f64":
|
|
7869
|
+
return reader.readF64();
|
|
7870
|
+
case "bool":
|
|
7871
|
+
return reader.readBool();
|
|
7872
|
+
case "string":
|
|
7873
|
+
return reader.readString();
|
|
7874
|
+
case "bytes":
|
|
7875
|
+
return reader.readVecBytes();
|
|
7876
|
+
case "unit":
|
|
7877
|
+
return void 0;
|
|
7878
|
+
case "fixedArray":
|
|
7879
|
+
return reader.readFixedArray(schema.length);
|
|
7880
|
+
case "pubkey":
|
|
7881
|
+
return reader.readFixedArray(32);
|
|
7882
|
+
case "vec": {
|
|
7883
|
+
const lengthBigInt = reader.readU64();
|
|
7884
|
+
if (lengthBigInt > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
7885
|
+
throw new Error(
|
|
7886
|
+
`Vec length ${lengthBigInt} exceeds safe integer range`
|
|
7887
|
+
);
|
|
7888
|
+
}
|
|
7889
|
+
const length = Number(lengthBigInt);
|
|
7890
|
+
const result = [];
|
|
7891
|
+
for (let i = 0; i < length; i++) {
|
|
7892
|
+
result.push(readValue(reader, schema.element));
|
|
7893
|
+
}
|
|
7894
|
+
return result;
|
|
7895
|
+
}
|
|
7896
|
+
case "option": {
|
|
7897
|
+
const hasValue = reader.readU8();
|
|
7898
|
+
if (hasValue === 0) return null;
|
|
7899
|
+
if (hasValue !== 1) {
|
|
7900
|
+
throw new Error(`Invalid option discriminant: ${hasValue}`);
|
|
7901
|
+
}
|
|
7902
|
+
return readValue(reader, schema.inner);
|
|
7903
|
+
}
|
|
7904
|
+
case "tuple": {
|
|
7905
|
+
const result = [];
|
|
7906
|
+
for (const elementSchema of schema.elements) {
|
|
7907
|
+
result.push(readValue(reader, elementSchema));
|
|
7908
|
+
}
|
|
7909
|
+
return result;
|
|
7910
|
+
}
|
|
7911
|
+
case "struct": {
|
|
7912
|
+
const result = {};
|
|
7913
|
+
for (const field2 of schema.fields) {
|
|
7914
|
+
result[field2.name] = readValue(reader, field2.schema);
|
|
7915
|
+
}
|
|
7916
|
+
return result;
|
|
7917
|
+
}
|
|
7918
|
+
case "enum": {
|
|
7919
|
+
const variantIndex = reader.readVariant();
|
|
7920
|
+
if (variantIndex >= schema.variants.length) {
|
|
7921
|
+
throw new Error(
|
|
7922
|
+
`Invalid variant index: ${variantIndex}, max: ${schema.variants.length - 1}`
|
|
7923
|
+
);
|
|
7924
|
+
}
|
|
7925
|
+
const variant = schema.variants[variantIndex];
|
|
7926
|
+
if (variant.schema === null) {
|
|
7927
|
+
return { variant: variant.name };
|
|
7928
|
+
}
|
|
7929
|
+
return {
|
|
7930
|
+
variant: variant.name,
|
|
7931
|
+
value: readValue(reader, variant.schema)
|
|
7932
|
+
};
|
|
7933
|
+
}
|
|
7934
|
+
case "map": {
|
|
7935
|
+
const lengthBigInt = reader.readU64();
|
|
7936
|
+
if (lengthBigInt > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
7937
|
+
throw new Error(
|
|
7938
|
+
`Map length ${lengthBigInt} exceeds safe integer range`
|
|
7939
|
+
);
|
|
7940
|
+
}
|
|
7941
|
+
const length = Number(lengthBigInt);
|
|
7942
|
+
const map = /* @__PURE__ */ new Map();
|
|
7943
|
+
for (let i = 0; i < length; i++) {
|
|
7944
|
+
const key = readValue(reader, schema.key);
|
|
7945
|
+
const value = readValue(reader, schema.value);
|
|
7946
|
+
map.set(key, value);
|
|
7947
|
+
}
|
|
7948
|
+
return map;
|
|
7949
|
+
}
|
|
7950
|
+
default: {
|
|
7951
|
+
const _exhaustive = schema;
|
|
7952
|
+
throw new Error(
|
|
7953
|
+
`Unknown schema type: ${_exhaustive.type}`
|
|
7954
|
+
);
|
|
7955
|
+
}
|
|
7956
|
+
}
|
|
7957
|
+
}
|
|
7958
|
+
|
|
7230
7959
|
// node_modules/@dao-xyz/borsh/lib/esm/binary.js
|
|
7231
7960
|
var import_float = __toESM(require_float());
|
|
7232
7961
|
var import_utf8 = __toESM(require_utf8());
|
|
7233
7962
|
|
|
7234
7963
|
// node_modules/@dao-xyz/borsh/lib/esm/bigint.js
|
|
7964
|
+
function arrayToHex(arr, reverse = false) {
|
|
7965
|
+
return [...reverse ? new Uint8Array(arr).reverse() : new Uint8Array(arr)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
7966
|
+
}
|
|
7967
|
+
function toBigIntLE(buf) {
|
|
7968
|
+
const hex = arrayToHex(buf, true);
|
|
7969
|
+
if (hex.length === 0) {
|
|
7970
|
+
return BigInt(0);
|
|
7971
|
+
}
|
|
7972
|
+
return BigInt(`0x${hex}`);
|
|
7973
|
+
}
|
|
7235
7974
|
function writeBufferLEBigInt(num, width, buffer, offset) {
|
|
7236
7975
|
const hex = num.toString(16);
|
|
7237
7976
|
const padded = hex.padStart(width * 2, "0").slice(0, width * 2);
|
|
@@ -7284,6 +8023,39 @@ var writeBigUint64Le = (bigIntOrNumber, buf, offset) => {
|
|
|
7284
8023
|
buf[offset + 6] = hi >>> 16;
|
|
7285
8024
|
buf[offset + 7] = hi >>> 24;
|
|
7286
8025
|
};
|
|
8026
|
+
var readBigUInt64LE = (buf, offset) => {
|
|
8027
|
+
const first = buf[offset];
|
|
8028
|
+
const last = buf[offset + 7];
|
|
8029
|
+
if (first === void 0 || last === void 0)
|
|
8030
|
+
throw new Error("Out of bounds");
|
|
8031
|
+
let lo = (first | buf[offset + 1] << 8 | buf[offset + 2] << 16 | buf[offset + 3] << 24) >>> 0;
|
|
8032
|
+
let hi = (buf[offset + 4] | buf[offset + 5] << 8 | buf[offset + 6] << 16 | last << 24) >>> 0;
|
|
8033
|
+
if (hi > 0) {
|
|
8034
|
+
return BigInt(lo) + (BigInt(hi) << 32n);
|
|
8035
|
+
}
|
|
8036
|
+
return BigInt(lo);
|
|
8037
|
+
};
|
|
8038
|
+
function readUIntLE(buf, offset, width) {
|
|
8039
|
+
const hex = arrayToHex(buf.subarray(offset, offset + width), true);
|
|
8040
|
+
if (hex.length === 0) {
|
|
8041
|
+
return BigInt(0);
|
|
8042
|
+
}
|
|
8043
|
+
return BigInt(`0x${hex}`);
|
|
8044
|
+
}
|
|
8045
|
+
var readUInt32LE = (buffer, offset) => {
|
|
8046
|
+
const first = buffer[offset];
|
|
8047
|
+
const last = buffer[offset + 3];
|
|
8048
|
+
if (first === void 0 || last === void 0)
|
|
8049
|
+
throw new Error("Out of bounds");
|
|
8050
|
+
return first + buffer[offset + 1] * 2 ** 8 + buffer[offset + 2] * 2 ** 16 + last * 2 ** 24;
|
|
8051
|
+
};
|
|
8052
|
+
var readUInt16LE = (buffer, offset) => {
|
|
8053
|
+
const first = buffer[offset];
|
|
8054
|
+
const last = buffer[offset + 1];
|
|
8055
|
+
if (first === void 0 || last === void 0)
|
|
8056
|
+
throw new Error("Out of bounds");
|
|
8057
|
+
return first + last * 2 ** 8;
|
|
8058
|
+
};
|
|
7287
8059
|
var checkInt = (value, min, max, byteLength) => {
|
|
7288
8060
|
if (value > max || value < min) {
|
|
7289
8061
|
const n = "";
|
|
@@ -7534,6 +8306,205 @@ var BinaryWriter = class _BinaryWriter {
|
|
|
7534
8306
|
return this._buf;
|
|
7535
8307
|
}
|
|
7536
8308
|
};
|
|
8309
|
+
var BinaryReader = class _BinaryReader {
|
|
8310
|
+
constructor(buf) {
|
|
8311
|
+
this._buf = buf;
|
|
8312
|
+
this._offset = 0;
|
|
8313
|
+
}
|
|
8314
|
+
bool() {
|
|
8315
|
+
return _BinaryReader.bool(this);
|
|
8316
|
+
}
|
|
8317
|
+
static bool(reader) {
|
|
8318
|
+
const value = reader._buf[reader._offset];
|
|
8319
|
+
reader._offset += 1;
|
|
8320
|
+
if (value !== 1 && value !== 0) {
|
|
8321
|
+
throw new BorshError("Unexpected value for boolean: " + value + ". Expecting either 1 or 0 ");
|
|
8322
|
+
}
|
|
8323
|
+
return value ? true : false;
|
|
8324
|
+
}
|
|
8325
|
+
u8() {
|
|
8326
|
+
return _BinaryReader.u8(this);
|
|
8327
|
+
}
|
|
8328
|
+
static u8(reader) {
|
|
8329
|
+
if (reader._offset >= reader._buf.length) {
|
|
8330
|
+
throw new BorshError("Reader out of bounds");
|
|
8331
|
+
}
|
|
8332
|
+
const value = reader._buf[reader._offset];
|
|
8333
|
+
reader._offset += 1;
|
|
8334
|
+
return value;
|
|
8335
|
+
}
|
|
8336
|
+
u16() {
|
|
8337
|
+
return _BinaryReader.u16(this);
|
|
8338
|
+
}
|
|
8339
|
+
static u16(reader) {
|
|
8340
|
+
const value = readUInt16LE(reader._buf, reader._offset);
|
|
8341
|
+
reader._offset += 2;
|
|
8342
|
+
return value;
|
|
8343
|
+
}
|
|
8344
|
+
u32() {
|
|
8345
|
+
return _BinaryReader.u32(this);
|
|
8346
|
+
}
|
|
8347
|
+
static u32(reader) {
|
|
8348
|
+
const value = readUInt32LE(reader._buf, reader._offset);
|
|
8349
|
+
reader._offset += 4;
|
|
8350
|
+
return value;
|
|
8351
|
+
}
|
|
8352
|
+
u64() {
|
|
8353
|
+
return _BinaryReader.u64(this);
|
|
8354
|
+
}
|
|
8355
|
+
static u64(reader) {
|
|
8356
|
+
const value = readBigUInt64LE(reader._buf, reader._offset);
|
|
8357
|
+
reader._offset += 8;
|
|
8358
|
+
return value;
|
|
8359
|
+
}
|
|
8360
|
+
u128() {
|
|
8361
|
+
return _BinaryReader.u128(this);
|
|
8362
|
+
}
|
|
8363
|
+
static u128(reader) {
|
|
8364
|
+
const value = readUIntLE(reader._buf, reader._offset, 16);
|
|
8365
|
+
reader._offset += 16;
|
|
8366
|
+
return value;
|
|
8367
|
+
}
|
|
8368
|
+
u256() {
|
|
8369
|
+
return _BinaryReader.u256(this);
|
|
8370
|
+
}
|
|
8371
|
+
static u256(reader) {
|
|
8372
|
+
const value = readUIntLE(reader._buf, reader._offset, 32);
|
|
8373
|
+
reader._offset += 32;
|
|
8374
|
+
return value;
|
|
8375
|
+
}
|
|
8376
|
+
u512() {
|
|
8377
|
+
return _BinaryReader.u512(this);
|
|
8378
|
+
}
|
|
8379
|
+
static u512(reader) {
|
|
8380
|
+
const buf = reader.buffer(64);
|
|
8381
|
+
return toBigIntLE(buf);
|
|
8382
|
+
}
|
|
8383
|
+
f32() {
|
|
8384
|
+
return _BinaryReader.f32(this);
|
|
8385
|
+
}
|
|
8386
|
+
static f32(reader) {
|
|
8387
|
+
const value = (0, import_float.readFloatLE)(reader._buf, reader._offset);
|
|
8388
|
+
reader._offset += 4;
|
|
8389
|
+
if (Number.isNaN(value)) {
|
|
8390
|
+
throw new BorshError("Recieved NaN reading f32");
|
|
8391
|
+
}
|
|
8392
|
+
return value;
|
|
8393
|
+
}
|
|
8394
|
+
f64() {
|
|
8395
|
+
return _BinaryReader.f64(this);
|
|
8396
|
+
}
|
|
8397
|
+
static f64(reader) {
|
|
8398
|
+
const value = (0, import_float.readDoubleLE)(reader._buf, reader._offset);
|
|
8399
|
+
reader._offset += 8;
|
|
8400
|
+
if (Number.isNaN(value)) {
|
|
8401
|
+
throw new BorshError("Recieved NaN reading f64");
|
|
8402
|
+
}
|
|
8403
|
+
return value;
|
|
8404
|
+
}
|
|
8405
|
+
string() {
|
|
8406
|
+
return _BinaryReader.string(this);
|
|
8407
|
+
}
|
|
8408
|
+
static string(reader) {
|
|
8409
|
+
const len = reader.u32();
|
|
8410
|
+
const end = reader._offset + len;
|
|
8411
|
+
if (end > reader._buf.length) {
|
|
8412
|
+
throw new BorshError("Error decoding UTF-8 string: Invalid length");
|
|
8413
|
+
}
|
|
8414
|
+
try {
|
|
8415
|
+
const string = import_utf8.default.read(reader._buf, reader._offset, end);
|
|
8416
|
+
reader._offset = end;
|
|
8417
|
+
return string;
|
|
8418
|
+
} catch (e) {
|
|
8419
|
+
throw new BorshError(`Error decoding UTF-8 string: ${e}`);
|
|
8420
|
+
}
|
|
8421
|
+
}
|
|
8422
|
+
static bufferString(reader) {
|
|
8423
|
+
const len = reader.u32();
|
|
8424
|
+
const end = reader._offset + len;
|
|
8425
|
+
if (end > reader._buf.length) {
|
|
8426
|
+
throw new BorshError("Error decoding UTF-8 string: Invalid length");
|
|
8427
|
+
}
|
|
8428
|
+
const string = reader._buf.toString(void 0, reader._offset, end);
|
|
8429
|
+
reader._offset = end;
|
|
8430
|
+
return string;
|
|
8431
|
+
}
|
|
8432
|
+
static bufferStringCustom(reader, length) {
|
|
8433
|
+
const len = length(reader);
|
|
8434
|
+
const end = reader._offset + len;
|
|
8435
|
+
if (end > reader._buf.length) {
|
|
8436
|
+
throw new BorshError("Error decoding UTF-8 string: Invalid length");
|
|
8437
|
+
}
|
|
8438
|
+
try {
|
|
8439
|
+
const string = reader._buf.toString(void 0, reader._offset, end);
|
|
8440
|
+
reader._offset = end;
|
|
8441
|
+
return string;
|
|
8442
|
+
} catch (e) {
|
|
8443
|
+
throw new BorshError(`Error decoding UTF-8 string: ${e}`);
|
|
8444
|
+
}
|
|
8445
|
+
}
|
|
8446
|
+
static stringCustom(reader, length) {
|
|
8447
|
+
const len = length(reader);
|
|
8448
|
+
const end = reader._offset + len;
|
|
8449
|
+
if (end > reader._buf.length) {
|
|
8450
|
+
throw new BorshError("Error decoding UTF-8 string: Invalid length");
|
|
8451
|
+
}
|
|
8452
|
+
try {
|
|
8453
|
+
const string = import_utf8.default.read(reader._buf, reader._offset, end);
|
|
8454
|
+
reader._offset = end;
|
|
8455
|
+
return string;
|
|
8456
|
+
} catch (e) {
|
|
8457
|
+
throw new BorshError(`Error decoding UTF-8 string: ${e}`);
|
|
8458
|
+
}
|
|
8459
|
+
}
|
|
8460
|
+
static read(encoding, fromBuffer) {
|
|
8461
|
+
if (encoding === "u8") {
|
|
8462
|
+
return _BinaryReader.u8;
|
|
8463
|
+
} else if (encoding === "u16") {
|
|
8464
|
+
return _BinaryReader.u16;
|
|
8465
|
+
} else if (encoding === "u32") {
|
|
8466
|
+
return _BinaryReader.u32;
|
|
8467
|
+
} else if (encoding === "u64") {
|
|
8468
|
+
return _BinaryReader.u64;
|
|
8469
|
+
} else if (encoding === "u128") {
|
|
8470
|
+
return _BinaryReader.u128;
|
|
8471
|
+
} else if (encoding === "u256") {
|
|
8472
|
+
return _BinaryReader.u256;
|
|
8473
|
+
} else if (encoding === "u512") {
|
|
8474
|
+
return _BinaryReader.u512;
|
|
8475
|
+
} else if (encoding === "string") {
|
|
8476
|
+
return fromBuffer ? _BinaryReader.bufferString : _BinaryReader.string;
|
|
8477
|
+
} else if (encoding === "bool") {
|
|
8478
|
+
return _BinaryReader.bool;
|
|
8479
|
+
} else if (encoding === "f32") {
|
|
8480
|
+
return _BinaryReader.f32;
|
|
8481
|
+
} else if (encoding === "f64") {
|
|
8482
|
+
return _BinaryReader.f64;
|
|
8483
|
+
} else {
|
|
8484
|
+
throw new Error("Unsupported encoding: " + encoding);
|
|
8485
|
+
}
|
|
8486
|
+
}
|
|
8487
|
+
buffer(len) {
|
|
8488
|
+
const end = this._offset + len;
|
|
8489
|
+
const result = this._buf.subarray(this._offset, end);
|
|
8490
|
+
this._offset = end;
|
|
8491
|
+
return result;
|
|
8492
|
+
}
|
|
8493
|
+
uint8Array() {
|
|
8494
|
+
return _BinaryReader.uint8Array(this);
|
|
8495
|
+
}
|
|
8496
|
+
static uint8Array(reader, size = reader.u32()) {
|
|
8497
|
+
return reader.buffer(size);
|
|
8498
|
+
}
|
|
8499
|
+
readArray(fn) {
|
|
8500
|
+
const len = this.u32();
|
|
8501
|
+
const result = new Array(len);
|
|
8502
|
+
for (let i = 0; i < len; ++i) {
|
|
8503
|
+
result[i] = fn();
|
|
8504
|
+
}
|
|
8505
|
+
return result;
|
|
8506
|
+
}
|
|
8507
|
+
};
|
|
7537
8508
|
|
|
7538
8509
|
// node_modules/@dao-xyz/borsh/lib/esm/types.js
|
|
7539
8510
|
var extendingClasses = (clazz) => {
|
|
@@ -7572,18 +8543,53 @@ var StringType = class {
|
|
|
7572
8543
|
};
|
|
7573
8544
|
var OptionKind = class extends WrappedType {
|
|
7574
8545
|
};
|
|
8546
|
+
var option = (type) => {
|
|
8547
|
+
return new OptionKind(type);
|
|
8548
|
+
};
|
|
7575
8549
|
var VecKind = class extends WrappedType {
|
|
7576
8550
|
constructor(elementType, sizeEncoding) {
|
|
7577
8551
|
super(elementType);
|
|
7578
8552
|
this.sizeEncoding = sizeEncoding;
|
|
7579
8553
|
}
|
|
7580
8554
|
};
|
|
8555
|
+
var vec = (type, sizeEncoding = "u32") => {
|
|
8556
|
+
return new VecKind(type, sizeEncoding);
|
|
8557
|
+
};
|
|
7581
8558
|
var FixedArrayKind = class extends WrappedType {
|
|
7582
8559
|
constructor(type, length) {
|
|
7583
8560
|
super(type);
|
|
7584
8561
|
this.length = length;
|
|
7585
8562
|
}
|
|
7586
8563
|
};
|
|
8564
|
+
var fixedArray = (type, length) => {
|
|
8565
|
+
return new FixedArrayKind(type, length);
|
|
8566
|
+
};
|
|
8567
|
+
var StructKind = class {
|
|
8568
|
+
constructor(properties) {
|
|
8569
|
+
if (properties) {
|
|
8570
|
+
this.fields = properties.fields;
|
|
8571
|
+
this.variant = properties.variant;
|
|
8572
|
+
} else {
|
|
8573
|
+
this.fields = [];
|
|
8574
|
+
}
|
|
8575
|
+
}
|
|
8576
|
+
getDependencies() {
|
|
8577
|
+
let ret = [];
|
|
8578
|
+
this.fields.forEach((field2, ix) => {
|
|
8579
|
+
if (!field2) {
|
|
8580
|
+
throw new BorshError("Field: " + ix + " is missing specification");
|
|
8581
|
+
}
|
|
8582
|
+
if (field2.type instanceof WrappedType) {
|
|
8583
|
+
let dependency = field2.type.getDependency();
|
|
8584
|
+
if (dependency)
|
|
8585
|
+
ret.push(dependency);
|
|
8586
|
+
} else if (typeof field2.type === "function") {
|
|
8587
|
+
ret.push(field2.type);
|
|
8588
|
+
}
|
|
8589
|
+
});
|
|
8590
|
+
return ret;
|
|
8591
|
+
}
|
|
8592
|
+
};
|
|
7587
8593
|
|
|
7588
8594
|
// node_modules/@dao-xyz/borsh/lib/esm/index.js
|
|
7589
8595
|
var MAX_PROTOTYPE_SEARCH = 250;
|
|
@@ -7591,7 +8597,7 @@ var PROTOTYPE_POLLUTION_CONTEXT_RANGE = 500;
|
|
|
7591
8597
|
var PROTOTYPE_DESERIALIZATION_HANDLER_OFFSET = 500;
|
|
7592
8598
|
var PROTOTYPE_DEPENDENCY_HANDLER_OFFSET = PROTOTYPE_DESERIALIZATION_HANDLER_OFFSET + PROTOTYPE_POLLUTION_CONTEXT_RANGE;
|
|
7593
8599
|
var PROTOTYPE_SCHEMA_OFFSET = PROTOTYPE_DESERIALIZATION_HANDLER_OFFSET + PROTOTYPE_POLLUTION_CONTEXT_RANGE * 2;
|
|
7594
|
-
function
|
|
8600
|
+
function serialize2(obj, writer = new BinaryWriter()) {
|
|
7595
8601
|
(obj.constructor._borsh_serialize || (obj.constructor._borsh_serialize = serializeStruct(obj.constructor, true)))(obj, writer);
|
|
7596
8602
|
return writer.finalize();
|
|
7597
8603
|
}
|
|
@@ -7599,6 +8605,15 @@ function recursiveSerialize(obj, writer = new BinaryWriter()) {
|
|
|
7599
8605
|
(obj.constructor._borsh_serialize_recursive || (obj.constructor._borsh_serialize_recursive = serializeStruct(obj.constructor, false)))(obj, writer);
|
|
7600
8606
|
return writer.finalize();
|
|
7601
8607
|
}
|
|
8608
|
+
function deserialize2(buffer, classType, options) {
|
|
8609
|
+
const reader = new BinaryReader(buffer);
|
|
8610
|
+
let fromBuffer = buffer.constructor !== Uint8Array;
|
|
8611
|
+
const result = deserializeStruct(classType, fromBuffer)(reader, options);
|
|
8612
|
+
if (reader._offset !== buffer.length) {
|
|
8613
|
+
throw new BorshError(`Unexpected ${buffer.length - reader._offset} bytes after deserialized data. This is most likely due to that you are deserializing into the wrong class`);
|
|
8614
|
+
}
|
|
8615
|
+
return result;
|
|
8616
|
+
}
|
|
7602
8617
|
function serializeField(fieldName, fieldType, options) {
|
|
7603
8618
|
if (typeof fieldType.serialize == "function") {
|
|
7604
8619
|
return (obj, writer) => fieldType.serialize(obj, writer);
|
|
@@ -7719,94 +8734,517 @@ function serializeStruct(ctor, allowCustomSerializer = true) {
|
|
|
7719
8734
|
} : (_obj, writer) => writer.string(index);
|
|
7720
8735
|
}
|
|
7721
8736
|
}
|
|
7722
|
-
if (allowCustomSerializer && schema.serializer) {
|
|
7723
|
-
let prev = handle;
|
|
7724
|
-
handle = prev ? (obj, writer) => {
|
|
7725
|
-
prev(obj, writer);
|
|
7726
|
-
schema.serializer(obj, writer, (obj2) => recursiveSerialize(obj2));
|
|
7727
|
-
} : (obj, writer) => schema.serializer(obj, writer, (obj2) => recursiveSerialize(obj2));
|
|
7728
|
-
} else {
|
|
7729
|
-
for (const field2 of schema.fields) {
|
|
7730
|
-
let prev = handle;
|
|
7731
|
-
const fieldHandle = serializeField(field2.key, field2.type);
|
|
7732
|
-
if (prev) {
|
|
7733
|
-
handle = (obj, writer) => {
|
|
7734
|
-
prev(obj, writer);
|
|
7735
|
-
fieldHandle(obj[field2.key], writer);
|
|
7736
|
-
};
|
|
7737
|
-
} else {
|
|
7738
|
-
handle = (obj, writer) => fieldHandle(obj[field2.key], writer);
|
|
7739
|
-
}
|
|
8737
|
+
if (allowCustomSerializer && schema.serializer) {
|
|
8738
|
+
let prev = handle;
|
|
8739
|
+
handle = prev ? (obj, writer) => {
|
|
8740
|
+
prev(obj, writer);
|
|
8741
|
+
schema.serializer(obj, writer, (obj2) => recursiveSerialize(obj2));
|
|
8742
|
+
} : (obj, writer) => schema.serializer(obj, writer, (obj2) => recursiveSerialize(obj2));
|
|
8743
|
+
} else {
|
|
8744
|
+
for (const field2 of schema.fields) {
|
|
8745
|
+
let prev = handle;
|
|
8746
|
+
const fieldHandle = serializeField(field2.key, field2.type);
|
|
8747
|
+
if (prev) {
|
|
8748
|
+
handle = (obj, writer) => {
|
|
8749
|
+
prev(obj, writer);
|
|
8750
|
+
fieldHandle(obj[field2.key], writer);
|
|
8751
|
+
};
|
|
8752
|
+
} else {
|
|
8753
|
+
handle = (obj, writer) => fieldHandle(obj[field2.key], writer);
|
|
8754
|
+
}
|
|
8755
|
+
}
|
|
8756
|
+
}
|
|
8757
|
+
} else if (once && !getDependencies(ctor, i)?.length) {
|
|
8758
|
+
return handle;
|
|
8759
|
+
}
|
|
8760
|
+
i++;
|
|
8761
|
+
if (i == MAX_PROTOTYPE_SEARCH && !once) {
|
|
8762
|
+
throw new BorshError(`Class ${ctor.name} is missing in schema`);
|
|
8763
|
+
}
|
|
8764
|
+
}
|
|
8765
|
+
}
|
|
8766
|
+
var MAX_ARRAY_SIZE_ALLOCATION = 1024 * 1024;
|
|
8767
|
+
function deserializeField(fieldName, fieldType, fromBuffer) {
|
|
8768
|
+
try {
|
|
8769
|
+
if (typeof fieldType === "string") {
|
|
8770
|
+
return BinaryReader.read(fieldType, fromBuffer);
|
|
8771
|
+
}
|
|
8772
|
+
if (fieldType === Uint8Array) {
|
|
8773
|
+
return (reader) => reader.uint8Array();
|
|
8774
|
+
}
|
|
8775
|
+
if (fieldType instanceof VecKind || fieldType instanceof FixedArrayKind) {
|
|
8776
|
+
if (fieldType.elementType === "u8") {
|
|
8777
|
+
if (fieldType instanceof FixedArrayKind) {
|
|
8778
|
+
return (reader) => reader.buffer(fieldType.length);
|
|
8779
|
+
} else {
|
|
8780
|
+
const sizeHandle = BinaryReader.read(fieldType.sizeEncoding, fromBuffer);
|
|
8781
|
+
return (reader) => BinaryReader.uint8Array(reader, sizeHandle(reader));
|
|
8782
|
+
}
|
|
8783
|
+
} else {
|
|
8784
|
+
let sizeHandle = fieldType instanceof VecKind ? BinaryReader.read(fieldType.sizeEncoding, fromBuffer) : () => fieldType.length;
|
|
8785
|
+
const fieldHandle = deserializeField(null, fieldType.elementType, fromBuffer);
|
|
8786
|
+
return (reader, options) => {
|
|
8787
|
+
const len = sizeHandle(reader);
|
|
8788
|
+
if (len < MAX_ARRAY_SIZE_ALLOCATION) {
|
|
8789
|
+
let arr = new Array(len);
|
|
8790
|
+
for (let i = 0; i < len; i++) {
|
|
8791
|
+
arr[i] = fieldHandle(reader, options);
|
|
8792
|
+
}
|
|
8793
|
+
return arr;
|
|
8794
|
+
} else {
|
|
8795
|
+
let arr = new Array(MAX_ARRAY_SIZE_ALLOCATION);
|
|
8796
|
+
for (let i = 0; i < len; i++) {
|
|
8797
|
+
arr[i] = fieldHandle(reader, options);
|
|
8798
|
+
}
|
|
8799
|
+
return arr;
|
|
8800
|
+
}
|
|
8801
|
+
};
|
|
8802
|
+
}
|
|
8803
|
+
}
|
|
8804
|
+
if (fieldType instanceof StringType) {
|
|
8805
|
+
const sizeReader = BinaryReader.read(fieldType.sizeEncoding, fromBuffer);
|
|
8806
|
+
return fromBuffer ? (reader) => BinaryReader.bufferStringCustom(reader, sizeReader) : (reader) => BinaryReader.stringCustom(reader, sizeReader);
|
|
8807
|
+
}
|
|
8808
|
+
if (typeof fieldType["deserialize"] == "function") {
|
|
8809
|
+
return (reader) => fieldType.deserialize(reader);
|
|
8810
|
+
}
|
|
8811
|
+
if (fieldType instanceof OptionKind) {
|
|
8812
|
+
const fieldHandle = deserializeField(fieldName, fieldType.elementType, fromBuffer);
|
|
8813
|
+
return (reader, options) => {
|
|
8814
|
+
return reader.bool() ? fieldHandle(reader, options) : void 0;
|
|
8815
|
+
};
|
|
8816
|
+
}
|
|
8817
|
+
return deserializeStruct(fieldType, fromBuffer);
|
|
8818
|
+
} catch (error) {
|
|
8819
|
+
if (error instanceof BorshError) {
|
|
8820
|
+
error.addToFieldPath(fieldName);
|
|
8821
|
+
}
|
|
8822
|
+
throw error;
|
|
8823
|
+
}
|
|
8824
|
+
}
|
|
8825
|
+
function deserializeStruct(targetClazz, fromBuffer) {
|
|
8826
|
+
const handle = getCreateDeserializationHandle(targetClazz, 0, fromBuffer);
|
|
8827
|
+
return (reader, options) => {
|
|
8828
|
+
const result = handle({}, reader, options);
|
|
8829
|
+
if (!options?.unchecked && !options?.object && !checkClazzesCompatible(result.constructor, targetClazz)) {
|
|
8830
|
+
throw new BorshError(`Deserialization of ${targetClazz?.name || targetClazz} yielded another Class: ${result.constructor?.name} which are not compatible`);
|
|
8831
|
+
}
|
|
8832
|
+
return result;
|
|
8833
|
+
};
|
|
8834
|
+
}
|
|
8835
|
+
var getCreateDeserializationHandle = (clazz, offset, fromBuffer) => getDeserializationHandle(clazz, offset, fromBuffer) || setDeserializationHandle(clazz, offset, fromBuffer, createDeserializeStructHandle(clazz, offset, fromBuffer));
|
|
8836
|
+
var getDeserializationHandle = (clazz, offset, fromBuffer) => clazz.prototype[PROTOTYPE_DESERIALIZATION_HANDLER_OFFSET + offset + (fromBuffer ? MAX_PROTOTYPE_SEARCH : 0)];
|
|
8837
|
+
var setDeserializationHandle = (clazz, offset, fromBuffer, handle) => clazz.prototype[PROTOTYPE_DESERIALIZATION_HANDLER_OFFSET + offset + (fromBuffer ? MAX_PROTOTYPE_SEARCH : 0)] = handle;
|
|
8838
|
+
var createDeserializeStructHandle = (currClazz, offset, fromBuffer) => {
|
|
8839
|
+
let handle = void 0;
|
|
8840
|
+
let endHandle = (result, reader, options) => {
|
|
8841
|
+
if (options?.object) {
|
|
8842
|
+
return result;
|
|
8843
|
+
}
|
|
8844
|
+
return Object.assign(options?.construct ? new currClazz() : Object.create(currClazz.prototype), result);
|
|
8845
|
+
};
|
|
8846
|
+
let structSchema = getSchema(currClazz, offset);
|
|
8847
|
+
if (structSchema) {
|
|
8848
|
+
if (offset === 0) {
|
|
8849
|
+
let index = getVariantIndex(structSchema);
|
|
8850
|
+
if (index != null) {
|
|
8851
|
+
if (typeof index === "number") {
|
|
8852
|
+
handle = (_, reader, __) => {
|
|
8853
|
+
reader._offset += 1;
|
|
8854
|
+
};
|
|
8855
|
+
} else if (Array.isArray(index)) {
|
|
8856
|
+
handle = (_, reader, __) => {
|
|
8857
|
+
reader._offset += index.length;
|
|
8858
|
+
};
|
|
8859
|
+
} else {
|
|
8860
|
+
handle = (_, reader, __) => {
|
|
8861
|
+
reader.string();
|
|
8862
|
+
};
|
|
8863
|
+
}
|
|
8864
|
+
}
|
|
8865
|
+
}
|
|
8866
|
+
for (const field2 of structSchema.fields) {
|
|
8867
|
+
const prev = handle;
|
|
8868
|
+
const fieldHandle = deserializeField(field2.key, field2.type, fromBuffer);
|
|
8869
|
+
if (prev) {
|
|
8870
|
+
handle = (result, reader, options) => {
|
|
8871
|
+
prev(result, reader, options);
|
|
8872
|
+
result[field2.key] = fieldHandle(reader, options);
|
|
8873
|
+
};
|
|
8874
|
+
} else
|
|
8875
|
+
handle = (result, reader, options) => {
|
|
8876
|
+
result[field2.key] = fieldHandle(reader, options);
|
|
8877
|
+
};
|
|
8878
|
+
}
|
|
8879
|
+
}
|
|
8880
|
+
let dependencies = getAllDependencies(currClazz, offset);
|
|
8881
|
+
if (dependencies) {
|
|
8882
|
+
let variantToDepndency = [];
|
|
8883
|
+
let variantType;
|
|
8884
|
+
for (const [actualClazz, dependency] of dependencies) {
|
|
8885
|
+
const variantIndex = getVariantIndex(dependency.schema);
|
|
8886
|
+
let currentVariantType = typeof variantIndex === "object" ? variantIndex.length : typeof variantIndex;
|
|
8887
|
+
if (!variantType) {
|
|
8888
|
+
variantType = currentVariantType;
|
|
8889
|
+
} else if (currentVariantType !== variantType) {
|
|
8890
|
+
throw new Error(`Variant extending ${currClazz.name} have different types, expecting either number, number[] (with same sizes) or string, but not a combination of them`);
|
|
8891
|
+
}
|
|
8892
|
+
variantToDepndency.push([variantIndex, actualClazz, dependency]);
|
|
8893
|
+
}
|
|
8894
|
+
if (variantType === "undefined") {
|
|
8895
|
+
if (dependencies.size === 1) {
|
|
8896
|
+
const dep = variantToDepndency[0];
|
|
8897
|
+
return (result, reader, options) => {
|
|
8898
|
+
handle && handle(result, reader, options);
|
|
8899
|
+
return getCreateDeserializationHandle(dep[1], dep[2].offset, fromBuffer)(result, reader, options);
|
|
8900
|
+
};
|
|
8901
|
+
} else
|
|
8902
|
+
throw new BorshError(`Failed to find class to deserialize to from ${currClazz.name}: but no variants are used which makes deserialization undeterministic`);
|
|
8903
|
+
}
|
|
8904
|
+
return (result, reader, options) => {
|
|
8905
|
+
handle && handle(result, reader, options);
|
|
8906
|
+
let next = void 0;
|
|
8907
|
+
let nextOffset = void 0;
|
|
8908
|
+
if (variantType === "number") {
|
|
8909
|
+
let agg = reader.u8();
|
|
8910
|
+
for (const dep of variantToDepndency) {
|
|
8911
|
+
if (agg === dep[0]) {
|
|
8912
|
+
return getCreateDeserializationHandle(dep[1], dep[2].offset, fromBuffer)(result, reader, options);
|
|
8913
|
+
}
|
|
8914
|
+
}
|
|
8915
|
+
} else if (variantType === "string") {
|
|
8916
|
+
let variant = reader.string();
|
|
8917
|
+
for (const dep of variantToDepndency) {
|
|
8918
|
+
if (variant === dep[0]) {
|
|
8919
|
+
return getCreateDeserializationHandle(dep[1], dep[2].offset, fromBuffer)(result, reader, options);
|
|
8920
|
+
}
|
|
8921
|
+
}
|
|
8922
|
+
} else {
|
|
8923
|
+
let agg = [];
|
|
8924
|
+
for (let i = 0; i < variantType; i++) {
|
|
8925
|
+
agg.push(reader.u8());
|
|
8926
|
+
}
|
|
8927
|
+
for (const dep of variantToDepndency) {
|
|
8928
|
+
let currentVariant = dep[0];
|
|
8929
|
+
if (currentVariant.length === agg.length && currentVariant.every((value, index) => value === agg[index])) {
|
|
8930
|
+
return getCreateDeserializationHandle(dep[1], dep[2].offset, fromBuffer)(result, reader, options);
|
|
8931
|
+
}
|
|
8932
|
+
}
|
|
8933
|
+
}
|
|
8934
|
+
if (next == void 0 && dependencies) {
|
|
8935
|
+
if (dependencies.size == 1) {
|
|
8936
|
+
const n = dependencies.entries().next().value;
|
|
8937
|
+
next = n[0];
|
|
8938
|
+
nextOffset = n[1].offset;
|
|
8939
|
+
} else if (dependencies.size > 1) {
|
|
8940
|
+
const classes = [...dependencies.entries()].map(([c]) => c.name).join(", ");
|
|
8941
|
+
throw new BorshError(`Failed to find class to deserialize to from ${currClazz.name} found: ${classes} but no variant matches bytes read from the buffer.`);
|
|
8942
|
+
}
|
|
8943
|
+
}
|
|
8944
|
+
if (next != null) {
|
|
8945
|
+
getCreateDeserializationHandle(next, nextOffset, fromBuffer)(result, reader, options);
|
|
8946
|
+
} else {
|
|
8947
|
+
return endHandle(result, reader, options);
|
|
8948
|
+
}
|
|
8949
|
+
};
|
|
8950
|
+
} else {
|
|
8951
|
+
if (handle) {
|
|
8952
|
+
return (result, reader, options) => {
|
|
8953
|
+
handle(result, reader, options);
|
|
8954
|
+
return endHandle(result, reader, options);
|
|
8955
|
+
};
|
|
8956
|
+
}
|
|
8957
|
+
return endHandle;
|
|
8958
|
+
}
|
|
8959
|
+
};
|
|
8960
|
+
var getOrCreateStructMeta = (clazz, offset) => {
|
|
8961
|
+
let schema = getSchema(clazz, offset);
|
|
8962
|
+
if (!schema) {
|
|
8963
|
+
schema = new StructKind();
|
|
8964
|
+
}
|
|
8965
|
+
setSchema(clazz, schema, offset);
|
|
8966
|
+
return schema;
|
|
8967
|
+
};
|
|
8968
|
+
var setDependencyToProtoType = (ctor, offset) => {
|
|
8969
|
+
let proto = Object.getPrototypeOf(ctor);
|
|
8970
|
+
while (proto.prototype?.constructor != void 0) {
|
|
8971
|
+
let newOffset = --offset;
|
|
8972
|
+
let dependencies = getDependencies(proto, newOffset);
|
|
8973
|
+
if (dependencies) {
|
|
8974
|
+
for (const dependency of dependencies) {
|
|
8975
|
+
if (ctor.prototype instanceof dependency || dependency === ctor) {
|
|
8976
|
+
return;
|
|
8977
|
+
}
|
|
8978
|
+
}
|
|
8979
|
+
} else {
|
|
8980
|
+
dependencies = [];
|
|
8981
|
+
}
|
|
8982
|
+
dependencies.push(ctor);
|
|
8983
|
+
setDependencies(proto, newOffset, dependencies);
|
|
8984
|
+
proto = Object.getPrototypeOf(proto);
|
|
8985
|
+
}
|
|
8986
|
+
};
|
|
8987
|
+
var getSuperMostClass = (clazz) => {
|
|
8988
|
+
while (Object.getPrototypeOf(clazz).prototype != void 0) {
|
|
8989
|
+
clazz = Object.getPrototypeOf(clazz);
|
|
8990
|
+
}
|
|
8991
|
+
return clazz;
|
|
8992
|
+
};
|
|
8993
|
+
var checkClazzesCompatible = (clazzA, clazzB) => {
|
|
8994
|
+
return clazzA == clazzB || clazzA.isPrototypeOf(clazzB) || clazzB.isPrototypeOf(clazzA);
|
|
8995
|
+
};
|
|
8996
|
+
var getDependencies = (ctor, offset) => ctor.prototype[PROTOTYPE_DEPENDENCY_HANDLER_OFFSET + offset];
|
|
8997
|
+
var setDependencies = (ctor, offset, dependencies) => {
|
|
8998
|
+
ctor.prototype[PROTOTYPE_DEPENDENCY_HANDLER_OFFSET + offset] = dependencies;
|
|
8999
|
+
};
|
|
9000
|
+
var getAllDependencies = (ctor, offset) => {
|
|
9001
|
+
let existing = getDependencies(ctor, offset);
|
|
9002
|
+
if (existing) {
|
|
9003
|
+
let ret = /* @__PURE__ */ new Map();
|
|
9004
|
+
for (const v of existing) {
|
|
9005
|
+
let schema = getSubMostSchema(v);
|
|
9006
|
+
if (schema.fields.length > 0 || schema.variant != void 0) {
|
|
9007
|
+
ret.set(v, { schema, offset: getOffset(v) });
|
|
9008
|
+
} else {
|
|
9009
|
+
let req = getAllDependencies(v, offset);
|
|
9010
|
+
for (const [rv, rk] of req) {
|
|
9011
|
+
ret.set(rv, rk);
|
|
9012
|
+
}
|
|
9013
|
+
}
|
|
9014
|
+
}
|
|
9015
|
+
return ret;
|
|
9016
|
+
}
|
|
9017
|
+
};
|
|
9018
|
+
var setSchema = (ctor, schemas, offset) => {
|
|
9019
|
+
ctor.prototype[PROTOTYPE_SCHEMA_OFFSET + offset] = schemas;
|
|
9020
|
+
};
|
|
9021
|
+
var getSchema = (ctor, offset = getOffset(ctor)) => ctor.prototype[PROTOTYPE_SCHEMA_OFFSET + offset];
|
|
9022
|
+
var getSubMostSchema = (ctor) => {
|
|
9023
|
+
let last = void 0;
|
|
9024
|
+
for (var i = 0; i < MAX_PROTOTYPE_SEARCH; i++) {
|
|
9025
|
+
const curr = ctor.prototype[PROTOTYPE_SCHEMA_OFFSET + i];
|
|
9026
|
+
if (!curr && last && !getDependencies(ctor, i)?.length) {
|
|
9027
|
+
return last;
|
|
9028
|
+
}
|
|
9029
|
+
last = curr;
|
|
9030
|
+
}
|
|
9031
|
+
return;
|
|
9032
|
+
};
|
|
9033
|
+
var getVariantIndex = (schema) => {
|
|
9034
|
+
return schema.variant;
|
|
9035
|
+
};
|
|
9036
|
+
function field(properties) {
|
|
9037
|
+
return (target, name) => {
|
|
9038
|
+
const offset = getOffset(target.constructor);
|
|
9039
|
+
setDependencyToProtoType(target.constructor, offset);
|
|
9040
|
+
const schemas = getOrCreateStructMeta(target.constructor, offset);
|
|
9041
|
+
const schema = schemas;
|
|
9042
|
+
const key = name.toString();
|
|
9043
|
+
let field2 = void 0;
|
|
9044
|
+
if (properties["type"] != void 0) {
|
|
9045
|
+
field2 = {
|
|
9046
|
+
key,
|
|
9047
|
+
type: properties["type"]
|
|
9048
|
+
};
|
|
9049
|
+
} else {
|
|
9050
|
+
field2 = {
|
|
9051
|
+
key,
|
|
9052
|
+
type: properties
|
|
9053
|
+
};
|
|
9054
|
+
}
|
|
9055
|
+
if (properties.index === void 0) {
|
|
9056
|
+
schema.fields.push(field2);
|
|
9057
|
+
} else {
|
|
9058
|
+
if (schema.fields[properties.index]) {
|
|
9059
|
+
throw new BorshError("Multiple fields defined at the same index: " + properties.index + ", class: " + target.constructor.name);
|
|
9060
|
+
}
|
|
9061
|
+
if (properties.index >= schema.fields.length) {
|
|
9062
|
+
resize(schema.fields, properties.index + 1, void 0);
|
|
9063
|
+
}
|
|
9064
|
+
schema.fields[properties.index] = field2;
|
|
9065
|
+
}
|
|
9066
|
+
};
|
|
9067
|
+
}
|
|
9068
|
+
var resize = (arr, newSize, defaultValue) => {
|
|
9069
|
+
while (newSize > arr.length)
|
|
9070
|
+
arr.push(defaultValue);
|
|
9071
|
+
arr.length = newSize;
|
|
9072
|
+
};
|
|
9073
|
+
|
|
9074
|
+
// src/serialization/borsh.ts
|
|
9075
|
+
function serializeBorsh(data) {
|
|
9076
|
+
return serialize2(data);
|
|
9077
|
+
}
|
|
9078
|
+
function deserializeBorsh(data, type) {
|
|
9079
|
+
return deserialize2(data, type);
|
|
9080
|
+
}
|
|
9081
|
+
|
|
9082
|
+
// src/transaction/account-meta-table.ts
|
|
9083
|
+
var AccountMetaTable = class {
|
|
9084
|
+
accounts = /* @__PURE__ */ new Map();
|
|
9085
|
+
accountOrder = [];
|
|
9086
|
+
/**
|
|
9087
|
+
* Add or update an account in the table.
|
|
9088
|
+
* If account already exists, merges signer/writable flags (OR operation).
|
|
9089
|
+
*/
|
|
9090
|
+
add(meta) {
|
|
9091
|
+
const key = meta.pubkey.toString();
|
|
9092
|
+
if (this.accounts.has(key)) {
|
|
9093
|
+
const existing = this.accounts.get(key);
|
|
9094
|
+
existing.isSigner ||= meta.isSigner;
|
|
9095
|
+
existing.isWritable ||= meta.isWritable;
|
|
9096
|
+
} else {
|
|
9097
|
+
this.accounts.set(key, {
|
|
9098
|
+
pubkey: meta.pubkey,
|
|
9099
|
+
isSigner: meta.isSigner,
|
|
9100
|
+
isWritable: meta.isWritable
|
|
9101
|
+
});
|
|
9102
|
+
this.accountOrder.push(key);
|
|
9103
|
+
}
|
|
9104
|
+
}
|
|
9105
|
+
/**
|
|
9106
|
+
* Add multiple accounts at once.
|
|
9107
|
+
*/
|
|
9108
|
+
addAll(metas) {
|
|
9109
|
+
for (const meta of metas) {
|
|
9110
|
+
this.add(meta);
|
|
9111
|
+
}
|
|
9112
|
+
}
|
|
9113
|
+
/**
|
|
9114
|
+
* Get the index of an account in the sorted order.
|
|
9115
|
+
* Returns -1 if account not found.
|
|
9116
|
+
*/
|
|
9117
|
+
getIndex(pubkey) {
|
|
9118
|
+
const sorted = this.getSortedAccounts();
|
|
9119
|
+
return sorted.findIndex((entry) => entry.pubkey.equals(pubkey));
|
|
9120
|
+
}
|
|
9121
|
+
/**
|
|
9122
|
+
* Get sorted accounts according to transaction rules.
|
|
9123
|
+
*/
|
|
9124
|
+
getSortedAccounts() {
|
|
9125
|
+
const entries = this.accountOrder.map((key) => this.accounts.get(key));
|
|
9126
|
+
return entries.sort((a, b) => {
|
|
9127
|
+
const aPriority = a.isSigner && a.isWritable ? 0 : a.isSigner ? 1 : a.isWritable ? 2 : 3;
|
|
9128
|
+
const bPriority = b.isSigner && b.isWritable ? 0 : b.isSigner ? 1 : b.isWritable ? 2 : 3;
|
|
9129
|
+
if (aPriority !== bPriority) {
|
|
9130
|
+
return aPriority - bPriority;
|
|
9131
|
+
}
|
|
9132
|
+
const aBytes = a.pubkey.toBytes();
|
|
9133
|
+
const bBytes = b.pubkey.toBytes();
|
|
9134
|
+
for (let i = 0; i < 32; i++) {
|
|
9135
|
+
if (aBytes[i] !== bBytes[i]) {
|
|
9136
|
+
return aBytes[i] - bBytes[i];
|
|
9137
|
+
}
|
|
9138
|
+
}
|
|
9139
|
+
return 0;
|
|
9140
|
+
});
|
|
9141
|
+
}
|
|
9142
|
+
/**
|
|
9143
|
+
* Get the message header based on sorted accounts.
|
|
9144
|
+
*/
|
|
9145
|
+
getHeader() {
|
|
9146
|
+
const sorted = this.getSortedAccounts();
|
|
9147
|
+
let numRequiredSignatures = 0;
|
|
9148
|
+
let numReadonlySignedAccounts = 0;
|
|
9149
|
+
let numReadonlyUnsignedAccounts = 0;
|
|
9150
|
+
for (const entry of sorted) {
|
|
9151
|
+
if (entry.isSigner) {
|
|
9152
|
+
numRequiredSignatures++;
|
|
9153
|
+
if (!entry.isWritable) {
|
|
9154
|
+
numReadonlySignedAccounts++;
|
|
7740
9155
|
}
|
|
9156
|
+
} else if (!entry.isWritable) {
|
|
9157
|
+
numReadonlyUnsignedAccounts++;
|
|
7741
9158
|
}
|
|
7742
|
-
} else if (once && !getDependencies(ctor, i)?.length) {
|
|
7743
|
-
return handle;
|
|
7744
|
-
}
|
|
7745
|
-
i++;
|
|
7746
|
-
if (i == MAX_PROTOTYPE_SEARCH && !once) {
|
|
7747
|
-
throw new BorshError(`Class ${ctor.name} is missing in schema`);
|
|
7748
9159
|
}
|
|
9160
|
+
return {
|
|
9161
|
+
numRequiredSignatures,
|
|
9162
|
+
numReadonlySignedAccounts,
|
|
9163
|
+
numReadonlyUnsignedAccounts
|
|
9164
|
+
};
|
|
7749
9165
|
}
|
|
7750
|
-
|
|
7751
|
-
|
|
7752
|
-
|
|
7753
|
-
|
|
9166
|
+
/**
|
|
9167
|
+
* Get sorted public keys.
|
|
9168
|
+
*/
|
|
9169
|
+
getPublicKeys() {
|
|
9170
|
+
return this.getSortedAccounts().map((entry) => entry.pubkey);
|
|
9171
|
+
}
|
|
9172
|
+
/**
|
|
9173
|
+
* Get number of accounts in table.
|
|
9174
|
+
*/
|
|
9175
|
+
size() {
|
|
9176
|
+
return this.accounts.size;
|
|
7754
9177
|
}
|
|
7755
|
-
return clazz;
|
|
7756
|
-
};
|
|
7757
|
-
var checkClazzesCompatible = (clazzA, clazzB) => {
|
|
7758
|
-
return clazzA == clazzB || clazzA.isPrototypeOf(clazzB) || clazzB.isPrototypeOf(clazzA);
|
|
7759
9178
|
};
|
|
7760
|
-
var getDependencies = (ctor, offset) => ctor.prototype[PROTOTYPE_DEPENDENCY_HANDLER_OFFSET + offset];
|
|
7761
|
-
var getSchema = (ctor, offset = getOffset(ctor)) => ctor.prototype[PROTOTYPE_SCHEMA_OFFSET + offset];
|
|
7762
|
-
|
|
7763
|
-
// src/serialization/borsh.ts
|
|
7764
|
-
function serializeBorsh(data) {
|
|
7765
|
-
return serialize(data);
|
|
7766
|
-
}
|
|
7767
9179
|
|
|
7768
|
-
// src/
|
|
7769
|
-
|
|
7770
|
-
|
|
7771
|
-
|
|
7772
|
-
|
|
7773
|
-
|
|
7774
|
-
|
|
9180
|
+
// src/transaction/errors.ts
|
|
9181
|
+
var TransactionErrorCode = /* @__PURE__ */ ((TransactionErrorCode2) => {
|
|
9182
|
+
TransactionErrorCode2["INVALID_INSTRUCTION"] = "INVALID_INSTRUCTION";
|
|
9183
|
+
TransactionErrorCode2["INVALID_ACCOUNT_META"] = "INVALID_ACCOUNT_META";
|
|
9184
|
+
TransactionErrorCode2["NO_INSTRUCTIONS"] = "NO_INSTRUCTIONS";
|
|
9185
|
+
TransactionErrorCode2["DUPLICATE_ACCOUNT"] = "DUPLICATE_ACCOUNT";
|
|
9186
|
+
TransactionErrorCode2["MISSING_SIGNATURE"] = "MISSING_SIGNATURE";
|
|
9187
|
+
TransactionErrorCode2["INVALID_SIGNATURE"] = "INVALID_SIGNATURE";
|
|
9188
|
+
TransactionErrorCode2["SIGNATURE_OUT_OF_BOUNDS"] = "SIGNATURE_OUT_OF_BOUNDS";
|
|
9189
|
+
TransactionErrorCode2["SIGNER_NOT_FOUND"] = "SIGNER_NOT_FOUND";
|
|
9190
|
+
TransactionErrorCode2["ALREADY_SIGNED"] = "ALREADY_SIGNED";
|
|
9191
|
+
TransactionErrorCode2["INVALID_MESSAGE_FORMAT"] = "INVALID_MESSAGE_FORMAT";
|
|
9192
|
+
TransactionErrorCode2["INSUFFICIENT_DATA"] = "INSUFFICIENT_DATA";
|
|
9193
|
+
TransactionErrorCode2["SERIALIZATION_FAILED"] = "SERIALIZATION_FAILED";
|
|
9194
|
+
TransactionErrorCode2["SIMULATION_FAILED"] = "SIMULATION_FAILED";
|
|
9195
|
+
TransactionErrorCode2["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
|
|
9196
|
+
TransactionErrorCode2["BLOCKHASH_EXPIRED"] = "BLOCKHASH_EXPIRED";
|
|
9197
|
+
return TransactionErrorCode2;
|
|
9198
|
+
})(TransactionErrorCode || {});
|
|
9199
|
+
var TransactionError = class _TransactionError extends Error {
|
|
9200
|
+
constructor(code, message, details) {
|
|
9201
|
+
super(message);
|
|
9202
|
+
this.code = code;
|
|
9203
|
+
this.details = details;
|
|
9204
|
+
this.name = "TransactionError";
|
|
9205
|
+
}
|
|
9206
|
+
static noInstructions() {
|
|
9207
|
+
return new _TransactionError(
|
|
9208
|
+
"NO_INSTRUCTIONS" /* NO_INSTRUCTIONS */,
|
|
9209
|
+
"Transaction must contain at least one instruction. Use addInstruction() to add instructions."
|
|
7775
9210
|
);
|
|
7776
9211
|
}
|
|
7777
|
-
|
|
7778
|
-
|
|
7779
|
-
|
|
9212
|
+
static signerNotFound(pubkey) {
|
|
9213
|
+
return new _TransactionError(
|
|
9214
|
+
"SIGNER_NOT_FOUND" /* SIGNER_NOT_FOUND */,
|
|
9215
|
+
`Public key ${pubkey} is not a required signer for this transaction. Check that the account is marked as a signer in the instruction.`,
|
|
9216
|
+
{ pubkey }
|
|
9217
|
+
);
|
|
7780
9218
|
}
|
|
7781
|
-
|
|
7782
|
-
|
|
7783
|
-
"
|
|
7784
|
-
|
|
9219
|
+
static missingSignature(index, pubkey) {
|
|
9220
|
+
return new _TransactionError(
|
|
9221
|
+
"MISSING_SIGNATURE" /* MISSING_SIGNATURE */,
|
|
9222
|
+
`Missing signature for signer ${index} (${pubkey}). Transaction requires ${index + 1} signatures.`,
|
|
9223
|
+
{ index, pubkey }
|
|
7785
9224
|
);
|
|
7786
9225
|
}
|
|
7787
|
-
|
|
7788
|
-
|
|
7789
|
-
|
|
9226
|
+
static simulationFailed(logs, error) {
|
|
9227
|
+
return new _TransactionError(
|
|
9228
|
+
"SIMULATION_FAILED" /* SIMULATION_FAILED */,
|
|
9229
|
+
`Transaction simulation failed: ${error}`,
|
|
9230
|
+
{ logs, error }
|
|
9231
|
+
);
|
|
7790
9232
|
}
|
|
7791
|
-
|
|
7792
|
-
|
|
7793
|
-
"
|
|
7794
|
-
|
|
9233
|
+
static insufficientFunds(required, available) {
|
|
9234
|
+
return new _TransactionError(
|
|
9235
|
+
"INSUFFICIENT_FUNDS" /* INSUFFICIENT_FUNDS */,
|
|
9236
|
+
`Insufficient funds: need ${required}, have ${available}`,
|
|
9237
|
+
{ required: required.toString(), available: available.toString() }
|
|
7795
9238
|
);
|
|
7796
9239
|
}
|
|
7797
|
-
|
|
7798
|
-
|
|
7799
|
-
|
|
7800
|
-
|
|
7801
|
-
|
|
7802
|
-
|
|
7803
|
-
buffer[offset] = value;
|
|
7804
|
-
return offset + 1;
|
|
9240
|
+
toJSON() {
|
|
9241
|
+
return {
|
|
9242
|
+
code: this.code,
|
|
9243
|
+
message: this.message,
|
|
9244
|
+
details: this.details
|
|
9245
|
+
};
|
|
7805
9246
|
}
|
|
7806
|
-
|
|
7807
|
-
buffer[offset + 1] = value >> 7;
|
|
7808
|
-
return offset + 2;
|
|
7809
|
-
}
|
|
9247
|
+
};
|
|
7810
9248
|
|
|
7811
9249
|
// src/transaction/instructions/borsh-instruction.ts
|
|
7812
9250
|
function createBorshInstruction(params) {
|
|
@@ -7897,14 +9335,17 @@ var Message = class _Message {
|
|
|
7897
9335
|
accountKeys;
|
|
7898
9336
|
/** Transaction valid from (milliseconds since Unix epoch) */
|
|
7899
9337
|
validFrom;
|
|
9338
|
+
/** Config hash prefix for replay protection across chains */
|
|
9339
|
+
configHashPrefix;
|
|
7900
9340
|
/** Compiled instructions with account indices */
|
|
7901
9341
|
instructions;
|
|
7902
9342
|
/** Cached serialized bytes */
|
|
7903
9343
|
serializedCache;
|
|
7904
|
-
constructor(header, accountKeys, validFrom, instructions) {
|
|
9344
|
+
constructor(header, accountKeys, validFrom, configHashPrefix, instructions) {
|
|
7905
9345
|
this.header = Object.freeze({ ...header });
|
|
7906
9346
|
this.accountKeys = Object.freeze([...accountKeys]);
|
|
7907
9347
|
this.validFrom = validFrom;
|
|
9348
|
+
this.configHashPrefix = configHashPrefix;
|
|
7908
9349
|
this.instructions = Object.freeze(
|
|
7909
9350
|
instructions.map((ix) => Object.freeze({ ...ix }))
|
|
7910
9351
|
);
|
|
@@ -7938,7 +9379,7 @@ var Message = class _Message {
|
|
|
7938
9379
|
numReadonlySignedAccounts: data[cursor++],
|
|
7939
9380
|
numReadonlyUnsignedAccounts: data[cursor++]
|
|
7940
9381
|
};
|
|
7941
|
-
const [accountKeysLength, newCursor1] =
|
|
9382
|
+
const [accountKeysLength, newCursor1] = deserializeCompactU16(data, cursor);
|
|
7942
9383
|
cursor = newCursor1;
|
|
7943
9384
|
const accountKeys = [];
|
|
7944
9385
|
for (let i = 0; i < accountKeysLength; i++) {
|
|
@@ -7964,7 +9405,19 @@ var Message = class _Message {
|
|
|
7964
9405
|
new DataView(validFromBytes.buffer).getBigUint64(0, true)
|
|
7965
9406
|
);
|
|
7966
9407
|
cursor += 8;
|
|
7967
|
-
|
|
9408
|
+
if (data.length < cursor + 8) {
|
|
9409
|
+
throw new TransactionError(
|
|
9410
|
+
"INSUFFICIENT_DATA" /* INSUFFICIENT_DATA */,
|
|
9411
|
+
"Insufficient data for configHashPrefix"
|
|
9412
|
+
);
|
|
9413
|
+
}
|
|
9414
|
+
const configHashPrefixBytes = data.slice(cursor, cursor + 8);
|
|
9415
|
+
const configHashPrefix = BigInt.asUintN(
|
|
9416
|
+
64,
|
|
9417
|
+
new DataView(configHashPrefixBytes.buffer).getBigUint64(0, true)
|
|
9418
|
+
);
|
|
9419
|
+
cursor += 8;
|
|
9420
|
+
const [instructionsLength, newCursor2] = deserializeCompactU16(
|
|
7968
9421
|
data,
|
|
7969
9422
|
cursor
|
|
7970
9423
|
);
|
|
@@ -7978,7 +9431,7 @@ var Message = class _Message {
|
|
|
7978
9431
|
);
|
|
7979
9432
|
}
|
|
7980
9433
|
const programIdIndex = data[cursor++];
|
|
7981
|
-
const [accountIndexesLength, newCursor3] =
|
|
9434
|
+
const [accountIndexesLength, newCursor3] = deserializeCompactU16(
|
|
7982
9435
|
data,
|
|
7983
9436
|
cursor
|
|
7984
9437
|
);
|
|
@@ -7993,7 +9446,7 @@ var Message = class _Message {
|
|
|
7993
9446
|
}
|
|
7994
9447
|
accountKeyIndexes.push(data[cursor++]);
|
|
7995
9448
|
}
|
|
7996
|
-
const [dataLength, newCursor4] =
|
|
9449
|
+
const [dataLength, newCursor4] = deserializeCompactU16(data, cursor);
|
|
7997
9450
|
cursor = newCursor4;
|
|
7998
9451
|
if (data.length < cursor + dataLength) {
|
|
7999
9452
|
throw new TransactionError(
|
|
@@ -8009,7 +9462,7 @@ var Message = class _Message {
|
|
|
8009
9462
|
data: instructionData
|
|
8010
9463
|
});
|
|
8011
9464
|
}
|
|
8012
|
-
return new _Message(header, accountKeys, validFrom, instructions);
|
|
9465
|
+
return new _Message(header, accountKeys, validFrom, configHashPrefix, instructions);
|
|
8013
9466
|
}
|
|
8014
9467
|
serializeInternal() {
|
|
8015
9468
|
const buffer = [];
|
|
@@ -8026,6 +9479,13 @@ var Message = class _Message {
|
|
|
8026
9479
|
true
|
|
8027
9480
|
);
|
|
8028
9481
|
buffer.push(...new Uint8Array(validFromBuffer));
|
|
9482
|
+
const configHashPrefixBuffer = new ArrayBuffer(8);
|
|
9483
|
+
new DataView(configHashPrefixBuffer).setBigUint64(
|
|
9484
|
+
0,
|
|
9485
|
+
this.configHashPrefix,
|
|
9486
|
+
true
|
|
9487
|
+
);
|
|
9488
|
+
buffer.push(...new Uint8Array(configHashPrefixBuffer));
|
|
8029
9489
|
this.serializeCompactArray(buffer, this.instructions, (buf, ix) => {
|
|
8030
9490
|
buf.push(ix.programIdIndex);
|
|
8031
9491
|
this.serializeCompactArray(buf, ix.accountKeyIndexes, (b, idx) => {
|
|
@@ -8080,7 +9540,7 @@ var Message = class _Message {
|
|
|
8080
9540
|
return this.getSigners().findIndex((signer) => signer.equals(pubkey));
|
|
8081
9541
|
}
|
|
8082
9542
|
};
|
|
8083
|
-
function
|
|
9543
|
+
function deserializeCompactU16(data, currentCursor) {
|
|
8084
9544
|
let cursor = currentCursor;
|
|
8085
9545
|
if (cursor >= data.length) {
|
|
8086
9546
|
throw new TransactionError(
|
|
@@ -8162,7 +9622,7 @@ var Transaction = class _Transaction {
|
|
|
8162
9622
|
);
|
|
8163
9623
|
}
|
|
8164
9624
|
let cursor = 0;
|
|
8165
|
-
const [signaturesLength, newCursor] =
|
|
9625
|
+
const [signaturesLength, newCursor] = deserializeCompactU162(data, cursor);
|
|
8166
9626
|
cursor = newCursor;
|
|
8167
9627
|
const signatures = [];
|
|
8168
9628
|
for (let i = 0; i < signaturesLength; i++) {
|
|
@@ -8410,6 +9870,7 @@ var Transaction = class _Transaction {
|
|
|
8410
9870
|
var TransactionBuilder = class _TransactionBuilder {
|
|
8411
9871
|
payer;
|
|
8412
9872
|
validFrom;
|
|
9873
|
+
configHashPrefix;
|
|
8413
9874
|
instructions = [];
|
|
8414
9875
|
constructor() {
|
|
8415
9876
|
}
|
|
@@ -8443,6 +9904,18 @@ var TransactionBuilder = class _TransactionBuilder {
|
|
|
8443
9904
|
this.validFrom = validFrom;
|
|
8444
9905
|
return this;
|
|
8445
9906
|
}
|
|
9907
|
+
/**
|
|
9908
|
+
* Set the config hash prefix for replay protection.
|
|
9909
|
+
*
|
|
9910
|
+
* This is the first 64 bits of the config hash, used to ensure
|
|
9911
|
+
* transactions cannot be replayed on different chains.
|
|
9912
|
+
*
|
|
9913
|
+
* @param configHashPrefix - config hash prefix as a u64
|
|
9914
|
+
*/
|
|
9915
|
+
setConfigHashPrefix(configHashPrefix) {
|
|
9916
|
+
this.configHashPrefix = configHashPrefix;
|
|
9917
|
+
return this;
|
|
9918
|
+
}
|
|
8446
9919
|
/**
|
|
8447
9920
|
* Add an instruction to the transaction.
|
|
8448
9921
|
*
|
|
@@ -8488,6 +9961,12 @@ var TransactionBuilder = class _TransactionBuilder {
|
|
|
8488
9961
|
"Transaction must have a valid from. Use setValidFrom() to set the valid from."
|
|
8489
9962
|
);
|
|
8490
9963
|
}
|
|
9964
|
+
if (this.configHashPrefix === void 0) {
|
|
9965
|
+
throw new TransactionError(
|
|
9966
|
+
"INVALID_MESSAGE_FORMAT" /* INVALID_MESSAGE_FORMAT */,
|
|
9967
|
+
"Transaction must have a config hash prefix. Use setConfigHashPrefix() to set the config hash prefix."
|
|
9968
|
+
);
|
|
9969
|
+
}
|
|
8491
9970
|
if (this.instructions.length === 0) {
|
|
8492
9971
|
throw TransactionError.noInstructions();
|
|
8493
9972
|
}
|
|
@@ -8536,11 +10015,94 @@ var TransactionBuilder = class _TransactionBuilder {
|
|
|
8536
10015
|
header,
|
|
8537
10016
|
accountKeys,
|
|
8538
10017
|
this.validFrom,
|
|
10018
|
+
this.configHashPrefix,
|
|
8539
10019
|
compiledInstructions
|
|
8540
10020
|
);
|
|
8541
10021
|
return Transaction.fromMessage(message);
|
|
8542
10022
|
}
|
|
8543
10023
|
};
|
|
10024
|
+
|
|
10025
|
+
// src/serialization/compact-u16.ts
|
|
10026
|
+
function serializeCompactU16(buffer, value) {
|
|
10027
|
+
if (value < 0 || value > 65535) {
|
|
10028
|
+
throw new TransactionError(
|
|
10029
|
+
"SERIALIZATION_FAILED" /* SERIALIZATION_FAILED */,
|
|
10030
|
+
`Value out of range for compact-u16: ${value}`
|
|
10031
|
+
);
|
|
10032
|
+
}
|
|
10033
|
+
if (value <= 127) {
|
|
10034
|
+
buffer.push(value);
|
|
10035
|
+
return;
|
|
10036
|
+
}
|
|
10037
|
+
if (value <= 16383) {
|
|
10038
|
+
buffer.push(value & 127 | 128);
|
|
10039
|
+
buffer.push(value >> 7 & 127);
|
|
10040
|
+
return;
|
|
10041
|
+
}
|
|
10042
|
+
buffer.push(value & 127 | 128);
|
|
10043
|
+
buffer.push(value >> 7 & 127 | 128);
|
|
10044
|
+
buffer.push(value >> 14 & 255);
|
|
10045
|
+
}
|
|
10046
|
+
function deserializeCompactU162(data, currentCursor) {
|
|
10047
|
+
let cursor = currentCursor;
|
|
10048
|
+
if (cursor >= data.length) {
|
|
10049
|
+
throw new TransactionError(
|
|
10050
|
+
"INSUFFICIENT_DATA" /* INSUFFICIENT_DATA */,
|
|
10051
|
+
"Insufficient data for compact-u16"
|
|
10052
|
+
);
|
|
10053
|
+
}
|
|
10054
|
+
const firstByte = data[cursor++];
|
|
10055
|
+
if ((firstByte & 128) === 0) {
|
|
10056
|
+
return [firstByte, cursor];
|
|
10057
|
+
}
|
|
10058
|
+
if (cursor >= data.length) {
|
|
10059
|
+
throw new TransactionError(
|
|
10060
|
+
"INSUFFICIENT_DATA" /* INSUFFICIENT_DATA */,
|
|
10061
|
+
"Incomplete compact-u16 encoding"
|
|
10062
|
+
);
|
|
10063
|
+
}
|
|
10064
|
+
const secondByte = data[cursor++];
|
|
10065
|
+
if ((secondByte & 128) === 0) {
|
|
10066
|
+
return [firstByte & 127 | secondByte << 7, cursor];
|
|
10067
|
+
}
|
|
10068
|
+
if (cursor >= data.length) {
|
|
10069
|
+
throw new TransactionError(
|
|
10070
|
+
"INSUFFICIENT_DATA" /* INSUFFICIENT_DATA */,
|
|
10071
|
+
"Incomplete compact-u16 encoding"
|
|
10072
|
+
);
|
|
10073
|
+
}
|
|
10074
|
+
const thirdByte = data[cursor++];
|
|
10075
|
+
const value = firstByte & 127 | (secondByte & 127) << 7 | thirdByte << 14;
|
|
10076
|
+
return [value, cursor];
|
|
10077
|
+
}
|
|
10078
|
+
function writeCompactU16(buffer, offset, value) {
|
|
10079
|
+
if (value < 128) {
|
|
10080
|
+
buffer[offset] = value;
|
|
10081
|
+
return offset + 1;
|
|
10082
|
+
}
|
|
10083
|
+
buffer[offset] = value & 127 | 128;
|
|
10084
|
+
buffer[offset + 1] = value >> 7;
|
|
10085
|
+
return offset + 2;
|
|
10086
|
+
}
|
|
10087
|
+
|
|
10088
|
+
// src/signer/keypair-signer.ts
|
|
10089
|
+
var KeypairSigner = class {
|
|
10090
|
+
constructor(keypair) {
|
|
10091
|
+
this.keypair = keypair;
|
|
10092
|
+
}
|
|
10093
|
+
/**
|
|
10094
|
+
* Returns the keypair's public key.
|
|
10095
|
+
*/
|
|
10096
|
+
async getPublicKey() {
|
|
10097
|
+
return this.keypair.publicKey;
|
|
10098
|
+
}
|
|
10099
|
+
/**
|
|
10100
|
+
* Signs a message using the keypair's private key.
|
|
10101
|
+
*/
|
|
10102
|
+
async signMessage(message) {
|
|
10103
|
+
return this.keypair.sign(message);
|
|
10104
|
+
}
|
|
10105
|
+
};
|
|
8544
10106
|
/*! Bundled license information:
|
|
8545
10107
|
|
|
8546
10108
|
@noble/ed25519/index.js:
|
|
@@ -8560,9 +10122,17 @@ var TransactionBuilder = class _TransactionBuilder {
|
|
|
8560
10122
|
exports.AccountMetaTable = AccountMetaTable;
|
|
8561
10123
|
exports.BASE_DERIVATION_PATH = BASE_DERIVATION_PATH;
|
|
8562
10124
|
exports.BaseRpcClient = BaseRpcClient;
|
|
10125
|
+
exports.BincodeReader = BincodeReader;
|
|
10126
|
+
exports.BincodeWriter = BincodeWriter;
|
|
10127
|
+
exports.CHACHA20_POLY1305_TAG_LENGTH = CHACHA20_POLY1305_TAG_LENGTH;
|
|
8563
10128
|
exports.CryptoError = CryptoError;
|
|
8564
10129
|
exports.CryptoErrorCode = CryptoErrorCode;
|
|
8565
10130
|
exports.DEFAULT_NUM_ACCOUNTS = DEFAULT_NUM_ACCOUNTS;
|
|
10131
|
+
exports.ED25519_PUBLIC_KEY_LENGTH = ED25519_PUBLIC_KEY_LENGTH;
|
|
10132
|
+
exports.HPKE_ENC_LENGTH = HPKE_ENC_LENGTH;
|
|
10133
|
+
exports.HPKE_OVERHEAD_LENGTH = HPKE_OVERHEAD_LENGTH;
|
|
10134
|
+
exports.HpkeError = HpkeError;
|
|
10135
|
+
exports.HpkeErrorCode = HpkeErrorCode;
|
|
8566
10136
|
exports.HttpTransport = HttpTransport;
|
|
8567
10137
|
exports.KELVIN_PER_RLO = KELVIN_PER_RLO;
|
|
8568
10138
|
exports.Keypair = Keypair;
|
|
@@ -8577,14 +10147,18 @@ exports.RIALO_LOCALNET_CHAIN = RIALO_LOCALNET_CHAIN;
|
|
|
8577
10147
|
exports.RIALO_MAINNET_CHAIN = RIALO_MAINNET_CHAIN;
|
|
8578
10148
|
exports.RIALO_SHITNET_CHAIN = RIALO_SHITNET_CHAIN;
|
|
8579
10149
|
exports.RIALO_TESTNET_CHAIN = RIALO_TESTNET_CHAIN;
|
|
10150
|
+
exports.RexValue = RexValue;
|
|
10151
|
+
exports.RexValueVariant = RexValueVariant;
|
|
8580
10152
|
exports.RialoClient = RialoClient;
|
|
8581
10153
|
exports.RialoError = RialoError;
|
|
8582
10154
|
exports.RialoErrorType = RialoErrorType;
|
|
8583
10155
|
exports.RpcError = RpcError;
|
|
8584
10156
|
exports.RpcErrorCode = RpcErrorCode;
|
|
8585
10157
|
exports.SECRET_KEY_LENGTH = SECRET_KEY_LENGTH;
|
|
10158
|
+
exports.SECRET_SHARING_HPKE_INFO = SECRET_SHARING_HPKE_INFO;
|
|
8586
10159
|
exports.SIGNATURE_LENGTH = SIGNATURE_LENGTH;
|
|
8587
10160
|
exports.SYSTEM_PROGRAM_ID = SYSTEM_PROGRAM_ID;
|
|
10161
|
+
exports.Schema = Schema;
|
|
8588
10162
|
exports.Signature = Signature;
|
|
8589
10163
|
exports.SystemInstruction = SystemInstruction;
|
|
8590
10164
|
exports.Transaction = Transaction;
|
|
@@ -8597,6 +10171,8 @@ exports.URL_LOCALNET = URL_LOCALNET;
|
|
|
8597
10171
|
exports.URL_MAINNET = URL_MAINNET;
|
|
8598
10172
|
exports.URL_SHITNET = URL_SHITNET;
|
|
8599
10173
|
exports.URL_TESTNET = URL_TESTNET;
|
|
10174
|
+
exports.USER_SECRET_AAD = USER_SECRET_AAD;
|
|
10175
|
+
exports.X25519_PUBLIC_KEY_LENGTH = X25519_PUBLIC_KEY_LENGTH;
|
|
8600
10176
|
exports.allocateInstruction = allocateInstruction;
|
|
8601
10177
|
exports.assignInstruction = assignInstruction;
|
|
8602
10178
|
exports.calculateBackoff = calculateBackoff;
|
|
@@ -8604,17 +10180,33 @@ exports.concatBytes = concatBytes2;
|
|
|
8604
10180
|
exports.createAccount = createAccount;
|
|
8605
10181
|
exports.createBorshInstruction = createBorshInstruction;
|
|
8606
10182
|
exports.createRialoClient = createRialoClient;
|
|
10183
|
+
exports.deserialize = deserialize;
|
|
10184
|
+
exports.deserializeBorsh = deserializeBorsh;
|
|
10185
|
+
exports.deserializeCompactU16 = deserializeCompactU162;
|
|
10186
|
+
exports.deserializeStrict = deserializeStrict;
|
|
8607
10187
|
exports.encodeBorshData = encodeBorshData;
|
|
10188
|
+
exports.encryptForRex = encryptForRex;
|
|
10189
|
+
exports.field = field;
|
|
10190
|
+
exports.fixedArray = fixedArray;
|
|
8608
10191
|
exports.fromBase64 = fromBase64;
|
|
10192
|
+
exports.getCiphertextLength = getCiphertextLength;
|
|
8609
10193
|
exports.getDefaultRialoClientConfig = getDefaultRialoClientConfig;
|
|
8610
10194
|
exports.getDevnetUrl = getDevnetUrl;
|
|
8611
10195
|
exports.getLocalnetUrl = getLocalnetUrl;
|
|
8612
10196
|
exports.getMainnetUrl = getMainnetUrl;
|
|
8613
10197
|
exports.getTestnetUrl = getTestnetUrl;
|
|
10198
|
+
exports.hpkeEncrypt = hpkeEncrypt;
|
|
8614
10199
|
exports.isOnCurve = isOnCurve;
|
|
10200
|
+
exports.isValidCiphertextLength = isValidCiphertextLength;
|
|
10201
|
+
exports.option = option;
|
|
8615
10202
|
exports.seedToBytes = seedToBytes;
|
|
10203
|
+
exports.serialize = serialize;
|
|
10204
|
+
exports.serializeBorsh = serializeBorsh;
|
|
10205
|
+
exports.serializeCompactU16 = serializeCompactU16;
|
|
8616
10206
|
exports.sleep = sleep;
|
|
8617
10207
|
exports.toBase64 = toBase64;
|
|
8618
10208
|
exports.transferInstruction = transferInstruction;
|
|
10209
|
+
exports.vec = vec;
|
|
10210
|
+
exports.writeCompactU16 = writeCompactU16;
|
|
8619
10211
|
//# sourceMappingURL=index.js.map
|
|
8620
10212
|
//# sourceMappingURL=index.js.map
|