@rougechain/sdk 0.7.0 → 0.8.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.cjs CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  var mlDsa_js = require('@noble/post-quantum/ml-dsa.js');
4
4
  var sha2 = require('@noble/hashes/sha2');
5
+ var bip39 = require('@scure/bip39');
6
+ var english_js = require('@scure/bip39/wordlists/english.js');
7
+ var hkdf_js = require('@noble/hashes/hkdf.js');
8
+ var sha2_js = require('@noble/hashes/sha2.js');
5
9
 
6
10
  // src/signer.ts
7
11
 
@@ -461,6 +465,28 @@ var RougeChain = class {
461
465
  async getBurnedTokens() {
462
466
  return this.get("/burned");
463
467
  }
468
+ // ===== Address Resolution =====
469
+ /**
470
+ * Resolve a rouge1… address to its public key, or a public key to its rouge1 address.
471
+ * Uses the persistent on-chain address index for O(1) lookups.
472
+ */
473
+ async resolveAddress(input) {
474
+ return this.get(`/resolve/${encodeURIComponent(input)}`);
475
+ }
476
+ // ===== Nonce =====
477
+ /** Get the current sequential nonce for an account. */
478
+ async getNonce(publicKey) {
479
+ return this.get(`/account/${encodeURIComponent(publicKey)}/nonce`);
480
+ }
481
+ // ===== Push Notifications =====
482
+ /** Register an Expo push token for a wallet to receive notifications. */
483
+ async registerPushToken(publicKey, pushToken, platform = "expo") {
484
+ return this.post("/push/register", { publicKey, pushToken, platform });
485
+ }
486
+ /** Unregister push notifications for a wallet. */
487
+ async unregisterPushToken(publicKey) {
488
+ return this.post("/push/unregister", { publicKey });
489
+ }
464
490
  // ===== Write operations =====
465
491
  async transfer(wallet, params) {
466
492
  const tx = createSignedTransfer(
@@ -1195,24 +1221,69 @@ function formatAddress(address, prefixLen = 12, suffixLen = 4) {
1195
1221
  if (address.length <= prefixLen + suffixLen + 3) return address;
1196
1222
  return `${address.slice(0, prefixLen)}...${address.slice(-suffixLen)}`;
1197
1223
  }
1224
+ var DOMAIN_INFO = new TextEncoder().encode("rougechain-ml-dsa-65-v1");
1225
+ function generateMnemonic(strength = 256) {
1226
+ return bip39.generateMnemonic(english_js.wordlist, strength);
1227
+ }
1228
+ function validateMnemonic(mnemonic) {
1229
+ return bip39.validateMnemonic(mnemonic, english_js.wordlist);
1230
+ }
1231
+ function mnemonicToMLDSASeed(mnemonic, passphrase) {
1232
+ const bip39Seed = bip39.mnemonicToSeedSync(mnemonic, passphrase);
1233
+ return hkdf_js.hkdf(sha2_js.sha256, bip39Seed, void 0, DOMAIN_INFO, 32);
1234
+ }
1235
+ function keypairFromMnemonic(mnemonic, passphrase) {
1236
+ if (!validateMnemonic(mnemonic)) {
1237
+ throw new Error("Invalid mnemonic phrase");
1238
+ }
1239
+ const seed = mnemonicToMLDSASeed(mnemonic, passphrase);
1240
+ const keypair = mlDsa_js.ml_dsa65.keygen(seed);
1241
+ return {
1242
+ publicKey: bytesToHex(keypair.publicKey),
1243
+ secretKey: bytesToHex(keypair.secretKey)
1244
+ };
1245
+ }
1198
1246
 
1199
1247
  // src/wallet.ts
1200
1248
  var Wallet = class _Wallet {
1201
- constructor(publicKey, privateKey) {
1249
+ constructor(publicKey, privateKey, mnemonic) {
1202
1250
  this.publicKey = publicKey;
1203
1251
  this.privateKey = privateKey;
1252
+ this.mnemonic = mnemonic;
1253
+ }
1254
+ /**
1255
+ * Generate a new ML-DSA-65 keypair with a BIP-39 mnemonic.
1256
+ * The mnemonic is stored on the wallet for backup/recovery.
1257
+ * @param strength 128 = 12 words (default), 256 = 24 words
1258
+ */
1259
+ static generate(strength = 128) {
1260
+ const mnemonic = generateMnemonic(strength);
1261
+ const { publicKey, secretKey } = keypairFromMnemonic(mnemonic);
1262
+ return new _Wallet(publicKey, secretKey, mnemonic);
1204
1263
  }
1205
1264
  /**
1206
- * Generate a new ML-DSA-65 keypair.
1207
- * Uses crypto.getRandomValues for secure randomness.
1265
+ * Generate a wallet using pure random entropy (no mnemonic).
1266
+ * Keys cannot be recovered from a seed phrase.
1208
1267
  */
1209
- static generate() {
1268
+ static generateRandom() {
1210
1269
  const keypair = mlDsa_js.ml_dsa65.keygen();
1211
1270
  return new _Wallet(
1212
1271
  bytesToHex(keypair.publicKey),
1213
1272
  bytesToHex(keypair.secretKey)
1214
1273
  );
1215
1274
  }
1275
+ /**
1276
+ * Restore a wallet from a BIP-39 mnemonic seed phrase.
1277
+ * @param mnemonic 12 or 24 word BIP-39 mnemonic
1278
+ * @param passphrase Optional BIP-39 passphrase (25th word)
1279
+ */
1280
+ static fromMnemonic(mnemonic, passphrase) {
1281
+ if (!validateMnemonic(mnemonic)) {
1282
+ throw new Error("Invalid mnemonic phrase");
1283
+ }
1284
+ const { publicKey, secretKey } = keypairFromMnemonic(mnemonic, passphrase);
1285
+ return new _Wallet(publicKey, secretKey, mnemonic);
1286
+ }
1216
1287
  /**
1217
1288
  * Restore a wallet from existing hex-encoded keys.
1218
1289
  */
@@ -1223,7 +1294,11 @@ var Wallet = class _Wallet {
1223
1294
  * Export keys as a plain object (for serialization/storage).
1224
1295
  */
1225
1296
  toJSON() {
1226
- return { publicKey: this.publicKey, privateKey: this.privateKey };
1297
+ return {
1298
+ publicKey: this.publicKey,
1299
+ privateKey: this.privateKey,
1300
+ ...this.mnemonic ? { mnemonic: this.mnemonic } : {}
1301
+ };
1227
1302
  }
1228
1303
  /**
1229
1304
  * Derive the compact Bech32m address from the public key.
@@ -1263,14 +1338,18 @@ exports.createSignedTokenMetadataUpdate = createSignedTokenMetadataUpdate;
1263
1338
  exports.createSignedTokenTransferFrom = createSignedTokenTransferFrom;
1264
1339
  exports.createSignedUnshield = createSignedUnshield;
1265
1340
  exports.formatAddress = formatAddress;
1341
+ exports.generateMnemonic = generateMnemonic;
1266
1342
  exports.generateNonce = generateNonce;
1267
1343
  exports.generateRandomness = generateRandomness;
1268
1344
  exports.hexToBytes = hexToBytes;
1269
1345
  exports.isBurnAddress = isBurnAddress;
1270
1346
  exports.isRougeAddress = isRougeAddress;
1347
+ exports.keypairFromMnemonic = keypairFromMnemonic;
1348
+ exports.mnemonicToMLDSASeed = mnemonicToMLDSASeed;
1271
1349
  exports.pubkeyToAddress = pubkeyToAddress;
1272
1350
  exports.serializePayload = serializePayload;
1273
1351
  exports.signTransaction = signTransaction;
1352
+ exports.validateMnemonic = validateMnemonic;
1274
1353
  exports.verifyTransaction = verifyTransaction;
1275
1354
  //# sourceMappingURL=index.cjs.map
1276
1355
  //# sourceMappingURL=index.cjs.map