@rialo/ts-cdk 0.4.1 → 0.5.0-alpha.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
@@ -10876,6 +10876,298 @@ var KeypairSigner = class {
10876
10876
  }
10877
10877
  };
10878
10878
 
10879
+ // src/generated/keyring.ts
10880
+ var Keyring = class {
10881
+ };
10882
+
10883
+ // src/keyring/validation.ts
10884
+ var U32_MAX = 4294967295;
10885
+ function validateKeypairIndex(index) {
10886
+ if (!Number.isInteger(index) || index < 0 || index > U32_MAX) {
10887
+ throw new RialoError(
10888
+ "INVALID_INPUT" /* INVALID_INPUT */,
10889
+ `Invalid keypair index: ${index}. Must be a non-negative integer <= ${U32_MAX}.`
10890
+ );
10891
+ }
10892
+ }
10893
+
10894
+ // src/keyring/keyring.ts
10895
+ var RialoKeyring = class extends Keyring {
10896
+ keypairs;
10897
+ derivationPaths;
10898
+ activeIndex;
10899
+ constructor(keypairs, derivationPaths) {
10900
+ super();
10901
+ this.keypairs = keypairs;
10902
+ this.derivationPaths = derivationPaths;
10903
+ this.activeIndex = 0;
10904
+ }
10905
+ /**
10906
+ * Sets the active keypair index (facade operation, not in WIT contract).
10907
+ *
10908
+ * @param index - The keypair index to make active
10909
+ * @throws {RialoError} If the index does not exist in this keyring
10910
+ */
10911
+ setActiveKeypair(index) {
10912
+ validateKeypairIndex(index);
10913
+ if (!this.keypairs.has(index)) {
10914
+ throw new RialoError(
10915
+ "WALLET" /* WALLET */,
10916
+ `Keypair ${index} not found in keyring`
10917
+ );
10918
+ }
10919
+ this.activeIndex = index;
10920
+ }
10921
+ activeKeypair() {
10922
+ const kp = this.keypairs.get(this.activeIndex);
10923
+ if (!kp) {
10924
+ throw new RialoError("WALLET" /* WALLET */, "Active keypair does not exist");
10925
+ }
10926
+ return kp;
10927
+ }
10928
+ sign(message) {
10929
+ return this.activeKeypair().sign(message).toBytes();
10930
+ }
10931
+ verify(message, sig) {
10932
+ return this.activeKeypair().verify(message, Signature.fromBytes(sig));
10933
+ }
10934
+ pubkeyString() {
10935
+ return this.activeKeypair().publicKey.toString();
10936
+ }
10937
+ pubkey() {
10938
+ return this.activeKeypair().publicKey;
10939
+ }
10940
+ getKeypairInfo(index) {
10941
+ validateKeypairIndex(index);
10942
+ const kp = this.keypairs.get(index);
10943
+ if (!kp) return void 0;
10944
+ return {
10945
+ index,
10946
+ pubkey: kp.publicKey,
10947
+ pubkeyString: kp.publicKey.toString(),
10948
+ derivationPath: this.derivationPaths.get(index) ?? void 0
10949
+ };
10950
+ }
10951
+ listKeypairs() {
10952
+ return Array.from(this.keypairs.keys()).sort((a, b) => a - b);
10953
+ }
10954
+ getKeypairsInfo() {
10955
+ return this.listKeypairs().map((index) => this.getKeypairInfo(index));
10956
+ }
10957
+ signWithKeypair(message, index) {
10958
+ validateKeypairIndex(index);
10959
+ const kp = this.keypairs.get(index);
10960
+ if (!kp) {
10961
+ throw new RialoError(
10962
+ "WALLET" /* WALLET */,
10963
+ `Keypair ${index} not found in keyring`
10964
+ );
10965
+ }
10966
+ return kp.sign(message).toBytes();
10967
+ }
10968
+ /**
10969
+ * Securely erases all secret key material from this keyring snapshot.
10970
+ *
10971
+ * Calls {@link Keypair.dispose} on every keypair, zeroing private key bytes.
10972
+ * After disposal, signing and secret-key export will throw. Verification
10973
+ * still works (uses only the public key). Does not affect provider-stored
10974
+ * state or other snapshots.
10975
+ */
10976
+ dispose() {
10977
+ for (const kp of this.keypairs.values()) {
10978
+ kp.dispose();
10979
+ }
10980
+ }
10981
+ };
10982
+
10983
+ // src/generated/keyring-provider.ts
10984
+ var KeyringProvider = class {
10985
+ };
10986
+
10987
+ // src/keyring/keyring-provider.ts
10988
+ var InMemoryKeyringProvider = class extends KeyringProvider {
10989
+ keyrings = /* @__PURE__ */ new Map();
10990
+ getStored(name) {
10991
+ const stored = this.keyrings.get(name);
10992
+ if (!stored) {
10993
+ throw new RialoError("WALLET" /* WALLET */, `Keyring not found: ${name}`);
10994
+ }
10995
+ return stored;
10996
+ }
10997
+ checkPassword(stored, password) {
10998
+ if (stored.password !== password) {
10999
+ throw new RialoError("PASSWORD" /* PASSWORD */, "Invalid password");
11000
+ }
11001
+ }
11002
+ buildKeyring(stored) {
11003
+ const keypairsCopy = /* @__PURE__ */ new Map();
11004
+ for (const [idx, kp] of stored.keypairs) {
11005
+ keypairsCopy.set(idx, Keypair.fromSecretKey(kp.secretKeyBytes()));
11006
+ }
11007
+ const pathsCopy = new Map(stored.derivationPaths);
11008
+ return new RialoKeyring(keypairsCopy, pathsCopy);
11009
+ }
11010
+ toInfo(index, kp, derivationPath) {
11011
+ return {
11012
+ index,
11013
+ pubkey: kp.publicKey,
11014
+ pubkeyString: kp.publicKey.toString(),
11015
+ derivationPath
11016
+ };
11017
+ }
11018
+ nextIndex(stored) {
11019
+ if (stored.keypairs.size === 0) return 0;
11020
+ return Math.max(...stored.keypairs.keys()) + 1;
11021
+ }
11022
+ async create(name, password) {
11023
+ if (this.keyrings.has(name)) {
11024
+ throw new RialoError("WALLET" /* WALLET */, `Keyring already exists: ${name}`);
11025
+ }
11026
+ const kp = Keypair.generate();
11027
+ const keypairs = /* @__PURE__ */ new Map([[0, kp]]);
11028
+ const derivationPaths = /* @__PURE__ */ new Map([[0, void 0]]);
11029
+ this.keyrings.set(name, { keypairs, derivationPaths, mnemonic: void 0, password });
11030
+ return this.buildKeyring(this.getStored(name));
11031
+ }
11032
+ async createWithMnemonic(name, strengthBits, password) {
11033
+ if (this.keyrings.has(name)) {
11034
+ throw new RialoError("WALLET" /* WALLET */, `Keyring already exists: ${name}`);
11035
+ }
11036
+ if (strengthBits !== 128 && strengthBits !== 256) {
11037
+ throw new RialoError(
11038
+ "INVALID_INPUT" /* INVALID_INPUT */,
11039
+ `Invalid mnemonic strength: ${strengthBits}. Must be 128 or 256.`
11040
+ );
11041
+ }
11042
+ const mnemonic = Mnemonic.generate(strengthBits);
11043
+ const kp = await mnemonic.toKeypair(0);
11044
+ const path = `${BASE_DERIVATION_PATH}0'/0'`;
11045
+ const keypairs = /* @__PURE__ */ new Map([[0, kp]]);
11046
+ const derivationPaths = /* @__PURE__ */ new Map([[0, path]]);
11047
+ this.keyrings.set(name, {
11048
+ keypairs,
11049
+ derivationPaths,
11050
+ mnemonic: mnemonic.toString(),
11051
+ password
11052
+ });
11053
+ return [this.buildKeyring(this.getStored(name)), mnemonic.toString()];
11054
+ }
11055
+ async recoverFromMnemonic(name, mnemonicPhrase, password) {
11056
+ if (this.keyrings.has(name)) {
11057
+ throw new RialoError("WALLET" /* WALLET */, `Keyring already exists: ${name}`);
11058
+ }
11059
+ if (!Mnemonic.isValid(mnemonicPhrase)) {
11060
+ throw new RialoError("INVALID_INPUT" /* INVALID_INPUT */, "Invalid BIP39 mnemonic phrase");
11061
+ }
11062
+ const mnemonic = Mnemonic.fromPhrase(mnemonicPhrase);
11063
+ const kp = await mnemonic.toKeypair(0);
11064
+ const path = `${BASE_DERIVATION_PATH}0'/0'`;
11065
+ const keypairs = /* @__PURE__ */ new Map([[0, kp]]);
11066
+ const derivationPaths = /* @__PURE__ */ new Map([[0, path]]);
11067
+ this.keyrings.set(name, {
11068
+ keypairs,
11069
+ derivationPaths,
11070
+ mnemonic: mnemonicPhrase,
11071
+ password
11072
+ });
11073
+ return this.buildKeyring(this.getStored(name));
11074
+ }
11075
+ async load(name, password) {
11076
+ const stored = this.getStored(name);
11077
+ this.checkPassword(stored, password);
11078
+ return this.buildKeyring(stored);
11079
+ }
11080
+ async list() {
11081
+ return Array.from(this.keyrings.keys()).sort();
11082
+ }
11083
+ async exists(name) {
11084
+ return this.keyrings.has(name);
11085
+ }
11086
+ async getPublicKey(name) {
11087
+ const stored = this.getStored(name);
11088
+ const kp = stored.keypairs.get(0);
11089
+ if (!kp) throw new RialoError("WALLET" /* WALLET */, `Keyring ${name} has no keypair at index 0`);
11090
+ return kp.publicKey;
11091
+ }
11092
+ async listPublicKeys() {
11093
+ const results = [];
11094
+ for (const [name, stored] of this.keyrings) {
11095
+ const kp = stored.keypairs.get(0);
11096
+ if (kp) {
11097
+ results.push([name, this.toInfo(0, kp, stored.derivationPaths.get(0))]);
11098
+ }
11099
+ }
11100
+ results.sort((a, b) => a[0].localeCompare(b[0]));
11101
+ return results;
11102
+ }
11103
+ async listKeypairs(keyringName) {
11104
+ const stored = this.getStored(keyringName);
11105
+ const indices = Array.from(stored.keypairs.keys()).sort((a, b) => a - b);
11106
+ return indices.map((idx) => {
11107
+ const kp = stored.keypairs.get(idx);
11108
+ return this.toInfo(idx, kp, stored.derivationPaths.get(idx));
11109
+ });
11110
+ }
11111
+ async deriveKeypair(keyringName, keypairIndex, password) {
11112
+ validateKeypairIndex(keypairIndex);
11113
+ const stored = this.getStored(keyringName);
11114
+ this.checkPassword(stored, password);
11115
+ if (stored.keypairs.has(keypairIndex)) {
11116
+ throw new RialoError(
11117
+ "WALLET" /* WALLET */,
11118
+ `Keypair with index ${keypairIndex} already exists`
11119
+ );
11120
+ }
11121
+ let kp;
11122
+ let derivationPath;
11123
+ if (stored.mnemonic) {
11124
+ const mnemonic = Mnemonic.fromPhrase(stored.mnemonic);
11125
+ kp = await mnemonic.toKeypair(keypairIndex);
11126
+ derivationPath = `${BASE_DERIVATION_PATH}${keypairIndex}'/0'`;
11127
+ } else {
11128
+ kp = Keypair.generate();
11129
+ derivationPath = void 0;
11130
+ }
11131
+ stored.keypairs.set(keypairIndex, kp);
11132
+ stored.derivationPaths.set(keypairIndex, derivationPath);
11133
+ return this.toInfo(keypairIndex, kp, derivationPath);
11134
+ }
11135
+ async importSecretKey(keyringName, secretKey, derivationPath, password) {
11136
+ if (secretKey.length !== 32) {
11137
+ throw new RialoError(
11138
+ "INVALID_INPUT" /* INVALID_INPUT */,
11139
+ `Invalid secret key length: expected 32 bytes, got ${secretKey.length}`
11140
+ );
11141
+ }
11142
+ const stored = this.getStored(keyringName);
11143
+ this.checkPassword(stored, password);
11144
+ const kp = Keypair.fromSecretKey(secretKey);
11145
+ const nextIdx = this.nextIndex(stored);
11146
+ stored.keypairs.set(nextIdx, kp);
11147
+ stored.derivationPaths.set(nextIdx, derivationPath);
11148
+ return this.toInfo(nextIdx, kp, derivationPath);
11149
+ }
11150
+ async getKeypairsInfo(name) {
11151
+ return this.listKeypairs(name);
11152
+ }
11153
+ async getKeypairInfo(name, keypairIndex) {
11154
+ validateKeypairIndex(keypairIndex);
11155
+ const stored = this.getStored(name);
11156
+ const kp = stored.keypairs.get(keypairIndex);
11157
+ if (!kp) {
11158
+ throw new RialoError(
11159
+ "WALLET" /* WALLET */,
11160
+ `Keypair ${keypairIndex} not found in keyring ${name}`
11161
+ );
11162
+ }
11163
+ return this.toInfo(keypairIndex, kp, stored.derivationPaths.get(keypairIndex));
11164
+ }
11165
+ async nextKeypairIndex(name) {
11166
+ const stored = this.getStored(name);
11167
+ return this.nextIndex(stored);
11168
+ }
11169
+ };
11170
+
10879
11171
  // src/program/constants.ts
10880
11172
  var RISCV_LOADER_PROGRAM_ID = "RiscVLoader11111111111111111111111111111111";
10881
11173
  var LOADER_V4_PROGRAM_ID = "LoaderV411111111111111111111111111111111111";
@@ -11272,9 +11564,12 @@ exports.HPKE_OVERHEAD_LENGTH = HPKE_OVERHEAD_LENGTH;
11272
11564
  exports.HpkeError = HpkeError;
11273
11565
  exports.HpkeErrorCode = HpkeErrorCode;
11274
11566
  exports.HttpTransport = HttpTransport;
11567
+ exports.InMemoryKeyringProvider = InMemoryKeyringProvider;
11275
11568
  exports.KELVIN_PER_RLO = KELVIN_PER_RLO;
11276
11569
  exports.Keypair = Keypair;
11277
11570
  exports.KeypairSigner = KeypairSigner;
11571
+ exports.Keyring = Keyring;
11572
+ exports.KeyringProvider = KeyringProvider;
11278
11573
  exports.LOADER_V4_PROGRAM_ID = LOADER_V4_PROGRAM_ID;
11279
11574
  exports.Message = Message;
11280
11575
  exports.Mnemonic = Mnemonic;
@@ -11293,6 +11588,7 @@ exports.RexValueVariant = RexValueVariant;
11293
11588
  exports.RialoClient = RialoClient;
11294
11589
  exports.RialoError = RialoError;
11295
11590
  exports.RialoErrorType = RialoErrorType;
11591
+ exports.RialoKeyring = RialoKeyring;
11296
11592
  exports.RiscVLoaderInstruction = RiscVLoaderInstruction;
11297
11593
  exports.RpcError = RpcError;
11298
11594
  exports.RpcErrorCode = RpcErrorCode;