@toon-protocol/sdk 0.4.0 → 0.5.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
@@ -10,7 +10,10 @@ import { wordlist } from "@scure/bip39/wordlists/english.js";
10
10
  import { HDKey } from "@scure/bip32";
11
11
  import { getPublicKey } from "nostr-tools/pure";
12
12
  import { secp256k1 } from "@noble/curves/secp256k1.js";
13
+ import { ed25519 } from "@noble/curves/ed25519.js";
13
14
  import { keccak_256 } from "@noble/hashes/sha3.js";
15
+ import { hmac } from "@noble/hashes/hmac.js";
16
+ import { sha512 } from "@noble/hashes/sha512.js";
14
17
  import { bytesToHex } from "@noble/hashes/utils.js";
15
18
 
16
19
  // src/errors.ts
@@ -71,7 +74,9 @@ function fromMnemonic(mnemonic, options) {
71
74
  throw new IdentityError(`Failed to derive private key at path ${path}`);
72
75
  }
73
76
  const secretKey = hdKey.privateKey;
74
- return deriveIdentity(secretKey);
77
+ const base = deriveIdentity(secretKey);
78
+ const solana = deriveSolanaIdentity(seed);
79
+ return { ...base, solana };
75
80
  } catch (error) {
76
81
  if (error instanceof IdentityError) {
77
82
  throw error;
@@ -140,6 +145,122 @@ function toChecksumAddress(addressHex) {
140
145
  }
141
146
  return checksummed;
142
147
  }
148
+ function slip0010Derive(seed, path) {
149
+ const encoder = new TextEncoder();
150
+ let I = hmac(sha512, encoder.encode("ed25519 seed"), seed);
151
+ let key = I.slice(0, 32);
152
+ let chainCode = I.slice(32);
153
+ for (const index of path) {
154
+ const data = new Uint8Array(37);
155
+ data[0] = 0;
156
+ data.set(key, 1);
157
+ data[33] = index >>> 24 & 255;
158
+ data[34] = index >>> 16 & 255;
159
+ data[35] = index >>> 8 & 255;
160
+ data[36] = index & 255;
161
+ I = hmac(sha512, chainCode, data);
162
+ key = I.slice(0, 32);
163
+ chainCode = I.slice(32);
164
+ }
165
+ return key;
166
+ }
167
+ var SOLANA_PATH = [
168
+ 2147483692,
169
+ // 44'
170
+ 2147484149,
171
+ // 501'
172
+ 2147483648,
173
+ // 0'
174
+ 2147483648
175
+ // 0'
176
+ ];
177
+ function deriveSolanaIdentity(seed) {
178
+ const privateKey = slip0010Derive(seed, SOLANA_PATH);
179
+ const publicKeyBytes = ed25519.getPublicKey(privateKey);
180
+ const keypair = new Uint8Array(64);
181
+ keypair.set(privateKey, 0);
182
+ keypair.set(publicKeyBytes, 32);
183
+ return { secretKey: keypair, publicKey: base58Encode(publicKeyBytes) };
184
+ }
185
+ async function deriveMinaIdentity(seed) {
186
+ const path = "m/44'/12586'/0'/0/0";
187
+ const hdKey = HDKey.fromMasterSeed(seed).derive(path);
188
+ if (!hdKey.privateKey) {
189
+ throw new IdentityError(`Failed to derive Mina private key at path ${path}`);
190
+ }
191
+ const keyBytes = new Uint8Array(hdKey.privateKey);
192
+ const hexKey = bytesToHex(keyBytes);
193
+ try {
194
+ const MinaSignerLib = await import("mina-signer");
195
+ const Client = "default" in MinaSignerLib ? MinaSignerLib.default : MinaSignerLib;
196
+ const client = new Client({ network: "mainnet" });
197
+ const publicKey = client.derivePublicKey(hexKey);
198
+ return { privateKey: hexKey, publicKey };
199
+ } catch {
200
+ return void 0;
201
+ }
202
+ }
203
+ async function fromMnemonicFull(mnemonic, options) {
204
+ const identity = fromMnemonic(mnemonic, options);
205
+ let seed;
206
+ try {
207
+ seed = mnemonicToSeedSync(mnemonic);
208
+ const mina = await deriveMinaIdentity(seed);
209
+ if (mina) {
210
+ return { ...identity, mina };
211
+ }
212
+ } finally {
213
+ if (seed) {
214
+ seed.fill(0);
215
+ }
216
+ }
217
+ return identity;
218
+ }
219
+ function generateSolanaKeypair() {
220
+ const privateKey = ed25519.utils.randomSecretKey();
221
+ const publicKeyBytes = ed25519.getPublicKey(privateKey);
222
+ const keypair = new Uint8Array(64);
223
+ keypair.set(privateKey, 0);
224
+ keypair.set(publicKeyBytes, 32);
225
+ return { secretKey: keypair, publicKey: base58Encode(publicKeyBytes) };
226
+ }
227
+ var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
228
+ function base58Encode(bytes) {
229
+ let zeros = 0;
230
+ for (let i = 0; i < bytes.length && bytes[i] === 0; i++) zeros++;
231
+ let value = 0n;
232
+ for (const byte of bytes) {
233
+ value = value * 256n + BigInt(byte);
234
+ }
235
+ let result = "";
236
+ while (value > 0n) {
237
+ result = BASE58_ALPHABET[Number(value % 58n)] + result;
238
+ value = value / 58n;
239
+ }
240
+ for (let i = 0; i < zeros; i++) {
241
+ result = "1" + result;
242
+ }
243
+ return result || "1";
244
+ }
245
+ function base58Decode(str) {
246
+ let zeros = 0;
247
+ for (let i = 0; i < str.length && str[i] === "1"; i++) zeros++;
248
+ let value = 0n;
249
+ for (const ch of str) {
250
+ const idx = BASE58_ALPHABET.indexOf(ch);
251
+ if (idx === -1) throw new IdentityError(`Invalid base58 character: ${ch}`);
252
+ value = value * 58n + BigInt(idx);
253
+ }
254
+ const hex = value === 0n ? "" : value.toString(16);
255
+ const hexPadded = hex.length % 2 ? "0" + hex : hex;
256
+ const rawBytes = [];
257
+ for (let i = 0; i < hexPadded.length; i += 2) {
258
+ rawBytes.push(parseInt(hexPadded.slice(i, i + 2), 16));
259
+ }
260
+ const result = new Uint8Array(zeros + rawBytes.length);
261
+ result.set(rawBytes, zeros);
262
+ return result;
263
+ }
143
264
 
144
265
  // src/handler-context.ts
145
266
  function createHandlerContext(options) {
@@ -2073,6 +2194,8 @@ export {
2073
2194
  TurboUploadAdapter,
2074
2195
  VerificationError,
2075
2196
  WorkflowOrchestrator,
2197
+ base58Decode,
2198
+ base58Encode,
2076
2199
  buildSkillDescriptor,
2077
2200
  createArweaveDvmHandler,
2078
2201
  createEventStorageHandler,
@@ -2083,8 +2206,10 @@ export {
2083
2206
  createPricingValidator,
2084
2207
  createVerificationPipeline,
2085
2208
  fromMnemonic,
2209
+ fromMnemonicFull,
2086
2210
  fromSecretKey,
2087
2211
  generateMnemonic,
2212
+ generateSolanaKeypair,
2088
2213
  uploadBlob,
2089
2214
  uploadBlobChunked
2090
2215
  };