@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/pda.js
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana Base58 codec, BIP-350 Bech32m `aud1...` stealth address codec (HRP `"aud"`,
|
|
3
|
+
* version `0x01`, 65-byte payload, 114 chars), and canonical 12 Audren v1 PDA derivations
|
|
4
|
+
* (`audren_*`) matching `crates/client/src/pda.rs` byte-for-byte.
|
|
5
|
+
*/
|
|
6
|
+
import { x25519 } from '@noble/curves/ed25519.js';
|
|
7
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
8
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, AUDREN_PROGRAM_ID, CLASSIC_TOKEN_PROGRAM_ID, ESCROW_SEED, LANE_RENT_SEED, LANE_SEED, MEMO_SEED, NF_PAGE_SEED, NULLIFIER_SEED, NUM_ESCROW_SHARDS, NUM_LANES, POOL_SEED, ROOTS_SEED, SUBROOT_SEED, SUBTREE_SEED, UNWRAP_SEED, VAULT_AUTH_SEED, } from './constants.js';
|
|
9
|
+
import { isCanonicalFr } from './poseidon.js';
|
|
10
|
+
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
11
|
+
const BASE58_MAP = new Int16Array(128).fill(-1);
|
|
12
|
+
for (let i = 0; i < BASE58_ALPHABET.length; i++) {
|
|
13
|
+
BASE58_MAP[BASE58_ALPHABET.charCodeAt(i)] = i;
|
|
14
|
+
}
|
|
15
|
+
const BECH32_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
|
|
16
|
+
const BECH32_CHARSET_MAP = new Int8Array(128).fill(-1);
|
|
17
|
+
for (let i = 0; i < BECH32_CHARSET.length; i++) {
|
|
18
|
+
BECH32_CHARSET_MAP[BECH32_CHARSET.charCodeAt(i)] = i;
|
|
19
|
+
}
|
|
20
|
+
const BECH32M_CONST = 0x2bc830a3;
|
|
21
|
+
const BECH32_GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
|
|
22
|
+
const BECH32_MAX_LEN = 128;
|
|
23
|
+
const AUDREN_ADDR_HRP = 'aud';
|
|
24
|
+
const ADDR_VERSION_V1 = 0x01;
|
|
25
|
+
const X25519_PROBE_SCALAR = new Uint8Array(32).fill(0x09);
|
|
26
|
+
function isAllZero32(bytes) {
|
|
27
|
+
if (!(bytes instanceof Uint8Array) || bytes.byteLength !== 32)
|
|
28
|
+
return true;
|
|
29
|
+
let acc = 0;
|
|
30
|
+
for (let i = 0; i < 32; i++)
|
|
31
|
+
acc |= bytes[i];
|
|
32
|
+
return acc === 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Verify an X25519 public key is contributory (rejects all-zero and Curve25519 order-8 low-order
|
|
36
|
+
* points using the fixed RFC 7748 probe scalar `[0x09; 32]`).
|
|
37
|
+
*/
|
|
38
|
+
export function isContributoryX25519Point(viewPk) {
|
|
39
|
+
if (!(viewPk instanceof Uint8Array) || viewPk.byteLength !== 32)
|
|
40
|
+
return false;
|
|
41
|
+
let shared;
|
|
42
|
+
try {
|
|
43
|
+
shared = x25519.getSharedSecret(X25519_PROBE_SCALAR, viewPk);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return !isAllZero32(shared);
|
|
49
|
+
}
|
|
50
|
+
const utf8Encoder = new TextEncoder();
|
|
51
|
+
const PDA_MARKER = utf8Encoder.encode('ProgramDerivedAddress');
|
|
52
|
+
const ED25519_P = (1n << 255n) - 19n;
|
|
53
|
+
const ED25519_D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;
|
|
54
|
+
const ED25519_SQRT_M1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n;
|
|
55
|
+
const ED25519_EXP = (ED25519_P - 5n) >> 3n;
|
|
56
|
+
export function base58Encode(bytes) {
|
|
57
|
+
if (!(bytes instanceof Uint8Array)) {
|
|
58
|
+
throw new RangeError(`base58Encode input must be a Uint8Array, got ${String(bytes)}`);
|
|
59
|
+
}
|
|
60
|
+
if (bytes.byteLength === 0)
|
|
61
|
+
return '';
|
|
62
|
+
let zeros = 0;
|
|
63
|
+
while (zeros < bytes.byteLength && bytes[zeros] === 0)
|
|
64
|
+
zeros++;
|
|
65
|
+
if (zeros === bytes.byteLength)
|
|
66
|
+
return '1'.repeat(zeros);
|
|
67
|
+
const digits = [0];
|
|
68
|
+
for (let i = zeros; i < bytes.byteLength; i++) {
|
|
69
|
+
let carry = bytes[i];
|
|
70
|
+
for (let j = 0; j < digits.length; j++) {
|
|
71
|
+
carry += digits[j] << 8;
|
|
72
|
+
digits[j] = carry % 58;
|
|
73
|
+
carry = (carry / 58) | 0;
|
|
74
|
+
}
|
|
75
|
+
while (carry > 0) {
|
|
76
|
+
digits.push(carry % 58);
|
|
77
|
+
carry = (carry / 58) | 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
let out = '1'.repeat(zeros);
|
|
81
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
82
|
+
out += BASE58_ALPHABET[digits[i]];
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
const BASE58_DEFAULT_MAX_LEN = 256;
|
|
87
|
+
export function base58Decode(input, maxLen = BASE58_DEFAULT_MAX_LEN) {
|
|
88
|
+
if (typeof input !== 'string') {
|
|
89
|
+
throw new Error(`Base58 input must be a string, got ${typeof input}`);
|
|
90
|
+
}
|
|
91
|
+
if (!Number.isInteger(maxLen) || maxLen < 0) {
|
|
92
|
+
throw new RangeError(`Base58 maxLen must be a non-negative integer, got ${String(maxLen)}`);
|
|
93
|
+
}
|
|
94
|
+
if (input.length === 0)
|
|
95
|
+
return new Uint8Array(0);
|
|
96
|
+
if (input.length > maxLen) {
|
|
97
|
+
throw new RangeError(`Base58 input length ${input.length} exceeds limit ${maxLen}`);
|
|
98
|
+
}
|
|
99
|
+
let zeros = 0;
|
|
100
|
+
while (zeros < input.length && input[zeros] === '1')
|
|
101
|
+
zeros++;
|
|
102
|
+
const bytes = [0];
|
|
103
|
+
for (let i = zeros; i < input.length; i++) {
|
|
104
|
+
const code = input.charCodeAt(i);
|
|
105
|
+
const val = code < 128 ? BASE58_MAP[code] : -1;
|
|
106
|
+
if (val < 0)
|
|
107
|
+
throw new Error(`Invalid base58 character at index ${i}`);
|
|
108
|
+
let carry = val;
|
|
109
|
+
for (let j = 0; j < bytes.length; j++) {
|
|
110
|
+
carry += bytes[j] * 58;
|
|
111
|
+
bytes[j] = carry & 0xff;
|
|
112
|
+
carry >>= 8;
|
|
113
|
+
}
|
|
114
|
+
while (carry > 0) {
|
|
115
|
+
bytes.push(carry & 0xff);
|
|
116
|
+
carry >>= 8;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const trimmedLen = bytes.length === 1 && bytes[0] === 0 ? 0 : bytes.length;
|
|
120
|
+
const out = new Uint8Array(zeros + trimmedLen);
|
|
121
|
+
for (let i = 0; i < trimmedLen; i++) {
|
|
122
|
+
out[zeros + i] = bytes[trimmedLen - 1 - i];
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
export function toPubkeyBytes(pubkey) {
|
|
127
|
+
if (typeof pubkey === 'string') {
|
|
128
|
+
if (pubkey.length > 44) {
|
|
129
|
+
throw new RangeError(`Solana public key base58 length ${pubkey.length} exceeds 44`);
|
|
130
|
+
}
|
|
131
|
+
const bytes = base58Decode(pubkey, 44);
|
|
132
|
+
if (bytes.byteLength !== 32) {
|
|
133
|
+
throw new RangeError(`Solana public key must be 32 bytes, got ${bytes.byteLength}`);
|
|
134
|
+
}
|
|
135
|
+
return bytes;
|
|
136
|
+
}
|
|
137
|
+
if (!(pubkey instanceof Uint8Array) || pubkey.byteLength !== 32) {
|
|
138
|
+
throw new RangeError(`Solana public key must be a base58 string or 32-byte Uint8Array, got ${pubkey instanceof Uint8Array ? pubkey.byteLength : String(pubkey)}`);
|
|
139
|
+
}
|
|
140
|
+
return pubkey;
|
|
141
|
+
}
|
|
142
|
+
// =============================================================================
|
|
143
|
+
// Canonical BIP-350 Bech32m Codec (`hrp = "aud"`, up to 128 chars)
|
|
144
|
+
// =============================================================================
|
|
145
|
+
function bech32Polymod(values) {
|
|
146
|
+
let chk = 1;
|
|
147
|
+
for (let i = 0; i < values.length; i++) {
|
|
148
|
+
const top = chk >>> 25;
|
|
149
|
+
chk = ((chk & 0x1ffffff) << 5) ^ values[i];
|
|
150
|
+
for (let b = 0; b < 5; b++) {
|
|
151
|
+
if ((top >>> b) & 1)
|
|
152
|
+
chk ^= BECH32_GEN[b];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return chk >>> 0;
|
|
156
|
+
}
|
|
157
|
+
function bech32HrpExpand(hrp) {
|
|
158
|
+
const ret = [];
|
|
159
|
+
for (let i = 0; i < hrp.length; i++) {
|
|
160
|
+
ret.push(hrp.charCodeAt(i) >>> 5);
|
|
161
|
+
}
|
|
162
|
+
ret.push(0);
|
|
163
|
+
for (let i = 0; i < hrp.length; i++) {
|
|
164
|
+
ret.push(hrp.charCodeAt(i) & 31);
|
|
165
|
+
}
|
|
166
|
+
return ret;
|
|
167
|
+
}
|
|
168
|
+
function bech32mCreateChecksum(hrp, data5) {
|
|
169
|
+
const values = [...bech32HrpExpand(hrp), ...data5, 0, 0, 0, 0, 0, 0];
|
|
170
|
+
const pmod = (bech32Polymod(values) ^ BECH32M_CONST) >>> 0;
|
|
171
|
+
const out = [];
|
|
172
|
+
for (let i = 0; i < 6; i++) {
|
|
173
|
+
out.push((pmod >>> (5 * (5 - i))) & 31);
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
function convertBits(data, fromBits, toBits, pad) {
|
|
178
|
+
let acc = 0;
|
|
179
|
+
let bits = 0;
|
|
180
|
+
const out = [];
|
|
181
|
+
const maxv = (1 << toBits) - 1;
|
|
182
|
+
const maxAcc = (1 << (fromBits + toBits - 1)) - 1;
|
|
183
|
+
for (let i = 0; i < data.length; i++) {
|
|
184
|
+
const value = data[i];
|
|
185
|
+
if (value < 0 || value >>> fromBits !== 0) {
|
|
186
|
+
throw new Error(`Invalid Bech32m bit group value: ${value}`);
|
|
187
|
+
}
|
|
188
|
+
acc = ((acc << fromBits) | value) & maxAcc;
|
|
189
|
+
bits += fromBits;
|
|
190
|
+
while (bits >= toBits) {
|
|
191
|
+
bits -= toBits;
|
|
192
|
+
out.push((acc >>> bits) & maxv);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (pad) {
|
|
196
|
+
if (bits > 0) {
|
|
197
|
+
out.push((acc << (toBits - bits)) & maxv);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else if (bits >= fromBits || ((acc << (toBits - bits)) & maxv) !== 0) {
|
|
201
|
+
throw new Error('Invalid Bech32m padding bits');
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Encode `(spendPk[32], viewPk[32])` into a canonical BIP-350 Bech32m `aud1...` stealth address
|
|
207
|
+
* (`hrp = "aud"`, payload `0x01 || spend_pk[32] || view_pk[32]`, 114 lowercase characters).
|
|
208
|
+
*/
|
|
209
|
+
export function formatAudrenAddress(spendPk, viewPk) {
|
|
210
|
+
if (!(spendPk instanceof Uint8Array) ||
|
|
211
|
+
spendPk.byteLength !== 32 ||
|
|
212
|
+
!(viewPk instanceof Uint8Array) ||
|
|
213
|
+
viewPk.byteLength !== 32) {
|
|
214
|
+
throw new RangeError('spendPk and viewPk must each be a 32-byte Uint8Array');
|
|
215
|
+
}
|
|
216
|
+
if (!isCanonicalFr(spendPk)) {
|
|
217
|
+
throw new Error('spendPk must be a canonical BN254 Fr scalar (< r)');
|
|
218
|
+
}
|
|
219
|
+
if (!isContributoryX25519Point(viewPk)) {
|
|
220
|
+
throw new Error('viewPk must be a contributory X25519 public key (non-low-order)');
|
|
221
|
+
}
|
|
222
|
+
const raw = new Uint8Array(65);
|
|
223
|
+
raw[0] = ADDR_VERSION_V1;
|
|
224
|
+
raw.set(spendPk, 1);
|
|
225
|
+
raw.set(viewPk, 33);
|
|
226
|
+
const data5 = convertBits(raw, 8, 5, true);
|
|
227
|
+
const checksum = bech32mCreateChecksum(AUDREN_ADDR_HRP, data5);
|
|
228
|
+
let out = `${AUDREN_ADDR_HRP}1`;
|
|
229
|
+
for (const v of data5)
|
|
230
|
+
out += BECH32_CHARSET[v];
|
|
231
|
+
for (const v of checksum)
|
|
232
|
+
out += BECH32_CHARSET[v];
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Strictly decode and validate a BIP-350 Bech32m `aud1...` stealth address.
|
|
237
|
+
* Rejects leading/trailing whitespace, mixed case, wrong HRP, invalid Bech32m checksum,
|
|
238
|
+
* non-zero pad bits, wrong payload length, wrong version byte (`!== 0x01`),
|
|
239
|
+
* non-canonical `spendPk >= r`, and non-contributory / low-order X25519 `viewPk`.
|
|
240
|
+
*/
|
|
241
|
+
export function parseAudrenAddress(encoded) {
|
|
242
|
+
if (typeof encoded !== 'string' || encoded.length < 8 || encoded.length > BECH32_MAX_LEN) {
|
|
243
|
+
throw new Error(`Invalid Audren Bech32m address length (${typeof encoded === 'string' ? encoded.length : String(encoded)})`);
|
|
244
|
+
}
|
|
245
|
+
let hasLower = false;
|
|
246
|
+
let hasUpper = false;
|
|
247
|
+
for (let i = 0; i < encoded.length; i++) {
|
|
248
|
+
const code = encoded.charCodeAt(i);
|
|
249
|
+
if (code < 33 || code > 126) {
|
|
250
|
+
throw new Error(`Invalid character in Audren Bech32m address at index ${i}`);
|
|
251
|
+
}
|
|
252
|
+
if (code >= 97 && code <= 122)
|
|
253
|
+
hasLower = true;
|
|
254
|
+
if (code >= 65 && code <= 90)
|
|
255
|
+
hasUpper = true;
|
|
256
|
+
}
|
|
257
|
+
if (hasLower && hasUpper) {
|
|
258
|
+
throw new Error('Mixed case is forbidden in Bech32m Audren address');
|
|
259
|
+
}
|
|
260
|
+
const lower = encoded.toLowerCase();
|
|
261
|
+
const sep = lower.lastIndexOf('1');
|
|
262
|
+
if (sep < 1 || sep + 7 > lower.length) {
|
|
263
|
+
throw new Error('Invalid Bech32m separator position in Audren address');
|
|
264
|
+
}
|
|
265
|
+
const hrp = lower.slice(0, sep);
|
|
266
|
+
if (hrp !== AUDREN_ADDR_HRP) {
|
|
267
|
+
throw new Error(`Invalid Audren address HRP: expected "${AUDREN_ADDR_HRP}", got "${hrp}"`);
|
|
268
|
+
}
|
|
269
|
+
const dataPart = lower.slice(sep + 1);
|
|
270
|
+
const dataValues = [];
|
|
271
|
+
for (let i = 0; i < dataPart.length; i++) {
|
|
272
|
+
const code = dataPart.charCodeAt(i);
|
|
273
|
+
const val = code < 128 ? BECH32_CHARSET_MAP[code] : -1;
|
|
274
|
+
if (val < 0) {
|
|
275
|
+
throw new Error(`Invalid Bech32m character '${dataPart[i]}' in Audren address`);
|
|
276
|
+
}
|
|
277
|
+
dataValues.push(val);
|
|
278
|
+
}
|
|
279
|
+
if (bech32Polymod([...bech32HrpExpand(hrp), ...dataValues]) !== BECH32M_CONST) {
|
|
280
|
+
throw new Error('Invalid Bech32m checksum in Audren address');
|
|
281
|
+
}
|
|
282
|
+
const payload5 = dataValues.slice(0, dataValues.length - 6);
|
|
283
|
+
const raw = new Uint8Array(convertBits(payload5, 5, 8, false));
|
|
284
|
+
if (raw.byteLength !== 65 || raw[0] !== ADDR_VERSION_V1) {
|
|
285
|
+
throw new Error(`Invalid Audren address payload length or version (len=${raw.byteLength}, v=${raw[0]})`);
|
|
286
|
+
}
|
|
287
|
+
const spendPk = raw.slice(1, 33);
|
|
288
|
+
const viewPk = raw.slice(33, 65);
|
|
289
|
+
if (!isCanonicalFr(spendPk)) {
|
|
290
|
+
throw new Error('Audren address spendPk is not a canonical BN254 Fr scalar');
|
|
291
|
+
}
|
|
292
|
+
if (!isContributoryX25519Point(viewPk)) {
|
|
293
|
+
throw new Error('Audren address viewPk is a low-order X25519 public key');
|
|
294
|
+
}
|
|
295
|
+
return { encoded: lower, spendPk, viewPk };
|
|
296
|
+
}
|
|
297
|
+
function modP(n) {
|
|
298
|
+
const r = n % ED25519_P;
|
|
299
|
+
return r >= 0n ? r : r + ED25519_P;
|
|
300
|
+
}
|
|
301
|
+
function modPowP(base, exp) {
|
|
302
|
+
let result = 1n;
|
|
303
|
+
let b = modP(base);
|
|
304
|
+
let e = exp;
|
|
305
|
+
while (e > 0n) {
|
|
306
|
+
if ((e & 1n) === 1n)
|
|
307
|
+
result = (result * b) % ED25519_P;
|
|
308
|
+
b = (b * b) % ED25519_P;
|
|
309
|
+
e >>= 1n;
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
export function isOnEd25519Curve(bytes) {
|
|
314
|
+
if (!(bytes instanceof Uint8Array) || bytes.byteLength !== 32)
|
|
315
|
+
return false;
|
|
316
|
+
let y = 0n;
|
|
317
|
+
for (let i = 31; i >= 0; i--) {
|
|
318
|
+
const b = i === 31 ? bytes[i] & 0x7f : bytes[i];
|
|
319
|
+
y = (y << 8n) | BigInt(b);
|
|
320
|
+
}
|
|
321
|
+
const yMod = modP(y);
|
|
322
|
+
const y2 = (yMod * yMod) % ED25519_P;
|
|
323
|
+
const u = modP(y2 - 1n);
|
|
324
|
+
const v = modP(ED25519_D * y2 + 1n);
|
|
325
|
+
if (v === 0n)
|
|
326
|
+
return u === 0n;
|
|
327
|
+
const v2 = (v * v) % ED25519_P;
|
|
328
|
+
const v3 = (v2 * v) % ED25519_P;
|
|
329
|
+
const v7 = (((v3 * v3) % ED25519_P) * v) % ED25519_P;
|
|
330
|
+
const uv7 = (u * v7) % ED25519_P;
|
|
331
|
+
let x = (((u * v3) % ED25519_P) * modPowP(uv7, ED25519_EXP)) % ED25519_P;
|
|
332
|
+
const vx2 = (v * ((x * x) % ED25519_P)) % ED25519_P;
|
|
333
|
+
if (vx2 === u)
|
|
334
|
+
return true;
|
|
335
|
+
if (vx2 === modP(-u)) {
|
|
336
|
+
x = (x * ED25519_SQRT_M1) % ED25519_P;
|
|
337
|
+
return (v * ((x * x) % ED25519_P)) % ED25519_P === u;
|
|
338
|
+
}
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
const U32_MAX = 0xffff_ffff;
|
|
342
|
+
const U64_MAX = 0xffffffffffffffffn;
|
|
343
|
+
function assertLaneId(laneId) {
|
|
344
|
+
if (!Number.isInteger(laneId) || laneId < 0 || laneId >= NUM_LANES) {
|
|
345
|
+
throw new RangeError(`laneId must be 0..${NUM_LANES - 1}, got ${laneId}`);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
function u32le(n) {
|
|
349
|
+
if (!Number.isInteger(n) || n < 0 || n > U32_MAX) {
|
|
350
|
+
throw new RangeError(`u32 value out of range (0..=${U32_MAX}), got ${n}`);
|
|
351
|
+
}
|
|
352
|
+
const out = new Uint8Array(4);
|
|
353
|
+
new DataView(out.buffer).setUint32(0, n, true);
|
|
354
|
+
return out;
|
|
355
|
+
}
|
|
356
|
+
function u64le(n) {
|
|
357
|
+
if (typeof n !== 'bigint' || n < 0n || n > U64_MAX) {
|
|
358
|
+
throw new RangeError(`u64 value out of range (0..=2^64-1), got ${String(n)}`);
|
|
359
|
+
}
|
|
360
|
+
const out = new Uint8Array(8);
|
|
361
|
+
new DataView(out.buffer).setBigUint64(0, n, true);
|
|
362
|
+
return out;
|
|
363
|
+
}
|
|
364
|
+
const MAX_SEED_LEN = 32;
|
|
365
|
+
function sha256ConcatSync(parts) {
|
|
366
|
+
let total = 0;
|
|
367
|
+
for (const p of parts) {
|
|
368
|
+
if (!(p instanceof Uint8Array)) {
|
|
369
|
+
throw new RangeError(`Expected Uint8Array seed part, got ${String(p)}`);
|
|
370
|
+
}
|
|
371
|
+
total += p.byteLength;
|
|
372
|
+
}
|
|
373
|
+
const buf = new Uint8Array(total);
|
|
374
|
+
let off = 0;
|
|
375
|
+
for (const p of parts) {
|
|
376
|
+
buf.set(p, off);
|
|
377
|
+
off += p.byteLength;
|
|
378
|
+
}
|
|
379
|
+
return sha256(buf);
|
|
380
|
+
}
|
|
381
|
+
/** Synchronous Solana `Pubkey::find_program_address`. */
|
|
382
|
+
export function findProgramAddressSync(seeds, programId = AUDREN_PROGRAM_ID) {
|
|
383
|
+
if (!Array.isArray(seeds)) {
|
|
384
|
+
throw new RangeError('seeds must be an array of Uint8Arrays');
|
|
385
|
+
}
|
|
386
|
+
if (seeds.length >= 16) {
|
|
387
|
+
throw new RangeError(`Solana PDA supports at most 15 caller seeds, got ${seeds.length}`);
|
|
388
|
+
}
|
|
389
|
+
for (let i = 0; i < seeds.length; i++) {
|
|
390
|
+
const s = seeds[i];
|
|
391
|
+
if (!(s instanceof Uint8Array)) {
|
|
392
|
+
throw new RangeError(`PDA seed[${i}] must be a Uint8Array, got ${String(s)}`);
|
|
393
|
+
}
|
|
394
|
+
if (s.byteLength > MAX_SEED_LEN) {
|
|
395
|
+
throw new RangeError(`PDA seed[${i}] length ${s.byteLength} exceeds Solana MAX_SEED_LEN (${MAX_SEED_LEN})`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const programBytes = toPubkeyBytes(programId);
|
|
399
|
+
const bumpBuf = new Uint8Array(1);
|
|
400
|
+
for (let bump = 255; bump >= 0; bump--) {
|
|
401
|
+
bumpBuf[0] = bump;
|
|
402
|
+
const candidate = sha256ConcatSync([...seeds, bumpBuf, programBytes, PDA_MARKER]);
|
|
403
|
+
if (!isOnEd25519Curve(candidate)) {
|
|
404
|
+
return {
|
|
405
|
+
address: base58Encode(candidate),
|
|
406
|
+
addressBytes: candidate,
|
|
407
|
+
bump,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
throw new Error('Unable to find viable PDA bump');
|
|
412
|
+
}
|
|
413
|
+
export async function findProgramAddress(seeds, programId = AUDREN_PROGRAM_ID) {
|
|
414
|
+
return findProgramAddressSync(seeds, programId);
|
|
415
|
+
}
|
|
416
|
+
export function derivePoolPdaSync(poolId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
417
|
+
return findProgramAddressSync([utf8Encoder.encode(POOL_SEED), u32le(poolId)], programId);
|
|
418
|
+
}
|
|
419
|
+
export async function derivePoolPda(poolId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
420
|
+
return derivePoolPdaSync(poolId, programId);
|
|
421
|
+
}
|
|
422
|
+
export function deriveVaultAuthPdaSync(poolId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
423
|
+
return findProgramAddressSync([utf8Encoder.encode(VAULT_AUTH_SEED), u32le(poolId)], programId);
|
|
424
|
+
}
|
|
425
|
+
export async function deriveVaultAuthPda(poolId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
426
|
+
return deriveVaultAuthPdaSync(poolId, programId);
|
|
427
|
+
}
|
|
428
|
+
export function deriveLanePdaSync(poolId, laneId, programId = AUDREN_PROGRAM_ID) {
|
|
429
|
+
assertLaneId(laneId);
|
|
430
|
+
return findProgramAddressSync([utf8Encoder.encode(LANE_SEED), u32le(poolId), new Uint8Array([laneId])], programId);
|
|
431
|
+
}
|
|
432
|
+
export async function deriveLanePda(poolId, laneId, programId = AUDREN_PROGRAM_ID) {
|
|
433
|
+
return deriveLanePdaSync(poolId, laneId, programId);
|
|
434
|
+
}
|
|
435
|
+
export function deriveLaneRentVaultPdaSync(poolId, laneId, programId = AUDREN_PROGRAM_ID) {
|
|
436
|
+
assertLaneId(laneId);
|
|
437
|
+
return findProgramAddressSync([utf8Encoder.encode(LANE_RENT_SEED), u32le(poolId), new Uint8Array([laneId])], programId);
|
|
438
|
+
}
|
|
439
|
+
export async function deriveLaneRentVaultPda(poolId, laneId, programId = AUDREN_PROGRAM_ID) {
|
|
440
|
+
return deriveLaneRentVaultPdaSync(poolId, laneId, programId);
|
|
441
|
+
}
|
|
442
|
+
export function deriveSubtreePdaSync(poolId, laneId, pageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
443
|
+
assertLaneId(laneId);
|
|
444
|
+
return findProgramAddressSync([utf8Encoder.encode(SUBTREE_SEED), u32le(poolId), new Uint8Array([laneId]), u64le(pageSeq)], programId);
|
|
445
|
+
}
|
|
446
|
+
export async function deriveSubtreePda(poolId, laneId, pageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
447
|
+
return deriveSubtreePdaSync(poolId, laneId, pageSeq, programId);
|
|
448
|
+
}
|
|
449
|
+
export function deriveSubrootPdaSync(poolId, laneId, rootPageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
450
|
+
assertLaneId(laneId);
|
|
451
|
+
return findProgramAddressSync([utf8Encoder.encode(SUBROOT_SEED), u32le(poolId), new Uint8Array([laneId]), u64le(rootPageSeq)], programId);
|
|
452
|
+
}
|
|
453
|
+
export async function deriveSubrootPda(poolId, laneId, rootPageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
454
|
+
return deriveSubrootPdaSync(poolId, laneId, rootPageSeq, programId);
|
|
455
|
+
}
|
|
456
|
+
export function deriveMemoPdaSync(poolId, laneId, pageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
457
|
+
assertLaneId(laneId);
|
|
458
|
+
return findProgramAddressSync([utf8Encoder.encode(MEMO_SEED), u32le(poolId), new Uint8Array([laneId]), u64le(pageSeq)], programId);
|
|
459
|
+
}
|
|
460
|
+
export async function deriveMemoPda(poolId, laneId, pageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
461
|
+
return deriveMemoPdaSync(poolId, laneId, pageSeq, programId);
|
|
462
|
+
}
|
|
463
|
+
export function deriveNfPagePdaSync(poolId, laneId, pageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
464
|
+
assertLaneId(laneId);
|
|
465
|
+
return findProgramAddressSync([utf8Encoder.encode(NF_PAGE_SEED), u32le(poolId), new Uint8Array([laneId]), u64le(pageSeq)], programId);
|
|
466
|
+
}
|
|
467
|
+
export async function deriveNfPagePda(poolId, laneId, pageSeq, programId = AUDREN_PROGRAM_ID) {
|
|
468
|
+
return deriveNfPagePdaSync(poolId, laneId, pageSeq, programId);
|
|
469
|
+
}
|
|
470
|
+
export function deriveRootsPdaSync(poolId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
471
|
+
return findProgramAddressSync([utf8Encoder.encode(ROOTS_SEED), u32le(poolId)], programId);
|
|
472
|
+
}
|
|
473
|
+
export async function deriveRootsPda(poolId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
474
|
+
return deriveRootsPdaSync(poolId, programId);
|
|
475
|
+
}
|
|
476
|
+
export function deriveNullifierPdaSync(poolId, nullifier, programId = AUDREN_PROGRAM_ID) {
|
|
477
|
+
if (!(nullifier instanceof Uint8Array) || nullifier.byteLength !== 32) {
|
|
478
|
+
throw new RangeError(`nullifier must be a 32-byte Uint8Array, got ${nullifier instanceof Uint8Array ? nullifier.byteLength : String(nullifier)}`);
|
|
479
|
+
}
|
|
480
|
+
return findProgramAddressSync([utf8Encoder.encode(NULLIFIER_SEED), u32le(poolId), nullifier], programId);
|
|
481
|
+
}
|
|
482
|
+
export async function deriveNullifierPda(poolId, nullifier, programId = AUDREN_PROGRAM_ID) {
|
|
483
|
+
return deriveNullifierPdaSync(poolId, nullifier, programId);
|
|
484
|
+
}
|
|
485
|
+
export function deriveEscrowPdaSync(poolId = 0, shardId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
486
|
+
if (!Number.isInteger(shardId) || shardId < 0 || shardId >= NUM_ESCROW_SHARDS) {
|
|
487
|
+
throw new RangeError(`shardId must be 0..${NUM_ESCROW_SHARDS - 1}, got ${shardId}`);
|
|
488
|
+
}
|
|
489
|
+
return findProgramAddressSync([utf8Encoder.encode(ESCROW_SEED), u32le(poolId), new Uint8Array([shardId])], programId);
|
|
490
|
+
}
|
|
491
|
+
export async function deriveEscrowPda(poolId = 0, shardId = 0, programId = AUDREN_PROGRAM_ID) {
|
|
492
|
+
return deriveEscrowPdaSync(poolId, shardId, programId);
|
|
493
|
+
}
|
|
494
|
+
export function deriveUnwrapPdaSync(poolId, nf0, programId = AUDREN_PROGRAM_ID) {
|
|
495
|
+
if (!(nf0 instanceof Uint8Array) || nf0.byteLength !== 32) {
|
|
496
|
+
throw new RangeError(`nf0 must be a 32-byte Uint8Array, got ${nf0 instanceof Uint8Array ? nf0.byteLength : String(nf0)}`);
|
|
497
|
+
}
|
|
498
|
+
return findProgramAddressSync([utf8Encoder.encode(UNWRAP_SEED), u32le(poolId), nf0], programId);
|
|
499
|
+
}
|
|
500
|
+
export async function deriveUnwrapPda(poolId, nf0, programId = AUDREN_PROGRAM_ID) {
|
|
501
|
+
return deriveUnwrapPdaSync(poolId, nf0, programId);
|
|
502
|
+
}
|
|
503
|
+
export function deriveAssociatedTokenAddressSync(owner, mint, tokenProgramId = CLASSIC_TOKEN_PROGRAM_ID) {
|
|
504
|
+
return findProgramAddressSync([toPubkeyBytes(owner), toPubkeyBytes(tokenProgramId), toPubkeyBytes(mint)], ASSOCIATED_TOKEN_PROGRAM_ID);
|
|
505
|
+
}
|
|
506
|
+
export async function deriveAssociatedTokenAddress(owner, mint, tokenProgramId = CLASSIC_TOKEN_PROGRAM_ID) {
|
|
507
|
+
return deriveAssociatedTokenAddressSync(owner, mint, tokenProgramId);
|
|
508
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface PoseidonParams {
|
|
2
|
+
readonly width: number;
|
|
3
|
+
readonly fullRounds: number;
|
|
4
|
+
readonly partialRounds: number;
|
|
5
|
+
readonly ark: readonly bigint[];
|
|
6
|
+
readonly mds: readonly (readonly bigint[])[];
|
|
7
|
+
}
|
|
8
|
+
export declare const POSEIDON_PARAMS_2: PoseidonParams;
|
|
9
|
+
export declare const POSEIDON_PARAMS_3: PoseidonParams;
|
|
10
|
+
export declare const POSEIDON_PARAMS_4: PoseidonParams;
|
|
11
|
+
export declare const POSEIDON_PARAMS_5: PoseidonParams;
|
|
12
|
+
export declare const POSEIDON_PARAMS_6: PoseidonParams;
|
|
13
|
+
export declare const POSEIDON_PARAMS: Record<number, PoseidonParams>;
|