@adelos/sdk 0.1.3 → 0.1.6

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.js CHANGED
@@ -33,7 +33,6 @@ __export(index_exports, {
33
33
  ADELOS_CONFIG: () => ADELOS_CONFIG,
34
34
  AdelosIndexer: () => AdelosIndexer,
35
35
  AdelosSDK: () => AdelosSDK,
36
- IDL: () => IDL,
37
36
  MEMO_PREFIX: () => MEMO_PREFIX,
38
37
  MEMO_PROGRAM_ID: () => MEMO_PROGRAM_ID,
39
38
  PROGRAM_ID: () => PROGRAM_ID,
@@ -56,7 +55,7 @@ __export(index_exports, {
56
55
  recoverStealthSecretKey: () => recoverStealthSecretKey
57
56
  });
58
57
  module.exports = __toCommonJS(index_exports);
59
- var import_web34 = require("@solana/web3.js");
58
+ var import_web33 = require("@solana/web3.js");
60
59
 
61
60
  // src/constants.ts
62
61
  var import_web3 = require("@solana/web3.js");
@@ -66,7 +65,8 @@ var ADELOS_CONFIG = {
66
65
  MEMO_PROGRAM_ID: new import_web3.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
67
66
  REGISTRY_SEED: "registry",
68
67
  MEMO_PREFIX: "ADLSv1:",
69
- STEALTH_DOMAIN: "adelos:stealth:v1"
68
+ STEALTH_DOMAIN: "adelos:stealth:v1",
69
+ UNLOCK_MESSAGE: "Adelos Protocol: Unlock Privacy Identity\n\nThis will derive your unique privacy key for stealth addresses. This does not cost gas."
70
70
  };
71
71
  var PROGRAM_ID = ADELOS_CONFIG.PROGRAM_ID;
72
72
  var RPC_URL = ADELOS_CONFIG.RPC_URL;
@@ -111,244 +111,8 @@ function getDiscriminator(instructionName) {
111
111
  return disc;
112
112
  }
113
113
 
114
- // src/idl.ts
115
- var IDL = {
116
- "address": "7T1UxHJ6psKiQheKZXxANu6mhgsmgaX55eNKZZL5u4Rp",
117
- "metadata": {
118
- "name": "adelos_registry",
119
- "version": "0.1.0",
120
- "spec": "0.1.0",
121
- "description": "Privacy Registry for Adelos Protocol"
122
- },
123
- "instructions": [
124
- {
125
- "name": "close_registry",
126
- "discriminator": [
127
- 76,
128
- 32,
129
- 154,
130
- 180,
131
- 51,
132
- 159,
133
- 218,
134
- 102
135
- ],
136
- "accounts": [
137
- {
138
- "name": "owner",
139
- "writable": true,
140
- "signer": true,
141
- "relations": [
142
- "registry"
143
- ]
144
- },
145
- {
146
- "name": "registry",
147
- "writable": true,
148
- "pda": {
149
- "seeds": [
150
- {
151
- "kind": "const",
152
- "value": [
153
- 114,
154
- 101,
155
- 103,
156
- 105,
157
- 115,
158
- 116,
159
- 114,
160
- 121
161
- ]
162
- },
163
- {
164
- "kind": "account",
165
- "path": "owner"
166
- }
167
- ]
168
- }
169
- }
170
- ],
171
- "args": []
172
- },
173
- {
174
- "name": "register_identity",
175
- "discriminator": [
176
- 164,
177
- 118,
178
- 227,
179
- 177,
180
- 47,
181
- 176,
182
- 187,
183
- 248
184
- ],
185
- "accounts": [
186
- {
187
- "name": "owner",
188
- "writable": true,
189
- "signer": true
190
- },
191
- {
192
- "name": "registry",
193
- "writable": true,
194
- "pda": {
195
- "seeds": [
196
- {
197
- "kind": "const",
198
- "value": [
199
- 114,
200
- 101,
201
- 103,
202
- 105,
203
- 115,
204
- 116,
205
- 114,
206
- 121
207
- ]
208
- },
209
- {
210
- "kind": "account",
211
- "path": "owner"
212
- }
213
- ]
214
- }
215
- },
216
- {
217
- "name": "system_program",
218
- "address": "11111111111111111111111111111111"
219
- }
220
- ],
221
- "args": [
222
- {
223
- "name": "meta_pubkey",
224
- "type": {
225
- "array": [
226
- "u8",
227
- 32
228
- ]
229
- }
230
- }
231
- ]
232
- },
233
- {
234
- "name": "update_identity",
235
- "discriminator": [
236
- 130,
237
- 54,
238
- 88,
239
- 104,
240
- 222,
241
- 124,
242
- 238,
243
- 252
244
- ],
245
- "accounts": [
246
- {
247
- "name": "owner",
248
- "writable": true,
249
- "signer": true,
250
- "relations": [
251
- "registry"
252
- ]
253
- },
254
- {
255
- "name": "registry",
256
- "writable": true,
257
- "pda": {
258
- "seeds": [
259
- {
260
- "kind": "const",
261
- "value": [
262
- 114,
263
- 101,
264
- 103,
265
- 105,
266
- 115,
267
- 116,
268
- 114,
269
- 121
270
- ]
271
- },
272
- {
273
- "kind": "account",
274
- "path": "owner"
275
- }
276
- ]
277
- }
278
- },
279
- {
280
- "name": "system_program",
281
- "address": "11111111111111111111111111111111"
282
- }
283
- ],
284
- "args": [
285
- {
286
- "name": "new_meta_pubkey",
287
- "type": {
288
- "array": [
289
- "u8",
290
- 32
291
- ]
292
- }
293
- }
294
- ]
295
- }
296
- ],
297
- "accounts": [
298
- {
299
- "name": "RegistryAccount",
300
- "discriminator": [
301
- 113,
302
- 93,
303
- 106,
304
- 201,
305
- 100,
306
- 166,
307
- 146,
308
- 98
309
- ]
310
- }
311
- ],
312
- "errors": [
313
- {
314
- "code": 6e3,
315
- "name": "InvalidMetaPubkey",
316
- "msg": "Invalid meta_pubkey"
317
- },
318
- {
319
- "code": 6001,
320
- "name": "Unauthorized",
321
- "msg": "Unauthorized"
322
- }
323
- ],
324
- "types": [
325
- {
326
- "name": "RegistryAccount",
327
- "type": {
328
- "kind": "struct",
329
- "fields": [
330
- {
331
- "name": "owner",
332
- "type": "pubkey"
333
- },
334
- {
335
- "name": "meta_pubkey",
336
- "type": {
337
- "array": [
338
- "u8",
339
- 32
340
- ]
341
- }
342
- },
343
- {
344
- "name": "bump",
345
- "type": "u8"
346
- }
347
- ]
348
- }
349
- }
350
- ]
351
- };
114
+ // src/index.ts
115
+ var import_sha2562 = require("@noble/hashes/sha256");
352
116
 
353
117
  // src/crypto.ts
354
118
  var import_sha256 = require("@noble/hashes/sha256");
@@ -360,24 +124,30 @@ ed.etc.sha512Sync = (...m) => {
360
124
  return h.digest();
361
125
  };
362
126
  var encoder = new TextEncoder();
127
+ var toScalar = (bytes) => {
128
+ const cleanBytes = bytes.length === 64 ? bytes.slice(0, 32) : bytes;
129
+ return ed.etc.mod(BigInt("0x" + bytesToHex(cleanBytes)), ed.CURVE.n);
130
+ };
363
131
  function generateEphemeralKeypair() {
364
132
  const secretKey = ed.utils.randomPrivateKey();
365
- return { secretKey, publicKey: ed.getPublicKey(secretKey) };
133
+ const scalar = toScalar(secretKey);
134
+ const publicKey = ed.ExtendedPoint.BASE.multiply(scalar).toRawBytes();
135
+ return { secretKey, publicKey };
366
136
  }
367
137
  function computeSharedSecret(ephemeralSk, recipientMetaPk) {
368
138
  const point = ed.ExtendedPoint.fromHex(bytesToHex(recipientMetaPk));
369
- const scalar = ed.etc.mod(BigInt("0x" + bytesToHex(ephemeralSk)), ed.CURVE.n);
139
+ const scalar = toScalar(ephemeralSk);
370
140
  return (0, import_sha256.sha256)(point.multiply(scalar).toRawBytes());
371
141
  }
372
142
  function computeSharedSecretAsRecipient(metaSk, ephemeralPk) {
373
143
  const point = ed.ExtendedPoint.fromHex(bytesToHex(ephemeralPk));
374
- const scalar = ed.etc.mod(BigInt("0x" + bytesToHex(metaSk)), ed.CURVE.n);
144
+ const scalar = toScalar(metaSk);
375
145
  return (0, import_sha256.sha256)(point.multiply(scalar).toRawBytes());
376
146
  }
377
147
  function deriveStealthPubkey(metaPk, sharedSecret) {
378
148
  const domain = encoder.encode(ADELOS_CONFIG.STEALTH_DOMAIN);
379
149
  const scalarBytes = (0, import_sha256.sha256)(new Uint8Array([...sharedSecret, ...domain]));
380
- const scalar = ed.etc.mod(BigInt("0x" + bytesToHex(scalarBytes)), ed.CURVE.n);
150
+ const scalar = toScalar(scalarBytes);
381
151
  const metaPoint = ed.ExtendedPoint.fromHex(bytesToHex(metaPk));
382
152
  const stealthPoint = metaPoint.add(ed.ExtendedPoint.BASE.multiply(scalar));
383
153
  return stealthPoint.toRawBytes();
@@ -385,8 +155,8 @@ function deriveStealthPubkey(metaPk, sharedSecret) {
385
155
  function recoverStealthSecretKey(metaSk, sharedSecret) {
386
156
  const domain = encoder.encode(ADELOS_CONFIG.STEALTH_DOMAIN);
387
157
  const scalarBytes = (0, import_sha256.sha256)(new Uint8Array([...sharedSecret, ...domain]));
388
- const scalar = ed.etc.mod(BigInt("0x" + bytesToHex(scalarBytes)), ed.CURVE.n);
389
- const metaScalar = ed.etc.mod(BigInt("0x" + bytesToHex(metaSk)), ed.CURVE.n);
158
+ const scalar = toScalar(scalarBytes);
159
+ const metaScalar = toScalar(metaSk);
390
160
  const stealthScalar = ed.etc.mod(metaScalar + scalar, ed.CURVE.n);
391
161
  const hex = stealthScalar.toString(16).padStart(64, "0");
392
162
  return hexToBytes(hex);
@@ -413,14 +183,10 @@ function generateStealthAddress(recipientMetaPk) {
413
183
  }
414
184
 
415
185
  // src/indexer.ts
416
- var import_web33 = require("@solana/web3.js");
417
- var AdelosIndexer = class _AdelosIndexer {
418
- constructor(config) {
419
- this.connection = new import_web33.Connection(config.rpcUrl, "confirmed");
420
- this.heliusApiKey = config.heliusApiKey;
421
- }
422
- static create(config) {
423
- return new _AdelosIndexer(config);
186
+ var AdelosIndexer = class {
187
+ // Sekarang langsung menerima objek Connection yang sudah jadi
188
+ constructor(connection) {
189
+ this.connection = connection;
424
190
  }
425
191
  /** Scan for stealth transfers to this recipient */
426
192
  async scanForStealthTransfers(metaSk, metaPk, limit = 100) {
@@ -448,7 +214,9 @@ var AdelosIndexer = class _AdelosIndexer {
448
214
  }
449
215
  return results;
450
216
  }
451
- /** Trial Decryption: Check if transaction is for this recipient */
217
+ /** * Trial Decryption: Mengecek apakah transaksi ini milik user.
218
+ * Dibuat 'public' agar bisa dites di file testing.
219
+ */
452
220
  attemptDecryption(tx, metaSk, metaPk) {
453
221
  const memo = this.extractMemo(tx);
454
222
  const ephemeralPk = parseStealthMemo(memo || "");
@@ -462,10 +230,10 @@ var AdelosIndexer = class _AdelosIndexer {
462
230
  if (idx === -1) return null;
463
231
  const preBalances = tx.meta?.preBalances || [];
464
232
  const postBalances = tx.meta?.postBalances || [];
465
- const change = (postBalances[idx] || 0) - (preBalances[idx] || 0);
233
+ const change = BigInt(postBalances[idx] || 0) - BigInt(preBalances[idx] || 0);
466
234
  return {
467
235
  stealthAddress: accounts[idx].pubkey,
468
- amount: BigInt(Math.max(change, 0))
236
+ amount: change > 0n ? change : 0n
469
237
  };
470
238
  }
471
239
  extractMemo(tx) {
@@ -475,125 +243,93 @@ var AdelosIndexer = class _AdelosIndexer {
475
243
  return ix?.parsed || null;
476
244
  }
477
245
  };
478
- function createIndexer(config) {
479
- return new AdelosIndexer(config);
246
+ function createIndexer(connection) {
247
+ return new AdelosIndexer(connection);
480
248
  }
481
249
 
482
250
  // src/index.ts
483
251
  var AdelosSDK = class {
484
252
  constructor(options = {}) {
485
- const rpcUrl = options.rpcUrl ?? ADELOS_CONFIG.RPC_URL;
486
- this.connection = new import_web34.Connection(rpcUrl, "confirmed");
253
+ this.connection = new import_web33.Connection(options.rpcUrl ?? ADELOS_CONFIG.RPC_URL, "confirmed");
487
254
  this.programId = ADELOS_CONFIG.PROGRAM_ID;
488
255
  }
489
- // --- Registry Operations ---
490
- deriveRegistryAddress(owner) {
491
- return deriveRegistryPda(owner, this.programId);
492
- }
256
+ // --- 1. Identity & Registry ---
493
257
  async getRegistry(owner) {
494
- const [address] = this.deriveRegistryAddress(owner);
258
+ const [address] = deriveRegistryPda(owner, this.programId);
495
259
  const accountInfo = await this.connection.getAccountInfo(address);
496
- if (!accountInfo) {
497
- return { address, exists: false, account: null };
498
- }
260
+ if (!accountInfo) return { address, exists: false, account: null };
499
261
  const data = accountInfo.data.slice(8);
500
- const account = {
501
- owner: new import_web34.PublicKey(data.slice(0, 32)),
502
- metaPubkey: new Uint8Array(data.slice(32, 64)),
503
- bump: data[64]
262
+ return {
263
+ address,
264
+ exists: true,
265
+ account: {
266
+ owner: new import_web33.PublicKey(data.slice(0, 32)),
267
+ metaPubkey: new Uint8Array(data.slice(32, 64)),
268
+ bump: data[64]
269
+ }
504
270
  };
505
- return { address, exists: true, account };
506
271
  }
507
- async getMetaPubkey(owner) {
508
- const registry = await this.getRegistry(owner);
509
- return registry.exists ? registry.account.metaPubkey : null;
272
+ /** Menghasilkan metaSk tanpa simpan di LocalStorage (Deterministic) */
273
+ async unlockPrivacy(signMessage) {
274
+ const message = new TextEncoder().encode(ADELOS_CONFIG.UNLOCK_MESSAGE);
275
+ const signature = await signMessage(message);
276
+ return (0, import_sha2562.sha256)(signature);
510
277
  }
511
- async isRegistered(owner) {
512
- const registry = await this.getRegistry(owner);
513
- return registry.exists;
278
+ // --- 2. High-Level Transaction Builders ---
279
+ /** API Satu Pintu untuk kirim SOL secara privat */
280
+ async createStealthTransfer(sender, receiver, amountSOL, version = "v0") {
281
+ const registry = await this.getRegistry(receiver);
282
+ if (!registry.exists) throw new Error("Penerima belum terdaftar.");
283
+ const { stealthPubkey, memo } = await generateStealthAddress(registry.account.metaPubkey);
284
+ const instructions = [
285
+ import_web33.SystemProgram.transfer({
286
+ fromPubkey: sender,
287
+ toPubkey: new import_web33.PublicKey(stealthPubkey),
288
+ lamports: BigInt(Math.round(amountSOL * 1e9))
289
+ }),
290
+ new import_web33.TransactionInstruction({
291
+ keys: [{ pubkey: sender, isSigner: true, isWritable: false }],
292
+ programId: ADELOS_CONFIG.MEMO_PROGRAM_ID,
293
+ data: Buffer.from(memo, "utf-8")
294
+ })
295
+ ];
296
+ return this.buildTransaction(sender, instructions, version);
514
297
  }
515
- // --- Instruction Builders ---
516
- createRegisterInstruction(owner, metaPubkey) {
517
- if (!isValidMetaPubkey(metaPubkey)) {
518
- throw new Error("Invalid meta pubkey: must be 32 bytes and not all zeros");
519
- }
520
- const [registryPda] = this.deriveRegistryAddress(owner);
521
- const data = Buffer.concat([
522
- Buffer.from(getDiscriminator("register_identity")),
523
- Buffer.from(metaPubkey)
524
- ]);
525
- return new import_web34.TransactionInstruction({
298
+ /** Membuat transaksi pendaftaran identitas */
299
+ async createRegisterTransaction(owner, metaPubkey, version = "v0") {
300
+ const [registryPda] = deriveRegistryPda(owner, this.programId);
301
+ const data = Buffer.concat([Buffer.from(getDiscriminator("register_identity")), Buffer.from(metaPubkey)]);
302
+ const ix = new import_web33.TransactionInstruction({
526
303
  keys: [
527
304
  { pubkey: owner, isSigner: true, isWritable: true },
528
305
  { pubkey: registryPda, isSigner: false, isWritable: true },
529
- { pubkey: import_web34.SystemProgram.programId, isSigner: false, isWritable: false }
306
+ { pubkey: import_web33.SystemProgram.programId, isSigner: false, isWritable: false }
530
307
  ],
531
308
  programId: this.programId,
532
309
  data
533
310
  });
311
+ return this.buildTransaction(owner, [ix], version);
534
312
  }
535
- createUpdateInstruction(owner, newMetaPubkey) {
536
- if (!isValidMetaPubkey(newMetaPubkey)) {
537
- throw new Error("Invalid meta pubkey: must be 32 bytes and not all zeros");
538
- }
539
- const [registryPda] = this.deriveRegistryAddress(owner);
540
- const data = Buffer.concat([
541
- Buffer.from(getDiscriminator("update_identity")),
542
- Buffer.from(newMetaPubkey)
543
- ]);
544
- return new import_web34.TransactionInstruction({
545
- keys: [
546
- { pubkey: owner, isSigner: true, isWritable: true },
547
- { pubkey: registryPda, isSigner: false, isWritable: true },
548
- { pubkey: import_web34.SystemProgram.programId, isSigner: false, isWritable: false }
549
- ],
550
- programId: this.programId,
551
- data
552
- });
553
- }
554
- createCloseInstruction(owner) {
555
- const [registryPda] = this.deriveRegistryAddress(owner);
556
- const data = Buffer.from(getDiscriminator("close_registry"));
557
- return new import_web34.TransactionInstruction({
558
- keys: [
559
- { pubkey: owner, isSigner: true, isWritable: true },
560
- { pubkey: registryPda, isSigner: false, isWritable: true }
561
- ],
562
- programId: this.programId,
563
- data
564
- });
565
- }
566
- // --- Transaction Builders ---
313
+ // --- 3. Core Engine (Internal Helpers) ---
567
314
  async buildTransaction(payer, instructions, version = "v0") {
568
315
  const { blockhash } = await this.connection.getLatestBlockhash();
569
316
  if (version === "legacy") {
570
- const tx = new import_web34.Transaction().add(...instructions);
317
+ const tx = new import_web33.Transaction().add(...instructions);
571
318
  tx.recentBlockhash = blockhash;
572
319
  tx.feePayer = payer;
573
320
  return tx;
574
321
  }
575
- const message = new import_web34.TransactionMessage({
322
+ const message = new import_web33.TransactionMessage({
576
323
  payerKey: payer,
577
324
  recentBlockhash: blockhash,
578
325
  instructions
579
326
  }).compileToV0Message();
580
- return new import_web34.VersionedTransaction(message);
581
- }
582
- async createRegisterTransaction(owner, metaPubkey) {
583
- const ix = this.createRegisterInstruction(owner, metaPubkey);
584
- return this.buildTransaction(owner, [ix], "legacy");
585
- }
586
- async createRegisterTransactionV0(owner, metaPubkey) {
587
- const ix = this.createRegisterInstruction(owner, metaPubkey);
588
- return this.buildTransaction(owner, [ix], "v0");
327
+ return new import_web33.VersionedTransaction(message);
589
328
  }
329
+ /** Satu fungsi kirim untuk semua jenis transaksi (Legacy/V0) */
590
330
  async sendAndConfirm(signedTx) {
591
- const sig = await this.connection.sendRawTransaction(signedTx.serialize());
592
- await this.connection.confirmTransaction(sig, "confirmed");
593
- return sig;
594
- }
595
- async sendAndConfirmV0(signedTx) {
596
- const sig = await this.connection.sendRawTransaction(signedTx.serialize());
331
+ const rawTx = signedTx instanceof import_web33.Transaction ? signedTx.serialize() : signedTx.serialize();
332
+ const sig = await this.connection.sendRawTransaction(rawTx);
597
333
  await this.connection.confirmTransaction(sig, "confirmed");
598
334
  return sig;
599
335
  }
@@ -603,7 +339,6 @@ var AdelosSDK = class {
603
339
  ADELOS_CONFIG,
604
340
  AdelosIndexer,
605
341
  AdelosSDK,
606
- IDL,
607
342
  MEMO_PREFIX,
608
343
  MEMO_PROGRAM_ID,
609
344
  PROGRAM_ID,