@rougechain/sdk 0.6.0 → 0.8.0

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
@@ -1,5 +1,9 @@
1
1
  import { ml_dsa65 } from '@noble/post-quantum/ml-dsa.js';
2
2
  import { sha256 } from '@noble/hashes/sha2';
3
+ import { generateMnemonic as generateMnemonic$1, validateMnemonic as validateMnemonic$1, mnemonicToSeedSync } from '@scure/bip39';
4
+ import { wordlist } from '@scure/bip39/wordlists/english.js';
5
+ import { hkdf } from '@noble/hashes/hkdf.js';
6
+ import { sha256 as sha256$1 } from '@noble/hashes/sha2.js';
3
7
 
4
8
  // src/signer.ts
5
9
 
@@ -99,6 +103,23 @@ function createSignedTokenMetadataClaim(wallet, tokenSymbol) {
99
103
  token_symbol: tokenSymbol
100
104
  });
101
105
  }
106
+ function createSignedTokenApproval(wallet, spender, tokenSymbol, amount) {
107
+ return buildAndSign(wallet, {
108
+ type: "approve",
109
+ spender,
110
+ token_symbol: tokenSymbol,
111
+ amount
112
+ });
113
+ }
114
+ function createSignedTokenTransferFrom(wallet, owner, to, tokenSymbol, amount) {
115
+ return buildAndSign(wallet, {
116
+ type: "transfer_from",
117
+ owner,
118
+ to,
119
+ token_symbol: tokenSymbol,
120
+ amount
121
+ });
122
+ }
102
123
  function createSignedSwap(wallet, tokenIn, tokenOut, amountIn, minAmountOut) {
103
124
  return buildAndSign(wallet, {
104
125
  type: "swap",
@@ -442,6 +463,19 @@ var RougeChain = class {
442
463
  async getBurnedTokens() {
443
464
  return this.get("/burned");
444
465
  }
466
+ // ===== Address Resolution =====
467
+ /**
468
+ * Resolve a rouge1… address to its public key, or a public key to its rouge1 address.
469
+ * Uses the persistent on-chain address index for O(1) lookups.
470
+ */
471
+ async resolveAddress(input) {
472
+ return this.get(`/resolve/${encodeURIComponent(input)}`);
473
+ }
474
+ // ===== Nonce =====
475
+ /** Get the current sequential nonce for an account. */
476
+ async getNonce(publicKey) {
477
+ return this.get(`/account/${encodeURIComponent(publicKey)}/nonce`);
478
+ }
445
479
  // ===== Write operations =====
446
480
  async transfer(wallet, params) {
447
481
  const tx = createSignedTransfer(
@@ -1176,24 +1210,69 @@ function formatAddress(address, prefixLen = 12, suffixLen = 4) {
1176
1210
  if (address.length <= prefixLen + suffixLen + 3) return address;
1177
1211
  return `${address.slice(0, prefixLen)}...${address.slice(-suffixLen)}`;
1178
1212
  }
1213
+ var DOMAIN_INFO = new TextEncoder().encode("rougechain-ml-dsa-65-v1");
1214
+ function generateMnemonic(strength = 256) {
1215
+ return generateMnemonic$1(wordlist, strength);
1216
+ }
1217
+ function validateMnemonic(mnemonic) {
1218
+ return validateMnemonic$1(mnemonic, wordlist);
1219
+ }
1220
+ function mnemonicToMLDSASeed(mnemonic, passphrase) {
1221
+ const bip39Seed = mnemonicToSeedSync(mnemonic, passphrase);
1222
+ return hkdf(sha256$1, bip39Seed, void 0, DOMAIN_INFO, 32);
1223
+ }
1224
+ function keypairFromMnemonic(mnemonic, passphrase) {
1225
+ if (!validateMnemonic(mnemonic)) {
1226
+ throw new Error("Invalid mnemonic phrase");
1227
+ }
1228
+ const seed = mnemonicToMLDSASeed(mnemonic, passphrase);
1229
+ const keypair = ml_dsa65.keygen(seed);
1230
+ return {
1231
+ publicKey: bytesToHex(keypair.publicKey),
1232
+ secretKey: bytesToHex(keypair.secretKey)
1233
+ };
1234
+ }
1179
1235
 
1180
1236
  // src/wallet.ts
1181
1237
  var Wallet = class _Wallet {
1182
- constructor(publicKey, privateKey) {
1238
+ constructor(publicKey, privateKey, mnemonic) {
1183
1239
  this.publicKey = publicKey;
1184
1240
  this.privateKey = privateKey;
1241
+ this.mnemonic = mnemonic;
1185
1242
  }
1186
1243
  /**
1187
- * Generate a new ML-DSA-65 keypair.
1188
- * Uses crypto.getRandomValues for secure randomness.
1244
+ * Generate a new ML-DSA-65 keypair with a BIP-39 mnemonic.
1245
+ * The mnemonic is stored on the wallet for backup/recovery.
1246
+ * @param strength 128 = 12 words (default), 256 = 24 words
1189
1247
  */
1190
- static generate() {
1248
+ static generate(strength = 128) {
1249
+ const mnemonic = generateMnemonic(strength);
1250
+ const { publicKey, secretKey } = keypairFromMnemonic(mnemonic);
1251
+ return new _Wallet(publicKey, secretKey, mnemonic);
1252
+ }
1253
+ /**
1254
+ * Generate a wallet using pure random entropy (no mnemonic).
1255
+ * Keys cannot be recovered from a seed phrase.
1256
+ */
1257
+ static generateRandom() {
1191
1258
  const keypair = ml_dsa65.keygen();
1192
1259
  return new _Wallet(
1193
1260
  bytesToHex(keypair.publicKey),
1194
1261
  bytesToHex(keypair.secretKey)
1195
1262
  );
1196
1263
  }
1264
+ /**
1265
+ * Restore a wallet from a BIP-39 mnemonic seed phrase.
1266
+ * @param mnemonic 12 or 24 word BIP-39 mnemonic
1267
+ * @param passphrase Optional BIP-39 passphrase (25th word)
1268
+ */
1269
+ static fromMnemonic(mnemonic, passphrase) {
1270
+ if (!validateMnemonic(mnemonic)) {
1271
+ throw new Error("Invalid mnemonic phrase");
1272
+ }
1273
+ const { publicKey, secretKey } = keypairFromMnemonic(mnemonic, passphrase);
1274
+ return new _Wallet(publicKey, secretKey, mnemonic);
1275
+ }
1197
1276
  /**
1198
1277
  * Restore a wallet from existing hex-encoded keys.
1199
1278
  */
@@ -1204,7 +1283,11 @@ var Wallet = class _Wallet {
1204
1283
  * Export keys as a plain object (for serialization/storage).
1205
1284
  */
1206
1285
  toJSON() {
1207
- return { publicKey: this.publicKey, privateKey: this.privateKey };
1286
+ return {
1287
+ publicKey: this.publicKey,
1288
+ privateKey: this.privateKey,
1289
+ ...this.mnemonic ? { mnemonic: this.mnemonic } : {}
1290
+ };
1208
1291
  }
1209
1292
  /**
1210
1293
  * Derive the compact Bech32m address from the public key.
@@ -1227,6 +1310,6 @@ var Wallet = class _Wallet {
1227
1310
  }
1228
1311
  };
1229
1312
 
1230
- export { BURN_ADDRESS, RougeChain, Wallet, addressToHash, bytesToHex, computeCommitment, computeNullifier, createShieldedNote, createSignedBridgeWithdraw, createSignedShield, createSignedShieldedTransfer, createSignedTokenMetadataClaim, createSignedTokenMetadataUpdate, createSignedUnshield, formatAddress, generateNonce, generateRandomness, hexToBytes, isBurnAddress, isRougeAddress, pubkeyToAddress, serializePayload, signTransaction, verifyTransaction };
1313
+ export { BURN_ADDRESS, RougeChain, Wallet, addressToHash, bytesToHex, computeCommitment, computeNullifier, createShieldedNote, createSignedBridgeWithdraw, createSignedShield, createSignedShieldedTransfer, createSignedTokenApproval, createSignedTokenMetadataClaim, createSignedTokenMetadataUpdate, createSignedTokenTransferFrom, createSignedUnshield, formatAddress, generateMnemonic, generateNonce, generateRandomness, hexToBytes, isBurnAddress, isRougeAddress, keypairFromMnemonic, mnemonicToMLDSASeed, pubkeyToAddress, serializePayload, signTransaction, validateMnemonic, verifyTransaction };
1231
1314
  //# sourceMappingURL=index.js.map
1232
1315
  //# sourceMappingURL=index.js.map