@audren-xyz/sdk 0.0.0-stage → 1.3.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/LICENSE +202 -0
- package/README.md +102 -2
- package/dist/constants.d.ts +229 -0
- package/dist/constants.js +301 -0
- package/dist/crypto.d.ts +85 -0
- package/dist/crypto.js +520 -0
- package/dist/idl.d.ts +4291 -0
- package/dist/idl.js +5608 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/dist/instructions.d.ts +398 -0
- package/dist/instructions.js +1139 -0
- package/dist/keys.d.ts +67 -0
- package/dist/keys.js +194 -0
- package/dist/layout.d.ts +153 -0
- package/dist/layout.js +535 -0
- package/dist/pda.d.ts +65 -0
- package/dist/pda.js +508 -0
- package/dist/poseidon-constants.d.ts +13 -0
- package/dist/poseidon-constants.js +1401 -0
- package/dist/poseidon.d.ts +46 -0
- package/dist/poseidon.js +214 -0
- package/dist/prover-entry.d.ts +1 -0
- package/dist/prover-entry.js +1 -0
- package/dist/prover.d.ts +100 -0
- package/dist/prover.js +507 -0
- package/dist/relayer.d.ts +353 -0
- package/dist/relayer.js +270 -0
- package/dist/scanner.d.ts +262 -0
- package/dist/scanner.js +3119 -0
- package/dist/store.d.ts +186 -0
- package/dist/store.js +1403 -0
- package/dist/swap.d.ts +105 -0
- package/dist/swap.js +156 -0
- package/dist/token2022.d.ts +29 -0
- package/dist/token2022.js +168 -0
- package/dist/tx-v1.d.ts +63 -0
- package/dist/tx-v1.js +249 -0
- package/idl/audren.anchor.json +1750 -0
- package/idl/audren.codama.json +3856 -0
- package/package.json +76 -4
- package/wasm/audren_circuits.wasm +0 -0
- package/wasm/audren_joinsplit.pk +0 -0
package/dist/tx-v1.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency Solana Transaction Version 1 (SIMD-0385 / `0x81`) compiler and wire serializer.
|
|
3
|
+
*
|
|
4
|
+
* Wire layout (`<= 4,096 B` total):
|
|
5
|
+
* wireBytes = messageBytes || signatures (fixed-len `numRequiredSignatures * 64 B`, no short_vec)
|
|
6
|
+
* messageBytes = 0x81
|
|
7
|
+
* | header(3 B: numRequiredSignatures, numReadonlySigned, numReadonlyUnsigned)
|
|
8
|
+
* | configMask(u32 LE)
|
|
9
|
+
* | lifetimeSpecifier(recentBlockhash: 32 B)
|
|
10
|
+
* | numInstructions(u8) | numAddresses(u8)
|
|
11
|
+
* | accountKeys(numAddresses * 32 B)
|
|
12
|
+
* | configValues (bit order: priorityFee u64 LE, computeUnitLimit u32 LE,
|
|
13
|
+
* loadedAccountsDataSizeLimit u32 LE, heapSize u32 LE)
|
|
14
|
+
* | instructionHeaders(numInstructions * 4 B: programIdx u8, numAccounts u8, dataLen u16 LE)
|
|
15
|
+
* | instructionPayloads(for each ix: accountIndices || data)
|
|
16
|
+
*
|
|
17
|
+
* Compute budget limits and priority fees are carried natively inside the v1 header
|
|
18
|
+
* without separate `ComputeBudgetInstruction`s.
|
|
19
|
+
*/
|
|
20
|
+
export const SOLANA_TX_V1_PREFIX = 0x81;
|
|
21
|
+
export const SOLANA_TX_V1_MAX_SIZE = 4_096;
|
|
22
|
+
export const SOLANA_TX_V1_MAX_SIGNATURES = 12;
|
|
23
|
+
export const SOLANA_TX_V1_DEFAULT_LOADED_ACCOUNTS_DATA_SIZE = 64 * 1024 * 1024;
|
|
24
|
+
const CONFIG_MASK_PRIORITY_FEE = 0b11;
|
|
25
|
+
const CONFIG_MASK_COMPUTE_UNIT_LIMIT = 0b100;
|
|
26
|
+
const CONFIG_MASK_LOADED_ACCOUNTS_DATA_SIZE = 0b1000;
|
|
27
|
+
const CONFIG_MASK_HEAP_SIZE = 0b10000;
|
|
28
|
+
const U64_MAX = 0xffffffffffffffffn;
|
|
29
|
+
function bytesToHex(bytes) {
|
|
30
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
31
|
+
throw new RangeError(`Expected Uint8Array, got ${String(bytes)}`);
|
|
32
|
+
}
|
|
33
|
+
let out = '';
|
|
34
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
35
|
+
out += bytes[i].toString(16).padStart(2, '0');
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
function concatBytes(...chunks) {
|
|
40
|
+
let total = 0;
|
|
41
|
+
for (const c of chunks) {
|
|
42
|
+
if (!(c instanceof Uint8Array)) {
|
|
43
|
+
throw new RangeError(`Expected Uint8Array chunk, got ${String(c)}`);
|
|
44
|
+
}
|
|
45
|
+
total += c.byteLength;
|
|
46
|
+
}
|
|
47
|
+
const out = new Uint8Array(total);
|
|
48
|
+
let off = 0;
|
|
49
|
+
for (const c of chunks) {
|
|
50
|
+
out.set(c, off);
|
|
51
|
+
off += c.byteLength;
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
function u16le(n) {
|
|
56
|
+
const out = new Uint8Array(2);
|
|
57
|
+
new DataView(out.buffer).setUint16(0, n, true);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
function u32le(n) {
|
|
61
|
+
const out = new Uint8Array(4);
|
|
62
|
+
new DataView(out.buffer).setUint32(0, n, true);
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
function u64le(n) {
|
|
66
|
+
const out = new Uint8Array(8);
|
|
67
|
+
new DataView(out.buffer).setBigUint64(0, n, true);
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Compile instructions and inline `TransactionConfig` into the exact Solana Transaction v1
|
|
72
|
+
* message byte buffer (`0x81...`) that signers sign with Ed25519.
|
|
73
|
+
*/
|
|
74
|
+
export function compileV1Message(params) {
|
|
75
|
+
if (!params || typeof params !== 'object') {
|
|
76
|
+
throw new RangeError('compileV1Message params must be an object');
|
|
77
|
+
}
|
|
78
|
+
const { feePayer, recentBlockhash, instructions, config } = params;
|
|
79
|
+
if (!(feePayer instanceof Uint8Array) || feePayer.byteLength !== 32) {
|
|
80
|
+
throw new RangeError(`feePayer must be a 32-byte Uint8Array, got ${feePayer instanceof Uint8Array ? feePayer.byteLength : String(feePayer)}`);
|
|
81
|
+
}
|
|
82
|
+
if (!(recentBlockhash instanceof Uint8Array) || recentBlockhash.byteLength !== 32) {
|
|
83
|
+
throw new RangeError(`recentBlockhash must be a 32-byte Uint8Array, got ${recentBlockhash instanceof Uint8Array ? recentBlockhash.byteLength : String(recentBlockhash)}`);
|
|
84
|
+
}
|
|
85
|
+
if (!Array.isArray(instructions) || instructions.length === 0 || instructions.length > 64) {
|
|
86
|
+
throw new RangeError(`V1 instruction count must be 1..=64, got ${Array.isArray(instructions) ? instructions.length : String(instructions)}`);
|
|
87
|
+
}
|
|
88
|
+
if (!config || typeof config !== 'object') {
|
|
89
|
+
throw new RangeError('config must be an object');
|
|
90
|
+
}
|
|
91
|
+
const map = new Map();
|
|
92
|
+
const addMeta = (m) => {
|
|
93
|
+
if (!m || typeof m !== 'object' || !(m.pubkey instanceof Uint8Array) || m.pubkey.byteLength !== 32) {
|
|
94
|
+
throw new RangeError(`account pubkey must be a 32-byte Uint8Array, got ${m && m.pubkey instanceof Uint8Array ? m.pubkey.byteLength : String(m?.pubkey)}`);
|
|
95
|
+
}
|
|
96
|
+
const k = bytesToHex(m.pubkey);
|
|
97
|
+
const prev = map.get(k);
|
|
98
|
+
if (!prev) {
|
|
99
|
+
map.set(k, { pubkey: m.pubkey, isSigner: m.isSigner, isWritable: m.isWritable });
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
prev.isSigner = prev.isSigner || m.isSigner;
|
|
103
|
+
prev.isWritable = prev.isWritable || m.isWritable;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
addMeta({ pubkey: feePayer, isSigner: true, isWritable: true });
|
|
107
|
+
for (const ix of instructions) {
|
|
108
|
+
if (!ix || typeof ix !== 'object' || !Array.isArray(ix.accounts)) {
|
|
109
|
+
throw new RangeError('Each V1Instruction must be an object with an accounts array');
|
|
110
|
+
}
|
|
111
|
+
if (!(ix.programId instanceof Uint8Array) || ix.programId.byteLength !== 32) {
|
|
112
|
+
throw new RangeError(`instruction programId must be a 32-byte Uint8Array, got ${ix.programId instanceof Uint8Array ? ix.programId.byteLength : String(ix.programId)}`);
|
|
113
|
+
}
|
|
114
|
+
if (!(ix.data instanceof Uint8Array)) {
|
|
115
|
+
throw new RangeError(`instruction data must be a Uint8Array, got ${String(ix.data)}`);
|
|
116
|
+
}
|
|
117
|
+
for (const acc of ix.accounts)
|
|
118
|
+
addMeta(acc);
|
|
119
|
+
addMeta({ pubkey: ix.programId, isSigner: false, isWritable: false });
|
|
120
|
+
}
|
|
121
|
+
const feeHex = bytesToHex(feePayer);
|
|
122
|
+
const compareBytes32 = (a, b) => {
|
|
123
|
+
for (let i = 0; i < 32; i++) {
|
|
124
|
+
const diff = a[i] - b[i];
|
|
125
|
+
if (diff !== 0)
|
|
126
|
+
return diff;
|
|
127
|
+
}
|
|
128
|
+
return 0;
|
|
129
|
+
};
|
|
130
|
+
const metas = Array.from(map.values()).sort((a, b) => {
|
|
131
|
+
const aIsFee = bytesToHex(a.pubkey) === feeHex ? 0 : 1;
|
|
132
|
+
const bIsFee = bytesToHex(b.pubkey) === feeHex ? 0 : 1;
|
|
133
|
+
if (aIsFee !== bIsFee)
|
|
134
|
+
return aIsFee - bIsFee;
|
|
135
|
+
const rank = (m) => (m.isSigner ? 0 : 2) + (m.isWritable ? 0 : 1);
|
|
136
|
+
const diff = rank(a) - rank(b);
|
|
137
|
+
if (diff !== 0)
|
|
138
|
+
return diff;
|
|
139
|
+
return compareBytes32(a.pubkey, b.pubkey);
|
|
140
|
+
});
|
|
141
|
+
if (metas.length > 64) {
|
|
142
|
+
throw new Error(`V1 address count ${metas.length} exceeds 64`);
|
|
143
|
+
}
|
|
144
|
+
const numRequiredSignatures = metas.filter((m) => m.isSigner).length;
|
|
145
|
+
if (numRequiredSignatures > SOLANA_TX_V1_MAX_SIGNATURES) {
|
|
146
|
+
throw new RangeError(`V1 required signature count ${numRequiredSignatures} exceeds ${SOLANA_TX_V1_MAX_SIGNATURES}`);
|
|
147
|
+
}
|
|
148
|
+
const numReadonlySigned = metas.filter((m) => m.isSigner && !m.isWritable).length;
|
|
149
|
+
const numReadonlyUnsigned = metas.filter((m) => !m.isSigner && !m.isWritable).length;
|
|
150
|
+
const indexByHex = new Map();
|
|
151
|
+
metas.forEach((m, idx) => indexByHex.set(bytesToHex(m.pubkey), idx));
|
|
152
|
+
let configMask = 0;
|
|
153
|
+
const configValues = [];
|
|
154
|
+
if (config.priorityFeeLamports !== undefined) {
|
|
155
|
+
if (typeof config.priorityFeeLamports !== 'bigint' ||
|
|
156
|
+
config.priorityFeeLamports < 0n ||
|
|
157
|
+
config.priorityFeeLamports > U64_MAX) {
|
|
158
|
+
throw new RangeError(`Invalid priorityFeeLamports (must be u64 bigint): ${String(config.priorityFeeLamports)}`);
|
|
159
|
+
}
|
|
160
|
+
configMask |= CONFIG_MASK_PRIORITY_FEE;
|
|
161
|
+
configValues.push(u64le(config.priorityFeeLamports));
|
|
162
|
+
}
|
|
163
|
+
if (!Number.isInteger(config.computeUnitLimit) || config.computeUnitLimit < 0 || config.computeUnitLimit > 0xffff_ffff) {
|
|
164
|
+
throw new RangeError(`Invalid computeUnitLimit (must be u32): ${config.computeUnitLimit}`);
|
|
165
|
+
}
|
|
166
|
+
configMask |= CONFIG_MASK_COMPUTE_UNIT_LIMIT;
|
|
167
|
+
configValues.push(u32le(config.computeUnitLimit));
|
|
168
|
+
const loadedLimit = config.loadedAccountsDataSizeLimit ?? SOLANA_TX_V1_DEFAULT_LOADED_ACCOUNTS_DATA_SIZE;
|
|
169
|
+
if (!Number.isInteger(loadedLimit) || loadedLimit < 0 || loadedLimit > 0xffff_ffff) {
|
|
170
|
+
throw new RangeError(`Invalid loadedAccountsDataSizeLimit (must be u32): ${loadedLimit}`);
|
|
171
|
+
}
|
|
172
|
+
configMask |= CONFIG_MASK_LOADED_ACCOUNTS_DATA_SIZE;
|
|
173
|
+
configValues.push(u32le(loadedLimit));
|
|
174
|
+
if (config.heapSize !== undefined) {
|
|
175
|
+
if (!Number.isInteger(config.heapSize) ||
|
|
176
|
+
config.heapSize % 1024 !== 0 ||
|
|
177
|
+
config.heapSize < 32 * 1024 ||
|
|
178
|
+
config.heapSize > 256 * 1024) {
|
|
179
|
+
throw new RangeError(`Invalid V1 heapSize: ${String(config.heapSize)}`);
|
|
180
|
+
}
|
|
181
|
+
configMask |= CONFIG_MASK_HEAP_SIZE;
|
|
182
|
+
configValues.push(u32le(config.heapSize));
|
|
183
|
+
}
|
|
184
|
+
const ixHeaders = [];
|
|
185
|
+
const ixPayloads = [];
|
|
186
|
+
for (const ix of instructions) {
|
|
187
|
+
const programIdIndex = indexByHex.get(bytesToHex(ix.programId));
|
|
188
|
+
if (programIdIndex === 0) {
|
|
189
|
+
throw new Error('Program ID cannot be fee payer in a V1 transaction');
|
|
190
|
+
}
|
|
191
|
+
if (ix.accounts.length > 255) {
|
|
192
|
+
throw new Error(`Instruction account count ${ix.accounts.length} exceeds 255`);
|
|
193
|
+
}
|
|
194
|
+
if (ix.data.byteLength > 65_535) {
|
|
195
|
+
throw new RangeError(`Instruction data length ${ix.data.byteLength} exceeds 65,535`);
|
|
196
|
+
}
|
|
197
|
+
const accIndices = new Uint8Array(ix.accounts.map((a) => indexByHex.get(bytesToHex(a.pubkey))));
|
|
198
|
+
ixHeaders.push(concatBytes(new Uint8Array([programIdIndex, accIndices.length]), u16le(ix.data.byteLength)));
|
|
199
|
+
ixPayloads.push(accIndices, ix.data);
|
|
200
|
+
}
|
|
201
|
+
const messageBytes = concatBytes(new Uint8Array([SOLANA_TX_V1_PREFIX]), new Uint8Array([numRequiredSignatures, numReadonlySigned, numReadonlyUnsigned]), u32le(configMask), recentBlockhash, new Uint8Array([instructions.length, metas.length]), ...metas.map((m) => m.pubkey), ...configValues, ...ixHeaders, ...ixPayloads);
|
|
202
|
+
const wireLen = messageBytes.byteLength + numRequiredSignatures * 64;
|
|
203
|
+
if (wireLen > SOLANA_TX_V1_MAX_SIZE) {
|
|
204
|
+
throw new Error(`Compiled V1 transaction size ${wireLen} B exceeds ${SOLANA_TX_V1_MAX_SIZE} B limit`);
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
messageBytes,
|
|
208
|
+
numRequiredSignatures,
|
|
209
|
+
signerPubkeys: metas.slice(0, numRequiredSignatures).map((m) => m.pubkey),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Assemble a finalized Solana Transaction v1 wire packet (`messageBytes || signatures`).
|
|
214
|
+
* Each signature must be a 64-byte Ed25519 signature over `compiled.messageBytes`.
|
|
215
|
+
*/
|
|
216
|
+
export function serializeV1Transaction(compiled, signatures) {
|
|
217
|
+
if (!compiled || typeof compiled !== 'object') {
|
|
218
|
+
throw new RangeError('compiled must be a CompiledV1Message object');
|
|
219
|
+
}
|
|
220
|
+
if (!(compiled.messageBytes instanceof Uint8Array) ||
|
|
221
|
+
compiled.messageBytes.byteLength < 42 ||
|
|
222
|
+
compiled.messageBytes[0] !== SOLANA_TX_V1_PREFIX) {
|
|
223
|
+
throw new RangeError(`compiled.messageBytes must be a V1 message (>= 42 B) starting with 0x${SOLANA_TX_V1_PREFIX.toString(16)}`);
|
|
224
|
+
}
|
|
225
|
+
if (!Number.isInteger(compiled.numRequiredSignatures) ||
|
|
226
|
+
compiled.numRequiredSignatures < 1 ||
|
|
227
|
+
compiled.numRequiredSignatures > SOLANA_TX_V1_MAX_SIGNATURES) {
|
|
228
|
+
throw new RangeError(`compiled.numRequiredSignatures must be an integer in 1..=${SOLANA_TX_V1_MAX_SIGNATURES}, got ${String(compiled.numRequiredSignatures)}`);
|
|
229
|
+
}
|
|
230
|
+
if (compiled.messageBytes[1] !== compiled.numRequiredSignatures) {
|
|
231
|
+
throw new RangeError(`compiled.messageBytes header signature count (${compiled.messageBytes[1]}) does not match numRequiredSignatures (${compiled.numRequiredSignatures})`);
|
|
232
|
+
}
|
|
233
|
+
if (!Array.isArray(signatures)) {
|
|
234
|
+
throw new RangeError('signatures must be an array of 64-byte Uint8Arrays');
|
|
235
|
+
}
|
|
236
|
+
if (signatures.length !== compiled.numRequiredSignatures) {
|
|
237
|
+
throw new RangeError(`Expected ${compiled.numRequiredSignatures} signatures, got ${signatures.length}`);
|
|
238
|
+
}
|
|
239
|
+
for (const sig of signatures) {
|
|
240
|
+
if (!(sig instanceof Uint8Array) || sig.byteLength !== 64) {
|
|
241
|
+
throw new RangeError(`Signature must be a 64-byte Uint8Array, got ${sig instanceof Uint8Array ? sig.byteLength : String(sig)}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const wireBytes = concatBytes(compiled.messageBytes, ...signatures);
|
|
245
|
+
if (wireBytes.byteLength > SOLANA_TX_V1_MAX_SIZE) {
|
|
246
|
+
throw new Error(`V1 transaction size ${wireBytes.byteLength} B exceeds ${SOLANA_TX_V1_MAX_SIZE} B limit`);
|
|
247
|
+
}
|
|
248
|
+
return wireBytes;
|
|
249
|
+
}
|