@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/README.md CHANGED
@@ -55,6 +55,7 @@ const { balance } = await rc.getBalance(wallet.publicKey);
55
55
  | **Wallet** | — | ML-DSA-65 keypair generation, import/export, client-side signing |
56
56
  | **Transfers** | `rc` | Send XRGE or custom tokens, burn tokens |
57
57
  | **Token Creation** | `rc` | Launch new tokens with on-chain logo support |
58
+ | **Token Allowances** | `rc` | ERC-20 style approve/transferFrom for DeFi composability |
58
59
  | **Staking** | `rc` | Stake/unstake XRGE for validation |
59
60
  | **DEX** | `rc.dex` | AMM pools, swaps with slippage protection, liquidity |
60
61
  | **NFTs** | `rc.nft` | RC-721 collections, mint, batch mint, royalties, freeze |
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
 
@@ -101,6 +105,23 @@ function createSignedTokenMetadataClaim(wallet, tokenSymbol) {
101
105
  token_symbol: tokenSymbol
102
106
  });
103
107
  }
108
+ function createSignedTokenApproval(wallet, spender, tokenSymbol, amount) {
109
+ return buildAndSign(wallet, {
110
+ type: "approve",
111
+ spender,
112
+ token_symbol: tokenSymbol,
113
+ amount
114
+ });
115
+ }
116
+ function createSignedTokenTransferFrom(wallet, owner, to, tokenSymbol, amount) {
117
+ return buildAndSign(wallet, {
118
+ type: "transfer_from",
119
+ owner,
120
+ to,
121
+ token_symbol: tokenSymbol,
122
+ amount
123
+ });
124
+ }
104
125
  function createSignedSwap(wallet, tokenIn, tokenOut, amountIn, minAmountOut) {
105
126
  return buildAndSign(wallet, {
106
127
  type: "swap",
@@ -444,6 +465,19 @@ var RougeChain = class {
444
465
  async getBurnedTokens() {
445
466
  return this.get("/burned");
446
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
+ }
447
481
  // ===== Write operations =====
448
482
  async transfer(wallet, params) {
449
483
  const tx = createSignedTransfer(
@@ -1178,24 +1212,69 @@ function formatAddress(address, prefixLen = 12, suffixLen = 4) {
1178
1212
  if (address.length <= prefixLen + suffixLen + 3) return address;
1179
1213
  return `${address.slice(0, prefixLen)}...${address.slice(-suffixLen)}`;
1180
1214
  }
1215
+ var DOMAIN_INFO = new TextEncoder().encode("rougechain-ml-dsa-65-v1");
1216
+ function generateMnemonic(strength = 256) {
1217
+ return bip39.generateMnemonic(english_js.wordlist, strength);
1218
+ }
1219
+ function validateMnemonic(mnemonic) {
1220
+ return bip39.validateMnemonic(mnemonic, english_js.wordlist);
1221
+ }
1222
+ function mnemonicToMLDSASeed(mnemonic, passphrase) {
1223
+ const bip39Seed = bip39.mnemonicToSeedSync(mnemonic, passphrase);
1224
+ return hkdf_js.hkdf(sha2_js.sha256, bip39Seed, void 0, DOMAIN_INFO, 32);
1225
+ }
1226
+ function keypairFromMnemonic(mnemonic, passphrase) {
1227
+ if (!validateMnemonic(mnemonic)) {
1228
+ throw new Error("Invalid mnemonic phrase");
1229
+ }
1230
+ const seed = mnemonicToMLDSASeed(mnemonic, passphrase);
1231
+ const keypair = mlDsa_js.ml_dsa65.keygen(seed);
1232
+ return {
1233
+ publicKey: bytesToHex(keypair.publicKey),
1234
+ secretKey: bytesToHex(keypair.secretKey)
1235
+ };
1236
+ }
1181
1237
 
1182
1238
  // src/wallet.ts
1183
1239
  var Wallet = class _Wallet {
1184
- constructor(publicKey, privateKey) {
1240
+ constructor(publicKey, privateKey, mnemonic) {
1185
1241
  this.publicKey = publicKey;
1186
1242
  this.privateKey = privateKey;
1243
+ this.mnemonic = mnemonic;
1187
1244
  }
1188
1245
  /**
1189
- * Generate a new ML-DSA-65 keypair.
1190
- * Uses crypto.getRandomValues for secure randomness.
1246
+ * Generate a new ML-DSA-65 keypair with a BIP-39 mnemonic.
1247
+ * The mnemonic is stored on the wallet for backup/recovery.
1248
+ * @param strength 128 = 12 words (default), 256 = 24 words
1191
1249
  */
1192
- static generate() {
1250
+ static generate(strength = 128) {
1251
+ const mnemonic = generateMnemonic(strength);
1252
+ const { publicKey, secretKey } = keypairFromMnemonic(mnemonic);
1253
+ return new _Wallet(publicKey, secretKey, mnemonic);
1254
+ }
1255
+ /**
1256
+ * Generate a wallet using pure random entropy (no mnemonic).
1257
+ * Keys cannot be recovered from a seed phrase.
1258
+ */
1259
+ static generateRandom() {
1193
1260
  const keypair = mlDsa_js.ml_dsa65.keygen();
1194
1261
  return new _Wallet(
1195
1262
  bytesToHex(keypair.publicKey),
1196
1263
  bytesToHex(keypair.secretKey)
1197
1264
  );
1198
1265
  }
1266
+ /**
1267
+ * Restore a wallet from a BIP-39 mnemonic seed phrase.
1268
+ * @param mnemonic 12 or 24 word BIP-39 mnemonic
1269
+ * @param passphrase Optional BIP-39 passphrase (25th word)
1270
+ */
1271
+ static fromMnemonic(mnemonic, passphrase) {
1272
+ if (!validateMnemonic(mnemonic)) {
1273
+ throw new Error("Invalid mnemonic phrase");
1274
+ }
1275
+ const { publicKey, secretKey } = keypairFromMnemonic(mnemonic, passphrase);
1276
+ return new _Wallet(publicKey, secretKey, mnemonic);
1277
+ }
1199
1278
  /**
1200
1279
  * Restore a wallet from existing hex-encoded keys.
1201
1280
  */
@@ -1206,7 +1285,11 @@ var Wallet = class _Wallet {
1206
1285
  * Export keys as a plain object (for serialization/storage).
1207
1286
  */
1208
1287
  toJSON() {
1209
- return { publicKey: this.publicKey, privateKey: this.privateKey };
1288
+ return {
1289
+ publicKey: this.publicKey,
1290
+ privateKey: this.privateKey,
1291
+ ...this.mnemonic ? { mnemonic: this.mnemonic } : {}
1292
+ };
1210
1293
  }
1211
1294
  /**
1212
1295
  * Derive the compact Bech32m address from the public key.
@@ -1240,18 +1323,24 @@ exports.createShieldedNote = createShieldedNote;
1240
1323
  exports.createSignedBridgeWithdraw = createSignedBridgeWithdraw;
1241
1324
  exports.createSignedShield = createSignedShield;
1242
1325
  exports.createSignedShieldedTransfer = createSignedShieldedTransfer;
1326
+ exports.createSignedTokenApproval = createSignedTokenApproval;
1243
1327
  exports.createSignedTokenMetadataClaim = createSignedTokenMetadataClaim;
1244
1328
  exports.createSignedTokenMetadataUpdate = createSignedTokenMetadataUpdate;
1329
+ exports.createSignedTokenTransferFrom = createSignedTokenTransferFrom;
1245
1330
  exports.createSignedUnshield = createSignedUnshield;
1246
1331
  exports.formatAddress = formatAddress;
1332
+ exports.generateMnemonic = generateMnemonic;
1247
1333
  exports.generateNonce = generateNonce;
1248
1334
  exports.generateRandomness = generateRandomness;
1249
1335
  exports.hexToBytes = hexToBytes;
1250
1336
  exports.isBurnAddress = isBurnAddress;
1251
1337
  exports.isRougeAddress = isRougeAddress;
1338
+ exports.keypairFromMnemonic = keypairFromMnemonic;
1339
+ exports.mnemonicToMLDSASeed = mnemonicToMLDSASeed;
1252
1340
  exports.pubkeyToAddress = pubkeyToAddress;
1253
1341
  exports.serializePayload = serializePayload;
1254
1342
  exports.signTransaction = signTransaction;
1343
+ exports.validateMnemonic = validateMnemonic;
1255
1344
  exports.verifyTransaction = verifyTransaction;
1256
1345
  //# sourceMappingURL=index.cjs.map
1257
1346
  //# sourceMappingURL=index.cjs.map