@rougechain/sdk 0.5.0 → 0.7.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 +14 -4
- package/dist/index.cjs +154 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +149 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/client.d.ts +0 -199
- package/dist/client.d.ts.map +0 -1
- package/dist/index.d.ts +0 -9
- package/dist/index.d.ts.map +0 -1
- package/dist/shielded.d.ts +0 -43
- package/dist/shielded.d.ts.map +0 -1
- package/dist/signer.d.ts +0 -47
- package/dist/signer.d.ts.map +0 -1
- package/dist/types.d.ts +0 -448
- package/dist/types.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -4
- package/dist/utils.d.ts.map +0 -1
- package/dist/wallet.d.ts +0 -24
- package/dist/wallet.d.ts.map +0 -1
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/
|
|
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
|
|
|
@@ -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 |
|
|
@@ -64,14 +65,18 @@ const { balance } = await rc.getBalance(wallet.publicKey);
|
|
|
64
65
|
| **Mail** | `rc.mail` | On-chain encrypted email (`@rouge.quant`) |
|
|
65
66
|
| **Messenger** | `rc.messenger` | E2E encrypted messaging with self-destruct |
|
|
66
67
|
|
|
67
|
-
## Wallet
|
|
68
|
+
## Wallet & Addresses
|
|
68
69
|
|
|
69
70
|
```typescript
|
|
70
|
-
import { Wallet } from "@rougechain/sdk";
|
|
71
|
+
import { Wallet, pubkeyToAddress, isRougeAddress, formatAddress } from "@rougechain/sdk";
|
|
71
72
|
|
|
72
73
|
// Generate a new post-quantum keypair
|
|
73
74
|
const wallet = Wallet.generate();
|
|
74
75
|
|
|
76
|
+
// Get the compact rouge1... address (~63 chars vs 3904-char hex pubkey)
|
|
77
|
+
const address = await wallet.address();
|
|
78
|
+
// "rouge1q8f3x7k2m4n9p..."
|
|
79
|
+
|
|
75
80
|
// Restore from saved keys
|
|
76
81
|
const restored = Wallet.fromKeys(publicKey, privateKey);
|
|
77
82
|
|
|
@@ -80,6 +85,11 @@ const keys = wallet.toJSON(); // { publicKey, privateKey }
|
|
|
80
85
|
|
|
81
86
|
// Verify keypair integrity
|
|
82
87
|
wallet.verify(); // true
|
|
88
|
+
|
|
89
|
+
// Address utilities
|
|
90
|
+
const addr = await pubkeyToAddress(someHexPubKey);
|
|
91
|
+
const display = formatAddress(addr); // "rouge1q8f3x7...k9m2"
|
|
92
|
+
isRougeAddress("rouge1q8f3x7k2m4..."); // true
|
|
83
93
|
```
|
|
84
94
|
|
|
85
95
|
## Transfers & Tokens
|
|
@@ -428,7 +438,7 @@ import type {
|
|
|
428
438
|
- [Website](https://rougechain.io)
|
|
429
439
|
- [Documentation](https://docs.rougechain.io)
|
|
430
440
|
- [Chrome Extension](https://chromewebstore.google.com/detail/rougechain-wallet/ilkbgjgphhaolfdjkfefdfiifipmhakj)
|
|
431
|
-
- [GitHub](https://github.com/cyberdreadx/
|
|
441
|
+
- [GitHub](https://github.com/cyberdreadx/rougechain-node)
|
|
432
442
|
|
|
433
443
|
## License
|
|
434
444
|
|
package/dist/index.cjs
CHANGED
|
@@ -101,6 +101,23 @@ function createSignedTokenMetadataClaim(wallet, tokenSymbol) {
|
|
|
101
101
|
token_symbol: tokenSymbol
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
|
+
function createSignedTokenApproval(wallet, spender, tokenSymbol, amount) {
|
|
105
|
+
return buildAndSign(wallet, {
|
|
106
|
+
type: "approve",
|
|
107
|
+
spender,
|
|
108
|
+
token_symbol: tokenSymbol,
|
|
109
|
+
amount
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function createSignedTokenTransferFrom(wallet, owner, to, tokenSymbol, amount) {
|
|
113
|
+
return buildAndSign(wallet, {
|
|
114
|
+
type: "transfer_from",
|
|
115
|
+
owner,
|
|
116
|
+
to,
|
|
117
|
+
token_symbol: tokenSymbol,
|
|
118
|
+
amount
|
|
119
|
+
});
|
|
120
|
+
}
|
|
104
121
|
function createSignedSwap(wallet, tokenIn, tokenOut, amountIn, minAmountOut) {
|
|
105
122
|
return buildAndSign(wallet, {
|
|
106
123
|
type: "swap",
|
|
@@ -1056,6 +1073,130 @@ var ShieldedClient = class {
|
|
|
1056
1073
|
return this.rc.submitTx("/v2/shielded/unshield", tx);
|
|
1057
1074
|
}
|
|
1058
1075
|
};
|
|
1076
|
+
|
|
1077
|
+
// src/address.ts
|
|
1078
|
+
var CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
|
1079
|
+
var BECH32M_CONST = 734539939;
|
|
1080
|
+
var HRP = "rouge";
|
|
1081
|
+
function hrpExpand(hrp) {
|
|
1082
|
+
const ret = [];
|
|
1083
|
+
for (let i = 0; i < hrp.length; i++) ret.push(hrp.charCodeAt(i) >> 5);
|
|
1084
|
+
ret.push(0);
|
|
1085
|
+
for (let i = 0; i < hrp.length; i++) ret.push(hrp.charCodeAt(i) & 31);
|
|
1086
|
+
return ret;
|
|
1087
|
+
}
|
|
1088
|
+
function polymod(values) {
|
|
1089
|
+
const GEN = [996825010, 642813549, 513874426, 1027748829, 705979059];
|
|
1090
|
+
let chk = 1;
|
|
1091
|
+
for (const v of values) {
|
|
1092
|
+
const b = chk >> 25;
|
|
1093
|
+
chk = (chk & 33554431) << 5 ^ v;
|
|
1094
|
+
for (let i = 0; i < 5; i++) {
|
|
1095
|
+
if (b >> i & 1) chk ^= GEN[i];
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return chk;
|
|
1099
|
+
}
|
|
1100
|
+
function createChecksum(hrp, data) {
|
|
1101
|
+
const values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
|
|
1102
|
+
const pm = polymod(values) ^ BECH32M_CONST;
|
|
1103
|
+
const ret = [];
|
|
1104
|
+
for (let i = 0; i < 6; i++) ret.push(pm >> 5 * (5 - i) & 31);
|
|
1105
|
+
return ret;
|
|
1106
|
+
}
|
|
1107
|
+
function verifyChecksum(hrp, data) {
|
|
1108
|
+
return polymod(hrpExpand(hrp).concat(data)) === BECH32M_CONST;
|
|
1109
|
+
}
|
|
1110
|
+
function convertBits(data, fromBits, toBits, pad) {
|
|
1111
|
+
let acc = 0;
|
|
1112
|
+
let bits = 0;
|
|
1113
|
+
const ret = [];
|
|
1114
|
+
const maxv = (1 << toBits) - 1;
|
|
1115
|
+
for (const value of data) {
|
|
1116
|
+
acc = acc << fromBits | value;
|
|
1117
|
+
bits += fromBits;
|
|
1118
|
+
while (bits >= toBits) {
|
|
1119
|
+
bits -= toBits;
|
|
1120
|
+
ret.push(acc >> bits & maxv);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
{
|
|
1124
|
+
if (bits > 0) ret.push(acc << toBits - bits & maxv);
|
|
1125
|
+
}
|
|
1126
|
+
return ret;
|
|
1127
|
+
}
|
|
1128
|
+
function bech32mEncode(hrp, data) {
|
|
1129
|
+
const data5bit = convertBits(data, 8, 5);
|
|
1130
|
+
const checksum = createChecksum(hrp, data5bit);
|
|
1131
|
+
const combined = data5bit.concat(checksum);
|
|
1132
|
+
let result = hrp + "1";
|
|
1133
|
+
for (const d of combined) result += CHARSET[d];
|
|
1134
|
+
return result;
|
|
1135
|
+
}
|
|
1136
|
+
function bech32mDecode(str) {
|
|
1137
|
+
const lower = str.toLowerCase();
|
|
1138
|
+
const pos = lower.lastIndexOf("1");
|
|
1139
|
+
if (pos < 1 || pos + 7 > lower.length) throw new Error("Invalid bech32m string");
|
|
1140
|
+
const hrp = lower.slice(0, pos);
|
|
1141
|
+
const data5bit = [];
|
|
1142
|
+
for (let i = pos + 1; i < lower.length; i++) {
|
|
1143
|
+
const d = CHARSET.indexOf(lower[i]);
|
|
1144
|
+
if (d === -1) throw new Error(`Invalid character: ${lower[i]}`);
|
|
1145
|
+
data5bit.push(d);
|
|
1146
|
+
}
|
|
1147
|
+
if (!verifyChecksum(hrp, data5bit)) throw new Error("Invalid bech32m checksum");
|
|
1148
|
+
const payload = data5bit.slice(0, data5bit.length - 6);
|
|
1149
|
+
const ret = [];
|
|
1150
|
+
let acc = 0, bits = 0;
|
|
1151
|
+
for (const value of payload) {
|
|
1152
|
+
acc = acc << 5 | value;
|
|
1153
|
+
bits += 5;
|
|
1154
|
+
while (bits >= 8) {
|
|
1155
|
+
bits -= 8;
|
|
1156
|
+
ret.push(acc >> bits & 255);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
return { hrp, data: new Uint8Array(ret) };
|
|
1160
|
+
}
|
|
1161
|
+
function hexToBytes2(hex) {
|
|
1162
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
1163
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
1164
|
+
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
1165
|
+
}
|
|
1166
|
+
return bytes;
|
|
1167
|
+
}
|
|
1168
|
+
function bytesToHex2(bytes) {
|
|
1169
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1170
|
+
}
|
|
1171
|
+
async function sha2562(data) {
|
|
1172
|
+
const buf = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
|
1173
|
+
const hash = await crypto.subtle.digest("SHA-256", buf);
|
|
1174
|
+
return new Uint8Array(hash);
|
|
1175
|
+
}
|
|
1176
|
+
async function pubkeyToAddress(publicKeyHex) {
|
|
1177
|
+
const pkBytes = hexToBytes2(publicKeyHex);
|
|
1178
|
+
const hash = await sha2562(pkBytes);
|
|
1179
|
+
return bech32mEncode(HRP, hash);
|
|
1180
|
+
}
|
|
1181
|
+
function addressToHash(address) {
|
|
1182
|
+
const { data } = bech32mDecode(address);
|
|
1183
|
+
return bytesToHex2(data);
|
|
1184
|
+
}
|
|
1185
|
+
function isRougeAddress(input) {
|
|
1186
|
+
if (!input.toLowerCase().startsWith("rouge1") || input.length < 10) return false;
|
|
1187
|
+
try {
|
|
1188
|
+
bech32mDecode(input);
|
|
1189
|
+
return true;
|
|
1190
|
+
} catch {
|
|
1191
|
+
return false;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
function formatAddress(address, prefixLen = 12, suffixLen = 4) {
|
|
1195
|
+
if (address.length <= prefixLen + suffixLen + 3) return address;
|
|
1196
|
+
return `${address.slice(0, prefixLen)}...${address.slice(-suffixLen)}`;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// src/wallet.ts
|
|
1059
1200
|
var Wallet = class _Wallet {
|
|
1060
1201
|
constructor(publicKey, privateKey) {
|
|
1061
1202
|
this.publicKey = publicKey;
|
|
@@ -1084,6 +1225,13 @@ var Wallet = class _Wallet {
|
|
|
1084
1225
|
toJSON() {
|
|
1085
1226
|
return { publicKey: this.publicKey, privateKey: this.privateKey };
|
|
1086
1227
|
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Derive the compact Bech32m address from the public key.
|
|
1230
|
+
* Returns a ~63-character `rouge1...` string.
|
|
1231
|
+
*/
|
|
1232
|
+
async address() {
|
|
1233
|
+
return pubkeyToAddress(this.publicKey);
|
|
1234
|
+
}
|
|
1087
1235
|
/**
|
|
1088
1236
|
* Verify that the keypair is valid by signing and verifying a test message.
|
|
1089
1237
|
*/
|
|
@@ -1101,6 +1249,7 @@ var Wallet = class _Wallet {
|
|
|
1101
1249
|
exports.BURN_ADDRESS = BURN_ADDRESS;
|
|
1102
1250
|
exports.RougeChain = RougeChain;
|
|
1103
1251
|
exports.Wallet = Wallet;
|
|
1252
|
+
exports.addressToHash = addressToHash;
|
|
1104
1253
|
exports.bytesToHex = bytesToHex;
|
|
1105
1254
|
exports.computeCommitment = computeCommitment;
|
|
1106
1255
|
exports.computeNullifier = computeNullifier;
|
|
@@ -1108,13 +1257,18 @@ exports.createShieldedNote = createShieldedNote;
|
|
|
1108
1257
|
exports.createSignedBridgeWithdraw = createSignedBridgeWithdraw;
|
|
1109
1258
|
exports.createSignedShield = createSignedShield;
|
|
1110
1259
|
exports.createSignedShieldedTransfer = createSignedShieldedTransfer;
|
|
1260
|
+
exports.createSignedTokenApproval = createSignedTokenApproval;
|
|
1111
1261
|
exports.createSignedTokenMetadataClaim = createSignedTokenMetadataClaim;
|
|
1112
1262
|
exports.createSignedTokenMetadataUpdate = createSignedTokenMetadataUpdate;
|
|
1263
|
+
exports.createSignedTokenTransferFrom = createSignedTokenTransferFrom;
|
|
1113
1264
|
exports.createSignedUnshield = createSignedUnshield;
|
|
1265
|
+
exports.formatAddress = formatAddress;
|
|
1114
1266
|
exports.generateNonce = generateNonce;
|
|
1115
1267
|
exports.generateRandomness = generateRandomness;
|
|
1116
1268
|
exports.hexToBytes = hexToBytes;
|
|
1117
1269
|
exports.isBurnAddress = isBurnAddress;
|
|
1270
|
+
exports.isRougeAddress = isRougeAddress;
|
|
1271
|
+
exports.pubkeyToAddress = pubkeyToAddress;
|
|
1118
1272
|
exports.serializePayload = serializePayload;
|
|
1119
1273
|
exports.signTransaction = signTransaction;
|
|
1120
1274
|
exports.verifyTransaction = verifyTransaction;
|