@rougechain/sdk 0.5.0 → 0.6.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
@@ -14,7 +14,7 @@
14
14
  <p align="center">
15
15
  <a href="https://www.npmjs.com/package/@rougechain/sdk"><img src="https://img.shields.io/npm/v/@rougechain/sdk?color=00d2be&label=npm" alt="npm version" /></a>
16
16
  <a href="https://www.npmjs.com/package/@rougechain/sdk"><img src="https://img.shields.io/npm/dm/@rougechain/sdk?color=00d2be" alt="npm downloads" /></a>
17
- <a href="https://github.com/cyberdreadx/quantum-vault/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license" /></a>
17
+ <a href="https://github.com/cyberdreadx/rougechain-node/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license" /></a>
18
18
  <a href="https://docs.rougechain.io"><img src="https://img.shields.io/badge/docs-rougechain-00d2be" alt="docs" /></a>
19
19
  </p>
20
20
 
@@ -64,14 +64,18 @@ const { balance } = await rc.getBalance(wallet.publicKey);
64
64
  | **Mail** | `rc.mail` | On-chain encrypted email (`@rouge.quant`) |
65
65
  | **Messenger** | `rc.messenger` | E2E encrypted messaging with self-destruct |
66
66
 
67
- ## Wallet
67
+ ## Wallet & Addresses
68
68
 
69
69
  ```typescript
70
- import { Wallet } from "@rougechain/sdk";
70
+ import { Wallet, pubkeyToAddress, isRougeAddress, formatAddress } from "@rougechain/sdk";
71
71
 
72
72
  // Generate a new post-quantum keypair
73
73
  const wallet = Wallet.generate();
74
74
 
75
+ // Get the compact rouge1... address (~63 chars vs 3904-char hex pubkey)
76
+ const address = await wallet.address();
77
+ // "rouge1q8f3x7k2m4n9p..."
78
+
75
79
  // Restore from saved keys
76
80
  const restored = Wallet.fromKeys(publicKey, privateKey);
77
81
 
@@ -80,6 +84,11 @@ const keys = wallet.toJSON(); // { publicKey, privateKey }
80
84
 
81
85
  // Verify keypair integrity
82
86
  wallet.verify(); // true
87
+
88
+ // Address utilities
89
+ const addr = await pubkeyToAddress(someHexPubKey);
90
+ const display = formatAddress(addr); // "rouge1q8f3x7...k9m2"
91
+ isRougeAddress("rouge1q8f3x7k2m4..."); // true
83
92
  ```
84
93
 
85
94
  ## Transfers & Tokens
@@ -428,7 +437,7 @@ import type {
428
437
  - [Website](https://rougechain.io)
429
438
  - [Documentation](https://docs.rougechain.io)
430
439
  - [Chrome Extension](https://chromewebstore.google.com/detail/rougechain-wallet/ilkbgjgphhaolfdjkfefdfiifipmhakj)
431
- - [GitHub](https://github.com/cyberdreadx/quantum-vault)
440
+ - [GitHub](https://github.com/cyberdreadx/rougechain-node)
432
441
 
433
442
  ## License
434
443
 
package/dist/index.cjs CHANGED
@@ -1056,6 +1056,130 @@ var ShieldedClient = class {
1056
1056
  return this.rc.submitTx("/v2/shielded/unshield", tx);
1057
1057
  }
1058
1058
  };
1059
+
1060
+ // src/address.ts
1061
+ var CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
1062
+ var BECH32M_CONST = 734539939;
1063
+ var HRP = "rouge";
1064
+ function hrpExpand(hrp) {
1065
+ const ret = [];
1066
+ for (let i = 0; i < hrp.length; i++) ret.push(hrp.charCodeAt(i) >> 5);
1067
+ ret.push(0);
1068
+ for (let i = 0; i < hrp.length; i++) ret.push(hrp.charCodeAt(i) & 31);
1069
+ return ret;
1070
+ }
1071
+ function polymod(values) {
1072
+ const GEN = [996825010, 642813549, 513874426, 1027748829, 705979059];
1073
+ let chk = 1;
1074
+ for (const v of values) {
1075
+ const b = chk >> 25;
1076
+ chk = (chk & 33554431) << 5 ^ v;
1077
+ for (let i = 0; i < 5; i++) {
1078
+ if (b >> i & 1) chk ^= GEN[i];
1079
+ }
1080
+ }
1081
+ return chk;
1082
+ }
1083
+ function createChecksum(hrp, data) {
1084
+ const values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
1085
+ const pm = polymod(values) ^ BECH32M_CONST;
1086
+ const ret = [];
1087
+ for (let i = 0; i < 6; i++) ret.push(pm >> 5 * (5 - i) & 31);
1088
+ return ret;
1089
+ }
1090
+ function verifyChecksum(hrp, data) {
1091
+ return polymod(hrpExpand(hrp).concat(data)) === BECH32M_CONST;
1092
+ }
1093
+ function convertBits(data, fromBits, toBits, pad) {
1094
+ let acc = 0;
1095
+ let bits = 0;
1096
+ const ret = [];
1097
+ const maxv = (1 << toBits) - 1;
1098
+ for (const value of data) {
1099
+ acc = acc << fromBits | value;
1100
+ bits += fromBits;
1101
+ while (bits >= toBits) {
1102
+ bits -= toBits;
1103
+ ret.push(acc >> bits & maxv);
1104
+ }
1105
+ }
1106
+ {
1107
+ if (bits > 0) ret.push(acc << toBits - bits & maxv);
1108
+ }
1109
+ return ret;
1110
+ }
1111
+ function bech32mEncode(hrp, data) {
1112
+ const data5bit = convertBits(data, 8, 5);
1113
+ const checksum = createChecksum(hrp, data5bit);
1114
+ const combined = data5bit.concat(checksum);
1115
+ let result = hrp + "1";
1116
+ for (const d of combined) result += CHARSET[d];
1117
+ return result;
1118
+ }
1119
+ function bech32mDecode(str) {
1120
+ const lower = str.toLowerCase();
1121
+ const pos = lower.lastIndexOf("1");
1122
+ if (pos < 1 || pos + 7 > lower.length) throw new Error("Invalid bech32m string");
1123
+ const hrp = lower.slice(0, pos);
1124
+ const data5bit = [];
1125
+ for (let i = pos + 1; i < lower.length; i++) {
1126
+ const d = CHARSET.indexOf(lower[i]);
1127
+ if (d === -1) throw new Error(`Invalid character: ${lower[i]}`);
1128
+ data5bit.push(d);
1129
+ }
1130
+ if (!verifyChecksum(hrp, data5bit)) throw new Error("Invalid bech32m checksum");
1131
+ const payload = data5bit.slice(0, data5bit.length - 6);
1132
+ const ret = [];
1133
+ let acc = 0, bits = 0;
1134
+ for (const value of payload) {
1135
+ acc = acc << 5 | value;
1136
+ bits += 5;
1137
+ while (bits >= 8) {
1138
+ bits -= 8;
1139
+ ret.push(acc >> bits & 255);
1140
+ }
1141
+ }
1142
+ return { hrp, data: new Uint8Array(ret) };
1143
+ }
1144
+ function hexToBytes2(hex) {
1145
+ const bytes = new Uint8Array(hex.length / 2);
1146
+ for (let i = 0; i < hex.length; i += 2) {
1147
+ bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
1148
+ }
1149
+ return bytes;
1150
+ }
1151
+ function bytesToHex2(bytes) {
1152
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
1153
+ }
1154
+ async function sha2562(data) {
1155
+ const buf = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
1156
+ const hash = await crypto.subtle.digest("SHA-256", buf);
1157
+ return new Uint8Array(hash);
1158
+ }
1159
+ async function pubkeyToAddress(publicKeyHex) {
1160
+ const pkBytes = hexToBytes2(publicKeyHex);
1161
+ const hash = await sha2562(pkBytes);
1162
+ return bech32mEncode(HRP, hash);
1163
+ }
1164
+ function addressToHash(address) {
1165
+ const { data } = bech32mDecode(address);
1166
+ return bytesToHex2(data);
1167
+ }
1168
+ function isRougeAddress(input) {
1169
+ if (!input.toLowerCase().startsWith("rouge1") || input.length < 10) return false;
1170
+ try {
1171
+ bech32mDecode(input);
1172
+ return true;
1173
+ } catch {
1174
+ return false;
1175
+ }
1176
+ }
1177
+ function formatAddress(address, prefixLen = 12, suffixLen = 4) {
1178
+ if (address.length <= prefixLen + suffixLen + 3) return address;
1179
+ return `${address.slice(0, prefixLen)}...${address.slice(-suffixLen)}`;
1180
+ }
1181
+
1182
+ // src/wallet.ts
1059
1183
  var Wallet = class _Wallet {
1060
1184
  constructor(publicKey, privateKey) {
1061
1185
  this.publicKey = publicKey;
@@ -1084,6 +1208,13 @@ var Wallet = class _Wallet {
1084
1208
  toJSON() {
1085
1209
  return { publicKey: this.publicKey, privateKey: this.privateKey };
1086
1210
  }
1211
+ /**
1212
+ * Derive the compact Bech32m address from the public key.
1213
+ * Returns a ~63-character `rouge1...` string.
1214
+ */
1215
+ async address() {
1216
+ return pubkeyToAddress(this.publicKey);
1217
+ }
1087
1218
  /**
1088
1219
  * Verify that the keypair is valid by signing and verifying a test message.
1089
1220
  */
@@ -1101,6 +1232,7 @@ var Wallet = class _Wallet {
1101
1232
  exports.BURN_ADDRESS = BURN_ADDRESS;
1102
1233
  exports.RougeChain = RougeChain;
1103
1234
  exports.Wallet = Wallet;
1235
+ exports.addressToHash = addressToHash;
1104
1236
  exports.bytesToHex = bytesToHex;
1105
1237
  exports.computeCommitment = computeCommitment;
1106
1238
  exports.computeNullifier = computeNullifier;
@@ -1111,10 +1243,13 @@ exports.createSignedShieldedTransfer = createSignedShieldedTransfer;
1111
1243
  exports.createSignedTokenMetadataClaim = createSignedTokenMetadataClaim;
1112
1244
  exports.createSignedTokenMetadataUpdate = createSignedTokenMetadataUpdate;
1113
1245
  exports.createSignedUnshield = createSignedUnshield;
1246
+ exports.formatAddress = formatAddress;
1114
1247
  exports.generateNonce = generateNonce;
1115
1248
  exports.generateRandomness = generateRandomness;
1116
1249
  exports.hexToBytes = hexToBytes;
1117
1250
  exports.isBurnAddress = isBurnAddress;
1251
+ exports.isRougeAddress = isRougeAddress;
1252
+ exports.pubkeyToAddress = pubkeyToAddress;
1118
1253
  exports.serializePayload = serializePayload;
1119
1254
  exports.signTransaction = signTransaction;
1120
1255
  exports.verifyTransaction = verifyTransaction;