|
| 1 | +import { ed25519CreateDerive, sr25519CreateDerive } from "@polkadot-labs/hdkd" |
| 2 | +import { entropyToMiniSecret, mnemonicToEntropy } from "@polkadot-labs/hdkd-helpers" |
| 3 | +import * as ss58 from "@subsquid/ss58" |
| 4 | +import type { PolkadotSigner } from "polkadot-api/signer" |
| 5 | +import { getPolkadotSigner } from "polkadot-api/signer" |
| 6 | + |
| 7 | +import { getAllSupportedChains, getChainById } from "../chains/chains" |
| 8 | +import type { AgentConfig } from "../types" |
| 9 | + |
| 10 | +/** |
| 11 | + * Convert a public key (Uint8Array) to a Substrate address |
| 12 | + * @param publicKey - The public key as Uint8Array (32 bytes) |
| 13 | + * @param chainId - The chain ID to get the correct SS58 prefix |
| 14 | + * @returns The SS58-encoded address string |
| 15 | + */ |
| 16 | +export function publicKeyToAddress(publicKey: Uint8Array, chainId: string = "polkadot"): string { |
| 17 | + const chain = getChainById(chainId, getAllSupportedChains()) |
| 18 | + return ss58.codec(chain.prefix).encode(publicKey) |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Derive and convert address from mini secret |
| 23 | + * |
| 24 | + * @param miniSecret - The mini secret as Uint8Array (32 bytes) |
| 25 | + * @param keyType - The cryptographic key type ("Sr25519" or "Ed25519") |
| 26 | + * @param derivationPath - The BIP44 derivation path (e.g., "//0", "//hard/soft") |
| 27 | + * @param chainId - The target chain ID for address encoding (default: "polkadot") |
| 28 | + * @returns The SS58-encoded address string for the specified chain |
| 29 | + * |
| 30 | + */ |
| 31 | +export function deriveAndConvertAddress( |
| 32 | + miniSecret: Uint8Array, |
| 33 | + keyType: "Sr25519" | "Ed25519", |
| 34 | + derivationPath: string, |
| 35 | + chainId: string = "polkadot" |
| 36 | +): string { |
| 37 | + const keypair = getKeypair(miniSecret, keyType, derivationPath) |
| 38 | + return publicKeyToAddress(keypair.publicKey, chainId) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Generate mini secret from agent config |
| 43 | + * @param config - The agent configuration |
| 44 | + * @returns The mini secret as Uint8Array |
| 45 | + */ |
| 46 | +export function generateMiniSecret(config: AgentConfig): Uint8Array { |
| 47 | + if (!config.mnemonic && !config.privateKey) { |
| 48 | + throw new Error("Missing mnemonic phrase or privateKey") |
| 49 | + } |
| 50 | + |
| 51 | + if (config.mnemonic && config.privateKey) { |
| 52 | + throw new Error("Cannot provide both mnemonic phrase and privateKey") |
| 53 | + } |
| 54 | + |
| 55 | + if (config.mnemonic) { |
| 56 | + const entropy = mnemonicToEntropy(config.mnemonic) |
| 57 | + return entropyToMiniSecret(entropy) |
| 58 | + } else if (config.privateKey) { |
| 59 | + const privateKeyHex = config.privateKey.startsWith("0x") |
| 60 | + ? config.privateKey.slice(2) |
| 61 | + : config.privateKey |
| 62 | + |
| 63 | + return new Uint8Array(privateKeyHex.match(/.{1,2}/g)!.map(byte => parseInt(byte, 16))) |
| 64 | + } else { |
| 65 | + throw new Error("No valid wallet source found") |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Get keypair from mini secret and derivation path |
| 71 | + * @param miniSecret - The mini secret |
| 72 | + * @param keyType - The key type |
| 73 | + * @param derivationPath - The derivation path |
| 74 | + * @returns The derived keypair |
| 75 | + */ |
| 76 | +export function getKeypair( |
| 77 | + miniSecret: Uint8Array, |
| 78 | + keyType: "Sr25519" | "Ed25519", |
| 79 | + derivationPath: string = "" |
| 80 | +) { |
| 81 | + const derive = |
| 82 | + keyType === "Sr25519" ? sr25519CreateDerive(miniSecret) : ed25519CreateDerive(miniSecret) |
| 83 | + |
| 84 | + return derive(derivationPath) |
| 85 | +} |
| 86 | + |
| 87 | +export function getSigner( |
| 88 | + miniSecret: Uint8Array, |
| 89 | + keyType: "Sr25519" | "Ed25519", |
| 90 | + derivationPath: string = "" |
| 91 | +): PolkadotSigner { |
| 92 | + if (keyType === "Sr25519") { |
| 93 | + const signer = getPolkadotSigner( |
| 94 | + getKeypair(miniSecret, keyType, derivationPath).publicKey, |
| 95 | + keyType, |
| 96 | + input => getKeypair(miniSecret, keyType, derivationPath).sign(input) |
| 97 | + ) |
| 98 | + |
| 99 | + return signer |
| 100 | + } else { |
| 101 | + const signer = getPolkadotSigner( |
| 102 | + getKeypair(miniSecret, keyType, derivationPath).publicKey, |
| 103 | + keyType, |
| 104 | + input => getKeypair(miniSecret, keyType, derivationPath).sign(input) |
| 105 | + ) |
| 106 | + return signer |
| 107 | + } |
| 108 | +} |
0 commit comments