diff --git a/src/chains/polkadot_1/ChainPolkadotV1.ts b/src/chains/polkadot_1/ChainPolkadotV1.ts index 611b77a7..2f2bf8f5 100644 --- a/src/chains/polkadot_1/ChainPolkadotV1.ts +++ b/src/chains/polkadot_1/ChainPolkadotV1.ts @@ -1,4 +1,4 @@ -import { ChainActionType, ChainInfo, ChainType, CryptoCurve, ChainEntityName, ChainDate } from '../../models' +import { ChainActionType, ChainType, CryptoCurve, ChainEntityName, ChainDate } from '../../models' import { ChainError } from '../../errors' import { Chain } from '../../interfaces' import { @@ -13,9 +13,10 @@ import { import { PolkadotChainState } from './polkadotChainState' import { notImplemented } from '../../helpers' import { PolkadotChainActionType } from './models/chainActionType' -import { PolkadotTransactionAction } from './models/transactionModels' +import { PolkadotTransactionAction, PolkadotTransactionOptions } from './models/transactionModels' import { PolkadotAccount } from './polkadotAccount' import { PolkadotTransaction } from './polkadotTransaction' +import { PolkadotCrypto } from './polkadotCrypto' import * as polkadotCrypto from './polkadotCrypto' import * as ethcrypto from '../ethereum_1/ethCrypto' @@ -136,15 +137,20 @@ class ChainPolkadotV1 implements Chain { return new PolkadotCreateAccount(this._chainState, options) } - private newTransaction = (options?: any): PolkadotTransaction => { + private newTransaction = (options?: PolkadotTransactionOptions): PolkadotTransaction => { this.assertIsConnected() return new PolkadotTransaction(this._chainState, options) } + private newCrypto = (options?: any): PolkadotCrypto => { + return new PolkadotCrypto(this._chainState, options) + } + public new = { Account: this.newAccount, CreateAccount: this.newCreateAccount, Transaction: this.newTransaction, + Crypto: this.newCrypto, } // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/src/chains/polkadot_1/examples/crypto.ts b/src/chains/polkadot_1/examples/crypto.ts index e40edacc..339bc76b 100644 --- a/src/chains/polkadot_1/examples/crypto.ts +++ b/src/chains/polkadot_1/examples/crypto.ts @@ -38,16 +38,16 @@ async function run() { }) console.log('decrypted payload:', decryptedPayload) - const publicKey1 = toPolkadotPublicKey( - '0x2e438c99bd7ded27ed921919e1d5ee1d9b1528bb8a2f6c974362ad1a9ba7a6f59a452a0e4dfbc178ab5c5c090506bd7f0a6659fd3cf0cc769d6c17216d414163', - ) + // const publicKey1 = toPolkadotPublicKey( + // '0x2e438c99bd7ded27ed921919e1d5ee1d9b1528bb8a2f6c974362ad1a9ba7a6f59a452a0e4dfbc178ab5c5c090506bd7f0a6659fd3cf0cc769d6c17216d414163', + // ) - const privateKey1 = toPolkadotPrivateKey('0x7b0c4bdbc24fd7b6045e9001dbe93f1e46478dedcfcefbc42180ac79fd08ce28') + // const privateKey1 = toPolkadotPrivateKey('0x7b0c4bdbc24fd7b6045e9001dbe93f1e46478dedcfcefbc42180ac79fd08ce28') - const encrypted2 = await para.encryptWithPublicKey('text to encrypt 2', publicKey1) - console.log('encrypted text 2:', encrypted2) - const decrypted2 = await para.decryptWithPrivateKey(encrypted2, privateKey1) - console.log('decrypted text 2:', decrypted2) + // const encrypted2 = await para.encryptWithPublicKey('text to encrypt 2', publicKey1) + // console.log('encrypted text 2:', encrypted2) + // const decrypted2 = await para.decryptWithPrivateKey(encrypted2, privateKey1) + // console.log('decrypted text 2:', decrypted2) } ;(async () => { diff --git a/src/chains/polkadot_1/polkadotCrypto.ts b/src/chains/polkadot_1/polkadotCrypto.ts index 62f908b4..b79faf82 100644 --- a/src/chains/polkadot_1/polkadotCrypto.ts +++ b/src/chains/polkadot_1/polkadotCrypto.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/indent */ +/* eslint-disable @typescript-eslint/no-unused-vars */ import { keyExtractSuri, keyFromPath, @@ -22,11 +24,12 @@ import { } from './models' import { CryptoCurve, PublicKey } from '../../models' import { AesCrypto, Asymmetric, Ed25519Crypto } from '../../crypto' -import { removeHexPrefix, byteArrayToHexString, hexStringToByteArray, notSupported, isInEnum } from '../../helpers' +import { removeHexPrefix, byteArrayToHexString, hexStringToByteArray, notSupported } from '../../helpers' import { ensureEncryptedValueIsObject } from '../../crypto/genericCryptoHelpers' // import * as AsymmetricHelpers from '../../crypto/asymmetricHelpers' import { throwNewError } from '../../errors' import { getCurveFromKeyType, toPolkadotPrivateKey, toPolkadotPublicKey, toSymEncryptedDataString } from './helpers' +import { PolkadotChainState } from './polkadotChainState' // TODO - should change depending on curve const enum POLKADOT_ASYMMETRIC_SCHEME_NAME { @@ -34,148 +37,248 @@ const enum POLKADOT_ASYMMETRIC_SCHEME_NAME { Secp256k1 = 'asym.chainjs.secp256k1.polkadot', } -/** returns a keypair for a specific curve */ -export function generateKeypairFromSeed(seed: Uint8Array, curve: CryptoCurve): Keypair { - if (curve === CryptoCurve.Secp256k1) return secp256k1KeypairFromSeed(seed) as Keypair - if (curve === CryptoCurve.Ed25519) return naclKeypairFromSeed(seed) as Keypair - if (curve === CryptoCurve.Sr25519) return schnorrkelKeypairFromSeed(seed) as Keypair - throwNewError(`Curve type not supported: ${curve}`) - return null -} +interface Crypto {} -export function generateNewAccountPhrase(): string { - const mnemonic = mnemonicGenerate() - return mnemonic -} +export class PolkadotCrypto implements Crypto { + private _chainState: PolkadotChainState -export function getKeypairFromPhrase(mnemonic: string, curve: CryptoCurve): Keypair { - const seed = mnemonicToMiniSecret(mnemonic) - const keyPair = generateKeypairFromSeed(seed, curve) - return keyPair -} + constructor(chainState: PolkadotChainState, options?: any) { + this._chainState = chainState + this.assertValidOptions(options) + this.applyOptions(options) + } -/** get uncompressed public key from Ethereum key */ -export function uncompressEthereumPublicKey(publicKey: PolkadotPublicKey): string { - // if already decompressed an not has trailing 04 - const cleanedPublicKey = removeHexPrefix(publicKey) - const testBuffer = Buffer.from(cleanedPublicKey, 'hex') - const prefixedPublicKey = testBuffer.length === 64 ? `04${cleanedPublicKey}` : cleanedPublicKey - const uncompressedPublicKey = byteArrayToHexString( - secp256k1.publicKeyConvert(hexStringToByteArray(prefixedPublicKey), false), - ) - return uncompressedPublicKey -} + /** Throws if from is not null or empty polkadot argument */ + private assertValidOptions(options: any) {} + + /** apply options and/or use defaults */ + private applyOptions(options: any) {} -/** Encrypts a string using a password and optional salt */ -export function encryptWithPassword( - unencrypted: string, - password: string, - options: PolkadotEncryptionOptions, - keypairType?: PolkadotKeyPairType, // TODO: keypairType should be required -): AesCrypto.AesEncryptedDataString | Ed25519Crypto.Ed25519EncryptedDataString { - // TODO: Define Src25519 curve - const curve = getCurveFromKeyType(keypairType) - if (curve === CryptoCurve.Ed25519) { - const passwordKey = Ed25519Crypto.calculatePasswordByteArray(password, options) - const encrypted = Ed25519Crypto.encrypt(unencrypted, passwordKey) - return toSymEncryptedDataString(encrypted, keypairType) + /** returns a keypair for a specific curve */ + private generateKeypairFromSeed(seed: Uint8Array, curve: CryptoCurve): Keypair { + if (curve === CryptoCurve.Secp256k1) return secp256k1KeypairFromSeed(seed) as Keypair + if (curve === CryptoCurve.Ed25519) return naclKeypairFromSeed(seed) as Keypair + if (curve === CryptoCurve.Sr25519) return schnorrkelKeypairFromSeed(seed) as Keypair + throwNewError(`Curve type not supported: ${curve}`) + return null } - if (curve === CryptoCurve.Secp256k1) return AesCrypto.encryptWithPassword(unencrypted, password, options) - // if no curve, throw an error - curve not supported - throw new Error(`Curve not supported ${curve}`) -} -/** Decrypts the encrypted value using a password, and optional salt using secp256k1, and nacl - * The encrypted value is either a stringified JSON object or a JSON object */ -export function decryptWithPassword( - encrypted: AesCrypto.AesEncryptedDataString | Ed25519Crypto.Ed25519EncryptedDataString | any, - password: string, - options: PolkadotEncryptionOptions, - keypairType?: PolkadotKeyPairType, // TODO: keypairType should be required -): string { - // TODO: Define Src25519 curve - const curve = getCurveFromKeyType(keypairType) - if (curve === CryptoCurve.Ed25519) { - const passwordKey = Ed25519Crypto.calculatePasswordByteArray(password, options) - const decrypted = Ed25519Crypto.decrypt(encrypted, passwordKey) - return decrypted + private generateNewAccountPhrase(): string { + const mnemonic = mnemonicGenerate() + return mnemonic } - if (curve === CryptoCurve.Secp256k1) return AesCrypto.decryptWithPassword(encrypted, password, options) - // if no curve, throw an error - curve not supported - throw new Error(`Curve not supported ${curve}`) -} -/** uncompress public key based on keypairType */ -export function uncompressPublicKey( - publicKey: PolkadotPublicKey, - keypairType: PolkadotKeyPairType, -): { curveType: Asymmetric.EciesCurveType; publicKeyUncompressed: PublicKey; scheme: POLKADOT_ASYMMETRIC_SCHEME_NAME } { - let scheme: POLKADOT_ASYMMETRIC_SCHEME_NAME - let curveType: Asymmetric.EciesCurveType - let publicKeyUncompressed - if (keypairType === PolkadotKeyPairType.Ecdsa) { - // TODO: confirm that ecdsa is uncompressed, might be same as Ethereum - publicKeyUncompressed = publicKey - } else if (keypairType === PolkadotKeyPairType.Ethereum) { - publicKeyUncompressed = uncompressEthereumPublicKey(publicKey) - curveType = Asymmetric.EciesCurveType.Secp256k1 - scheme = POLKADOT_ASYMMETRIC_SCHEME_NAME.Secp256k1 - } else if (keypairType === PolkadotKeyPairType.Ed25519) { - publicKeyUncompressed = publicKey - curveType = Asymmetric.EciesCurveType.Ed25519 - scheme = POLKADOT_ASYMMETRIC_SCHEME_NAME.Ed25519 - } else if (keypairType === PolkadotKeyPairType.Sr25519) { - // TODO: add - } else { - notSupported(`uncompressPublicKey keypairType: ${keypairType}`) + private getKeypairFromPhrase(mnemonic: string, curve: CryptoCurve): Keypair { + const seed = mnemonicToMiniSecret(mnemonic) + const keyPair = this.generateKeypairFromSeed(seed, curve) + return keyPair } - return { curveType, publicKeyUncompressed, scheme } -} -/** Encrypts a string using a public key into a stringified JSON object - * The encrypted result can be decrypted with the matching private key */ -export async function encryptWithPublicKey( - unencrypted: string, - publicKey: PolkadotPublicKey, - options: Asymmetric.EciesOptions, - keypairType?: PolkadotKeyPairType, // TODO: keypairType should be required -): Promise { - const { curveType, publicKeyUncompressed, scheme } = uncompressPublicKey(publicKey, keypairType) - const useOptions = { - ...options, - curveType, - scheme, + /** get uncompressed public key from Ethereum key */ + public uncompressEthereumPublicKey(publicKey: PolkadotPublicKey): string { + // if already decompressed an not has trailing 04 + const cleanedPublicKey = removeHexPrefix(publicKey) + const testBuffer = Buffer.from(cleanedPublicKey, 'hex') + const prefixedPublicKey = testBuffer.length === 64 ? `04${cleanedPublicKey}` : cleanedPublicKey + const uncompressedPublicKey = byteArrayToHexString( + secp256k1.publicKeyConvert(hexStringToByteArray(prefixedPublicKey), false), + ) + return uncompressedPublicKey } - const response = Asymmetric.encryptWithPublicKey(publicKeyUncompressed, unencrypted, useOptions) - return Asymmetric.toAsymEncryptedDataString(JSON.stringify(response)) -} -// TODO: Refactor - Tray - reuse functions across chains + /** Encrypts a string using a password and optional salt */ + public encryptWithPassword( + unencrypted: string, + password: string, + options: PolkadotEncryptionOptions, + keypairType: PolkadotKeyPairType, + ): AesCrypto.AesEncryptedDataString | Ed25519Crypto.Ed25519EncryptedDataString { + // TODO: Define Src25519 curve + const curve = getCurveFromKeyType(keypairType) + if (curve === CryptoCurve.Ed25519) { + const passwordKey = Ed25519Crypto.calculatePasswordByteArray(password, options) + const encrypted = Ed25519Crypto.encrypt(unencrypted, passwordKey) + return toSymEncryptedDataString(encrypted, keypairType) + } + if (curve === CryptoCurve.Secp256k1) return AesCrypto.encryptWithPassword(unencrypted, password, options) + // if no curve, throw an error - curve not supported + throw new Error(`Curve not supported ${curve}`) + } -/** Decrypts the encrypted value using a private key - * The encrypted value is a stringified JSON object - * ... and must have been encrypted with the public key that matches the private ley provided */ -export async function decryptWithPrivateKey( - encrypted: Asymmetric.AsymmetricEncryptedDataString | Asymmetric.AsymmetricEncryptedData, - privateKey: PolkadotPrivateKey, - options: Asymmetric.EciesOptions, - keypairType?: PolkadotKeyPairType, // TODO: keypairType should be required -): Promise { - const curve = getCurveFromKeyType(keypairType) // TODO: Should be keypairtype not curve - let useOptions = { ...options } - let privateKeyConverted = '' - if (curve === CryptoCurve.Secp256k1) { - useOptions = { ...useOptions, curveType: Asymmetric.EciesCurveType.Secp256k1 } - privateKeyConverted = removeHexPrefix(privateKey) - } else if (curve === CryptoCurve.Ed25519) { - useOptions = { ...useOptions, curveType: Asymmetric.EciesCurveType.Ed25519 } - privateKeyConverted = privateKey.slice(0, privateKey.length / 2) - } else { - // if no curve matched, throw an error - not supported curve + /** Decrypts the encrypted value using a password, and optional salt using secp256k1, and nacl + * The encrypted value is either a stringified JSON object or a JSON object */ + public decryptWithPassword( + encrypted: AesCrypto.AesEncryptedDataString | Ed25519Crypto.Ed25519EncryptedDataString | any, + password: string, + options: PolkadotEncryptionOptions, + keypairType: PolkadotKeyPairType, + ): string { + // TODO: Define Src25519 curve + const curve = getCurveFromKeyType(keypairType) + if (curve === CryptoCurve.Ed25519) { + const passwordKey = Ed25519Crypto.calculatePasswordByteArray(password, options) + const decrypted = Ed25519Crypto.decrypt(encrypted, passwordKey) + return decrypted + } + if (curve === CryptoCurve.Secp256k1) return AesCrypto.decryptWithPassword(encrypted, password, options) + // if no curve, throw an error - curve not supported throw new Error(`Curve not supported ${curve}`) } - const encryptedObject = ensureEncryptedValueIsObject(encrypted) as Asymmetric.AsymmetricEncryptedData - return Asymmetric.decryptWithPrivateKey(encryptedObject, privateKeyConverted, useOptions) + + /** uncompress public key based on keypairType */ + private uncompressPublicKey( + publicKey: PolkadotPublicKey, + keypairType: PolkadotKeyPairType, + ): { + curveType: Asymmetric.EciesCurveType + publicKeyUncompressed: PublicKey + scheme: POLKADOT_ASYMMETRIC_SCHEME_NAME + } { + let scheme: POLKADOT_ASYMMETRIC_SCHEME_NAME + let curveType: Asymmetric.EciesCurveType + let publicKeyUncompressed + if (keypairType === PolkadotKeyPairType.Ecdsa) { + // TODO: confirm that ecdsa is uncompressed, might be same as Ethereum + publicKeyUncompressed = publicKey + } else if (keypairType === PolkadotKeyPairType.Ethereum) { + publicKeyUncompressed = this.uncompressEthereumPublicKey(publicKey) + curveType = Asymmetric.EciesCurveType.Secp256k1 + scheme = POLKADOT_ASYMMETRIC_SCHEME_NAME.Secp256k1 + } else if (keypairType === PolkadotKeyPairType.Ed25519) { + publicKeyUncompressed = publicKey + curveType = Asymmetric.EciesCurveType.Ed25519 + scheme = POLKADOT_ASYMMETRIC_SCHEME_NAME.Ed25519 + } else if (keypairType === PolkadotKeyPairType.Sr25519) { + // TODO: add + } else { + notSupported(`uncompressPublicKey keypairType: ${keypairType}`) + } + return { curveType, publicKeyUncompressed, scheme } + } + + /** Encrypts a string using a public key into a stringified JSON object + * The encrypted result can be decrypted with the matching private key */ + public encryptWithPublicKey( + unencrypted: string, + publicKey: PolkadotPublicKey, + options: Asymmetric.EciesOptions, + keypairType: PolkadotKeyPairType, + ): Asymmetric.AsymmetricEncryptedDataString { + const { curveType, publicKeyUncompressed, scheme } = this.uncompressPublicKey(publicKey, keypairType) + const useOptions = { + ...options, + curveType, + scheme, + } + const response = Asymmetric.encryptWithPublicKey(publicKeyUncompressed, unencrypted, useOptions) + return Asymmetric.toAsymEncryptedDataString(JSON.stringify(response)) + } + + // TODO: Refactor - Tray - reuse functions across chains + /** Decrypts the encrypted value using a private key + * The encrypted value is a stringified JSON object + * ... and must have been encrypted with the public key that matches the private ley provided */ + public decryptWithPrivateKey( + encrypted: Asymmetric.AsymmetricEncryptedDataString | Asymmetric.AsymmetricEncryptedData, + privateKey: PolkadotPrivateKey, + options: Asymmetric.EciesOptions, + keypairType: PolkadotKeyPairType, + ): string { + const curve = getCurveFromKeyType(keypairType) // TODO: Should be keypairtype not curve + let useOptions = { ...options } + let privateKeyConverted = '' + if (curve === CryptoCurve.Secp256k1) { + useOptions = { ...useOptions, curveType: Asymmetric.EciesCurveType.Secp256k1 } + privateKeyConverted = removeHexPrefix(privateKey) + } else if (curve === CryptoCurve.Ed25519) { + useOptions = { ...useOptions, curveType: Asymmetric.EciesCurveType.Ed25519 } + privateKeyConverted = privateKey.slice(0, privateKey.length / 2) + } else { + // if no curve matched, throw an error - not supported curve + throw new Error(`Curve not supported ${curve}`) + } + const encryptedObject = ensureEncryptedValueIsObject(encrypted) as Asymmetric.AsymmetricEncryptedData + return Asymmetric.decryptWithPrivateKey(encryptedObject, privateKeyConverted, useOptions) + } + + /** Derive a seed from a mnemoic (and optional derivation path) */ + private generateSeedFromMnemonic( + keypairType: PolkadotKeyPairType, + mnemonic: string, + derivationPath?: string, + ): { seed: Uint8Array; path: DeriveJunction[] } { + const suri = derivationPath !== undefined ? `${mnemonic}//${derivationPath}` : mnemonic + const { password, path, phrase } = keyExtractSuri(suri) + let seed: Uint8Array + if (isHex(phrase, 256)) { + seed = hexStringToByteArray(phrase) + } else { + const str = phrase as string + const parts = str.split(' ') + if ([12, 15, 18, 21, 24].includes(parts.length)) { + seed = + keypairType === PolkadotKeyPairType.Ethereum + ? mnemonicToLegacySeed(phrase) + : mnemonicToMiniSecret(phrase, password) + } else { + throw new Error('Specified phrase is not a valild mnemonic and is invalid as a raw seed at > 32 bytes') + } + } + return { seed, path } + } + + /** Generates and returns a new public/private key pair + * Supports optional key gen from mnemonic phase + * Note: Reference - createFromUri from @polkadot/keyring + * https://github.com/polkadot-js/common/blob/master/packages/keyring/src/keyring.ts#L197 + */ + public async generateKeyPair( + keypairType?: PolkadotKeyPairType, + mnemonic?: string, + derivationPath?: string, + ): Promise { + const curve = getCurveFromKeyType(keypairType) + const overrideMnemonic = mnemonic || this.generateNewAccountPhrase() + const { seed, path } = this.generateSeedFromMnemonic(keypairType, overrideMnemonic, derivationPath) + const derivedKeypair = keyFromPath(this.generateKeypairFromSeed(seed, curve), path, keypairType) + const keypair: PolkadotKeypair = { + type: keypairType, + publicKey: toPolkadotPublicKey(byteArrayToHexString(derivedKeypair.publicKey)), + privateKey: toPolkadotPrivateKey(byteArrayToHexString(derivedKeypair.secretKey)), + } + return keypair + } + + /** Adds privateKeyEncrypted if missing by encrypting privateKey (using password) */ + public encryptAccountPrivateKeysIfNeeded( + keys: PolkadotKeypair, + password: string, + options: PolkadotEncryptionOptions, + ): PolkadotKeypair { + const privateKeyEncrypted = keys.privateKeyEncrypted + ? keys.privateKeyEncrypted + : this.encryptWithPassword(keys.privateKey, password, options, keys.type) + const encryptedKeys: PolkadotKeypair = { + type: keys.type, + publicKey: keys.publicKey, + privateKey: keys.privateKey, + privateKeyEncrypted, + } + return encryptedKeys + } + + /** Generates new public and private key pair + * Encrypts the private key using password and optional salt + */ + public async generateNewAccountKeysAndEncryptPrivateKeys( + password: string, + keypairType: PolkadotKeyPairType, + options: PolkadotEncryptionOptions, + ): Promise { + const keys = await this.generateKeyPair(keypairType) + const encryptedKeys = this.encryptAccountPrivateKeysIfNeeded(keys, password, options) + return encryptedKeys + } } /** Encrypts a string using multiple assymmetric encryptions with multiple public keys - one after the other @@ -221,85 +324,6 @@ export async function decryptWithPrivateKey( // return toEthereumSignature(ecsign(dataBuffer, keyBuffer)) // } -/** Derive a seed from a mnemoic (and optional derivation path) */ -function generateSeedFromMnemonic( - keypairType: PolkadotKeyPairType, - mnemonic: string, - derivationPath?: string, -): { seed: Uint8Array; path: DeriveJunction[] } { - const suri = derivationPath !== undefined ? `${mnemonic}//${derivationPath}` : mnemonic - const { password, path, phrase } = keyExtractSuri(suri) - let seed: Uint8Array - if (isHex(phrase, 256)) { - seed = hexStringToByteArray(phrase) - } else { - const str = phrase as string - const parts = str.split(' ') - if ([12, 15, 18, 21, 24].includes(parts.length)) { - seed = - keypairType === PolkadotKeyPairType.Ethereum - ? mnemonicToLegacySeed(phrase) - : mnemonicToMiniSecret(phrase, password) - } else { - throw new Error('Specified phrase is not a valild mnemonic and is invalid as a raw seed at > 32 bytes') - } - } - return { seed, path } -} - -/** Generates and returns a new public/private key pair - * Supports optional key gen from mnemonic phase - * Note: Reference - createFromUri from @polkadot/keyring - * https://github.com/polkadot-js/common/blob/master/packages/keyring/src/keyring.ts#L197 - */ -export async function generateKeyPair( - keypairType?: PolkadotKeyPairType, - mnemonic?: string, - derivationPath?: string, -): Promise { - const curve = getCurveFromKeyType(keypairType) - const overrideMnemonic = mnemonic || generateNewAccountPhrase() - const { seed, path } = generateSeedFromMnemonic(keypairType, overrideMnemonic, derivationPath) - const derivedKeypair = keyFromPath(generateKeypairFromSeed(seed, curve), path, keypairType) - const keypair: PolkadotKeypair = { - type: keypairType, - publicKey: toPolkadotPublicKey(byteArrayToHexString(derivedKeypair.publicKey)), - privateKey: toPolkadotPrivateKey(byteArrayToHexString(derivedKeypair.secretKey)), - } - return keypair -} - -/** Adds privateKeyEncrypted if missing by encrypting privateKey (using password) */ -function encryptAccountPrivateKeysIfNeeded( - keys: PolkadotKeypair, - password: string, - options: PolkadotEncryptionOptions, -): PolkadotKeypair { - const privateKeyEncrypted = keys.privateKeyEncrypted - ? keys.privateKeyEncrypted - : encryptWithPassword(keys.privateKey, password, options, keys.type) - const encryptedKeys: PolkadotKeypair = { - type: keys.type, - publicKey: keys.publicKey, - privateKey: keys.privateKey, - privateKeyEncrypted, - } - return encryptedKeys -} - -/** Generates new public and private key pair - * Encrypts the private key using password and optional salt - */ -export async function generateNewAccountKeysAndEncryptPrivateKeys( - password: string, - keypairType: PolkadotKeyPairType, - options: PolkadotEncryptionOptions, -): Promise { - const keys = await generateKeyPair(keypairType) - const encryptedKeys = encryptAccountPrivateKeysIfNeeded(keys, password, options) - return encryptedKeys -} - // export function determineCurveFromAddress() {} // export function determineCurveFromKeyPair() {