diff --git a/src/chains/polkadot_1/examples/account.ts b/src/chains/polkadot_1/examples/account.ts index ebb0bf94..64fce01c 100644 --- a/src/chains/polkadot_1/examples/account.ts +++ b/src/chains/polkadot_1/examples/account.ts @@ -1,9 +1,12 @@ import { Chain } from '../../../interfaces' import { ChainFactory, ChainType } from '../../../index' -import { PolkadotChainEndpoint } from '../models' +import { PolkadotChainEndpoint, PolkadotKeyPairType } from '../models' +import { ModelsCryptoAes } from '../../../models' require('dotenv').config() +const { env } = process + const westendEndpoints: PolkadotChainEndpoint[] = [ { url: 'wss://westend-rpc.polkadot.io', @@ -11,7 +14,12 @@ const westendEndpoints: PolkadotChainEndpoint[] = [ ] const createAccountOptions = { - // ... + newKeysOptions: { + password: '2233', + keypairType: PolkadotKeyPairType.Ecdsa, + encryptionOptions: ModelsCryptoAes.AesEncryptedDataStringBrand, + salt: env.EOS_KYLIN_PK_SALT_V0, + }, } async function createAccount(paraChain: Chain) { @@ -21,6 +29,14 @@ async function createAccount(paraChain: Chain) { await createdAccount.generateKeysIfNeeded() console.log('generatedKeys:', createdAccount.generatedKeys) console.log('address:', createdAccount.accountName) + } catch (error) { + console.log(error) + } +} + +async function newAccount(paraChain: Chain) { + try { + await paraChain.connect() const account = await paraChain.new.Account('5FkJuxShVBRJRirM3t3k5Y8XyDaxMi1c8hLdBsH2qeAYqAzF') console.log('account', account) } catch (error) { @@ -31,11 +47,11 @@ async function createAccount(paraChain: Chain) { async function run() { try { const paraChainA = new ChainFactory().create(ChainType.PolkadotV1, westendEndpoints) - const paraChainB = new ChainFactory().create(ChainType.PolkadotV1, westendEndpoints) - const accountA = createAccount(paraChainA) - console.log('account', accountA) - const accountB = createAccount(paraChainB) - console.log('account', accountB) + // const paraChainB = new ChainFactory().create(ChainType.PolkadotV1, westendEndpoints) + await createAccount(paraChainA) + await newAccount(paraChainA) + // const accountB = createAccount(paraChainB) + // console.log('account', accountB) } catch (error) { console.log(error) } diff --git a/src/chains/polkadot_1/models/cryptoModels.ts b/src/chains/polkadot_1/models/cryptoModels.ts index 36054975..a95dffd5 100644 --- a/src/chains/polkadot_1/models/cryptoModels.ts +++ b/src/chains/polkadot_1/models/cryptoModels.ts @@ -9,9 +9,9 @@ export enum PolkadotKeyPairType { } export type PolkadotNewKeysOptions = { - keyPairType?: PolkadotKeyPairType - phrase?: string - derivationPath?: string + password: string + keypairType: PolkadotKeyPairType + encryptionOptions: PolkadotEncryptionOptions } /** an address string - formatted correctly for polkadot */ diff --git a/src/chains/polkadot_1/polkadotAccount.ts b/src/chains/polkadot_1/polkadotAccount.ts index 159451a1..ded7890f 100644 --- a/src/chains/polkadot_1/polkadotAccount.ts +++ b/src/chains/polkadot_1/polkadotAccount.ts @@ -1,4 +1,4 @@ -import { notSupported } from '../../helpers' +import { notImplemented } from '../../helpers' import { throwNewError } from '../../errors' import { Account } from '../../interfaces' import { PolkadotChainState } from './polkadotChainState' @@ -18,13 +18,15 @@ export class PolkadotAccount implements Account { /** Whether the account is currently unused and can be reused - not supported in Polkadot */ get canBeRecycled(): boolean { - return notSupported('PolkadotAccount.canBeRecycled') + return notImplemented() + // return notSupported('PolkadotAccount.canBeRecycled') } /** Polkadot address */ get name(): any { - this.assertHasAddress() - return this._address + return notImplemented() + // this.assertHasAddress() + // return this._address } /** Public Key(s) associated with the account */ diff --git a/src/chains/polkadot_1/polkadotCreateAccount.ts b/src/chains/polkadot_1/polkadotCreateAccount.ts index 09a544e8..5918de19 100644 --- a/src/chains/polkadot_1/polkadotCreateAccount.ts +++ b/src/chains/polkadot_1/polkadotCreateAccount.ts @@ -6,12 +6,13 @@ import { PolkadotChainState } from './polkadotChainState' // getKeypairFromPhrase, // getPolkadotAddressFromPublicKey, // } from './polkadotCrypto' -import { isValidPolkadotPublicKey } from './helpers' -import { notImplemented, notSupported } from '../../helpers' +import { getCurveFromKeyType, isValidPolkadotPublicKey } from './helpers' +import { isNullOrEmpty, notImplemented, notSupported } from '../../helpers' import { CreateAccount } from '../../interfaces' import { throwNewError } from '../../errors' -import { PolkadotAddress, PolkadotKeypair } from './models' +import { PolkadotAddress, PolkadotKeypair, PolkadotPublicKey } from './models' import { CryptoCurve } from '../../models' +import { generateNewAccountKeysAndEncryptPrivateKeys } from './polkadotCrypto' // import { DEFAULT_POLKADOT_KEY_PAIR_TYPE } from './polkadotConstants' /** Helper class to compose a transaction for creating a new chain account @@ -96,7 +97,9 @@ export class PolkadotCreateAccount implements CreateAccount { * Recycling is not supported on Polkadot. */ async determineNewAccountName(accountName: PolkadotAddress): Promise { - return { alreadyExists: false, newAccountName: accountName, canRecycle: false } + notImplemented() + return null + // return { alreadyExists: false, newAccountName: accountName, canRecycle: false } } /** Returns the Polkadot Address as a Polkadot Account name for the public key provided in options @@ -112,37 +115,36 @@ export class PolkadotCreateAccount implements CreateAccount { /** Returns a string of the Polkadot Address for the public key provide in options - OR generates a new mnemonic(phrase)/private/public/address */ async generateAccountNameString(): Promise { - await this.generateKeysIfNeeded() - return this.accountName as string + notImplemented() + return null + // await this.generateKeysIfNeeded() + // return this.accountName as string } /** Checks create options - if publicKeys are missing, * autogenerate the public and private key pair and add them to options */ async generateKeysIfNeeded() { - // let publicKey: PolkadotPublicKey - // this.assertValidOptionPublicKeys() - // publicKey = this?._options?.publicKey - // if (!publicKey) { - // await this.generateAccountKeys() - // } - // this._accountName = getPolkadotAddressFromPublicKey(this._generatedKeypair.publicKey) - // this._accountType = this._options.newKeysOptions.keyPairType - } - - // private async generateAccountKeys(): Promise { - // const { newKeysOptions } = this._options - // const { keyPairType, phrase: overridePhrase, derivationPath } = newKeysOptions || {} - // const overrideType = keyPairType || DEFAULT_POLKADOT_KEY_PAIR_TYPE - - // // this._generatedKeypair = getKeypairFromPhrase(overridePhrase, overrideType) - // this._generatedKeypair = await generateKeyPair(keyPairType, phrase, derivationPath) - // this._options.publicKey = this._generatedKeypair.publicKey - // this._options.newKeysOptions = { - // phrase: overridePhrase, - // keyPairType: overrideType, - // } - // } + let publicKey: PolkadotPublicKey + this.assertValidOptionPublicKeys() + this.assertValidOptionNewKeys() + publicKey = this._options?.publicKey + if (!publicKey) { + await this.generateAccountKeys() + publicKey = this._generatedKeypair?.publicKey + } + const curve = getCurveFromKeyType(this._generatedKeypair?.type) + this._accountType = curve + // TODO: Determine whether polkadot has account name in production + // this._accountName = await getPolkadotAddressFromPublicKey(publicKey) + } + + private async generateAccountKeys(): Promise { + const { newKeysOptions } = this._options || {} + const { password, keypairType, encryptionOptions } = newKeysOptions || {} + this._generatedKeypair = await generateNewAccountKeysAndEncryptPrivateKeys(password, keypairType, encryptionOptions) + this._options.publicKey = this._generatedKeypair?.publicKey + } private assertValidOptionPublicKeys() { const { publicKey } = this._options @@ -150,4 +152,12 @@ export class PolkadotCreateAccount implements CreateAccount { throwNewError('Invalid option - provided publicKey isnt valid') } } + + private assertValidOptionNewKeys() { + const { newKeysOptions } = this._options + const { password } = newKeysOptions || {} + if (isNullOrEmpty(password)) { + throwNewError('Invalid Option - You must provide a password to generate new keys') + } + } }