Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/chains/polkadot_1/examples/account.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
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',
},
]

const createAccountOptions = {
// ...
newKeysOptions: {
password: '2233',
keypairType: PolkadotKeyPairType.Ecdsa,
encryptionOptions: ModelsCryptoAes.AesEncryptedDataStringBrand,
salt: env.EOS_KYLIN_PK_SALT_V0,
},
}

async function createAccount(paraChain: Chain) {
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/chains/polkadot_1/models/cryptoModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
10 changes: 6 additions & 4 deletions src/chains/polkadot_1/polkadotAccount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { notSupported } from '../../helpers'
import { notImplemented } from '../../helpers'
import { throwNewError } from '../../errors'
import { Account } from '../../interfaces'
import { PolkadotChainState } from './polkadotChainState'
Expand All @@ -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 */
Expand Down
68 changes: 39 additions & 29 deletions src/chains/polkadot_1/polkadotCreateAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,7 +97,9 @@ export class PolkadotCreateAccount implements CreateAccount {
* Recycling is not supported on Polkadot.
*/
async determineNewAccountName(accountName: PolkadotAddress): Promise<any> {
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
Expand All @@ -112,42 +115,49 @@ 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<string> {
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<void> {
// 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<void> {
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
if (publicKey && isValidPolkadotPublicKey(publicKey)) {
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')
}
}
}