Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename members #15

Merged
merged 6 commits into from
Jan 20, 2020
Merged
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
52 changes: 29 additions & 23 deletions examples/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-console */
import { Chain, ChainFactory, ChainType } from '../src/index'
import { ChainEndpoint, ChainSettings, AccountType } from '../src/models'
import { EosPrivateKey, EosAccountType, EosActionStruct } from '../src/chains/eos_1_8/models'
import { ChainEndpoint, ChainSettings, NewAccountType } from '../src/models'
import { EosPrivateKey, EosNewAccountType, EosActionStruct } from '../src/chains/eos_1_8/models'
import { EosAccount } from '../src/chains/eos_1_8/eosAccount'
import { ChainEosV18 } from '../src/chains/eos_1_8/ChainEosV18'
import { DeletePermissionsParams, LinkPermissionsParams } from '../src/chains/eos_1_8/eosPermissionsHelper'
Expand Down Expand Up @@ -65,8 +65,8 @@ export const createAccountOptions_createBridge = {
creatorAccountName: toEosEntityName('oreidfunding'),
creatorPermission: toEosEntityName('active'),
newKeysOptions: {
newKeysPassword: '2233',
newKeysSalt: env.EOS_KYLIN_PK_SALT_V0, // kylin
password: '2233',
salt: env.EOS_KYLIN_PK_SALT_V0, // kylin
},
createEscrowOptions: {
contractName: toEosEntityName('createbridge'),
Expand All @@ -83,8 +83,8 @@ export const createAccountOptions_OreNative = {
owner: 'EOS7rNR9AhgcqkmMAoUSHrjTXgxM4PnpGYDXhS3B4UW3jjZgBATXL',
},
newKeysOptions: {
newKeysPassword: '2233',
newKeysSalt: env.$ORE_TESTNET_PK_SALT_V0, // ore staging
password: '2233',
salt: env.$ORE_TESTNET_PK_SALT_V0, // ore staging
},
}

Expand All @@ -94,8 +94,8 @@ export const createAccountOptions_virtualNested = {
creatorAccountName: 'moonlightore',
creatorPermission: 'active',
newKeysOptions: {
newKeysPassword: '2233',
newKeysSalt: env.EOS_KYLIN_PK_SALT_V0, // kylin
password: '2233',
salt: env.EOS_KYLIN_PK_SALT_V0, // kylin
},
createEscrowOptions: {
contractName: 'createbridge',
Expand Down Expand Up @@ -123,8 +123,8 @@ export const createAccountOptions_EosNative = {
creatorAccountName: 'proppropprop',
creatorPermission: 'active',
newKeysOptions: {
newKeysPassword: '2233',
newKeysSalt: env.EOS_KYLIN_PK_SALT_V0, // kylin
password: '2233',
salt: env.EOS_KYLIN_PK_SALT_V0, // kylin
},
publicKeys: {
owner: 'EOS5TjGeH12cqxKrXExiQohiVZo8utowncv7Qg4FbFUhbwVNgUbKs',
Expand All @@ -145,8 +145,8 @@ export const createAccountOptions_OreRecycleNative = {
creatorAccountName: 'ore1qadesjxm',
creatorPermission: 'owner',
newKeysOptions: {
newKeysPassword: '2233',
newKeysSalt: env.EOS_KYLIN_PK_SALT_V0, // kylin
password: '2233',
salt: env.EOS_KYLIN_PK_SALT_V0, // kylin
},
}

Expand All @@ -159,8 +159,8 @@ export const resetPermissions = [
]

export const permissionNewKeysOptions = {
newKeysPassword: '2233',
newKeysSalt: env.EOS_KYLIN_PK_SALT_V0, // kylin
password: '2233',
salt: env.EOS_KYLIN_PK_SALT_V0, // kylin
}

export const accountNewPermissions = [
Expand Down Expand Up @@ -220,46 +220,52 @@ export const accountLinkPermissions: LinkPermissionsParams[] = [

// -----> CreateAccount - createbridge
// const createAccount = kylin.new.createAccount()
// await createAccount.composeTransaction(EosAccountType.CreateEscrow, createAccountOptions_createBridge)
// await createAccount.composeTransaction(EosNewAccountType.CreateEscrow, createAccountOptions_createBridge)
// await prepTransaction(kylin, createAccount.transaction, env.EOS_KYLIN_OREIDFUNDING_PRIVATE_KEY)
// console.log('createAccount response: ', await createAccount.transaction.send())

// -----> CreateAccount - create native kylin account
// const createAccount = kylin.new.createAccount()
// await createAccount.composeTransaction(EosAccountType.Native, createAccountOptions_EosNative)
// await createAccount.composeTransaction(EosNewAccountType.Native, createAccountOptions_EosNative)
// await prepTransaction(kylin, createAccount.transaction, env.KYLIN_proppropprop_PRIVATE_KEY)
// console.log('createAccount response: ', await createAccount.transaction.send())

// -----> CreateAccount - create native ore-staging account
// const createAccount = oreStaging.new.createAccount()
// await createAccount.composeTransaction(EosAccountType.NativeOre, createAccountOptions_OreNative)
// await createAccount.composeTransaction(EosNewAccountType.NativeOre, createAccountOptions_OreNative)
// await prepTransaction(oreStaging, createAccount.transaction, env.ORE_TESTNET_APPOREID_PRIVATE_KEY)
// console.log(JSON.stringify(createAccount.transaction.toJson()))
// console.log('createAccount response: ', await createAccount.transaction.send())

// -----> CreateAccount - create virtual nested account
// const createAccount = kylin.new.createAccount()
// await createAccount.composeTransaction(EosAccountType.VirtualNested, createAccountOptions_virtualNested)
// await createAccount.composeTransaction(EosNewAccountType.VirtualNested, createAccountOptions_virtualNested)
// await prepTransaction(kylin, createAccount.transaction, env.KYLIN_moonlightore_PRIVATE_KEY)
// console.log('createAccount response: ', await createAccount.transaction.send())

// // -----> Reset account to be recyclable
// const recycleAccount = (await kylin.new.account('ore1qadesjxm')) as EosAccount

// console.log('ore1qadesjxm account permissions:', recycleAccount.permissions)
// const { generatedKeys, actions } = await recycleAccount.composeAddPermissionsActions(
// toEosEntityName('owner'),
// resetPermissions,
// null,
// false,
// )
// console.log('generated Keys:', generatedKeys)
// console.log('actions:', actions)
// const transaction = await prepTransactionFromActions(kylin, actions, env.EOS_KYLIN_OREIDFUNDING_PRIVATE_KEY)
// console.log('response:', await transaction.send())

// // -----> CreateAccount - recycle native Kylin account
// const recycleAccount = kylin.new.createAccount()
// await recycleAccount.composeTransaction(EosAccountType.Native, createAccountOptions_OreRecycleNative)
// await prepTransaction(kylin, recycleAccount.transaction, env.EOS_KYLIN_OREIDFUNDING_PRIVATE_KEY)
// console.log('createAccount response: ', await recycleAccount.transaction.send())
// const account = await kylin.new.account(createAccountOptions_OreRecycleNative.accountName)
// console.log('account can be recycled:', account.canBeRecycled)
// if (account.canBeRecycled) {
// const recycleAccount = kylin.new.createAccount()
// await recycleAccount.composeTransaction(EosNewAccountType.Native, createAccountOptions_OreRecycleNative)
// await prepTransaction(kylin, recycleAccount.transaction, env.EOS_KYLIN_OREIDFUNDING_PRIVATE_KEY)
// console.log('createAccount response: ', await recycleAccount.transaction.send())
// }

// -------------------- Permissions -----------------------

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@open-rights-exchange/chainjs",
"version": "0.9.9",
"version": "0.9.10",
"description": "Javascript helper library providing plug-in interfaces to multiple block chains.",
"main": "./dist/index.js",
"scripts": {
Expand Down
28 changes: 15 additions & 13 deletions src/chains/eos_1_8/eosAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EosPermissionSimplified,
EosActionStruct,
EosPublicKey,
EosGenerateMissingKeysParams,
EosNewKeysOptions,
EosGeneratedPermissionKeys,
EosGeneratedKeys,
} from './models'
Expand Down Expand Up @@ -129,11 +129,11 @@ export class EosAccount implements Account {
/** Both new password and salt must be provided if any permissions are missing public keys */
private assertValidOptionNewKeys = (
permissionsToAdd: Partial<EosPermissionSimplified>[],
generateMissingKeysParams?: EosGenerateMissingKeysParams,
newKeysOptions?: EosNewKeysOptions,
) => {
const isAnyPublicKeyMissing = permissionsToAdd.some(p => isNullOrEmpty(p.publicKey))
const { newKeysPassword, newKeysSalt } = generateMissingKeysParams || {}
if (isAnyPublicKeyMissing && (isNullOrEmpty(newKeysPassword) || isNullOrEmpty(newKeysSalt))) {
const { password, salt } = newKeysOptions || {}
if (isAnyPublicKeyMissing && (isNullOrEmpty(password) || isNullOrEmpty(salt))) {
throwNewError('Invalid Option - You must provide either public keys or a password AND salt to generate new keys')
}
}
Expand All @@ -144,20 +144,22 @@ export class EosAccount implements Account {
async composeAddPermissionsActions(
authPermission: EosEntityName,
permissionsToUpdate: Partial<EosPermissionSimplified>[],
generateMissingKeysParams?: EosGenerateMissingKeysParams,
newKeysOptions?: EosNewKeysOptions,
/** don't return an action for a permission that is already on the account with the same parent and publicKey */
skipExistingPermissions: boolean = true,
): Promise<{ generatedKeys: EosGeneratedPermissionKeys[]; actions: EosActionStruct[] }> {
let usePermissionsToUpdate = permissionsToUpdate
// Add permissions to current account structure
this.assertValidOptionNewKeys(permissionsToUpdate, generateMissingKeysParams)
this.assertValidOptionNewKeys(permissionsToUpdate, newKeysOptions)
// filter out permissions already on this account
const filteredPermissions = permissionsToUpdate.filter(
pu => !this.permissions.some(p => p.name === pu.name && p.parent === pu.parent && p.publicKey === pu.publicKey),
)
if (skipExistingPermissions) {
usePermissionsToUpdate = permissionsToUpdate.filter(
pu => !this.permissions.some(p => p.name === pu.name && p.parent === pu.parent && p.publicKey === pu.publicKey),
)
}
// generate new keys for each new permission if needed
const { generatedKeys, permissionsToAdd: usePermissionsToAdd } =
(await PermissionsHelper.generateMissingKeysForPermissionsToAdd(
filteredPermissions,
generateMissingKeysParams,
)) || {}
(await PermissionsHelper.generateMissingKeysForPermissionsToAdd(usePermissionsToUpdate, newKeysOptions)) || {}
const actions = this._permHelper.composeAddPermissionActions(this.name, authPermission, usePermissionsToAdd)
return { generatedKeys, actions }
}
Expand Down
26 changes: 13 additions & 13 deletions src/chains/eos_1_8/eosCreateAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EosEntityName,
EosPermissionStruct,
EosGeneratedKeys,
EosAccountType,
EosNewAccountType,
} from './models/index'
import { EosAccount } from './eosAccount'
import { throwNewError } from '../../errors'
Expand Down Expand Up @@ -45,7 +45,7 @@ export class EosCreateAccount implements CreateAccount {

private _transaction: EosTransaction

private _accountType: EosAccountType
private _accountType: EosNewAccountType

private _options: EosCreateAccountOptions

Expand All @@ -56,7 +56,7 @@ export class EosCreateAccount implements CreateAccount {
}

/** Compose a transaction to send to the chain to create a new account */
async composeTransaction(accountType: EosAccountType, options?: EosCreateAccountOptions): Promise<void> {
async composeTransaction(accountType: EosNewAccountType, options?: EosCreateAccountOptions): Promise<void> {
this._accountType = accountType
this._options = this.applyDefaultOptions(options)
const {
Expand Down Expand Up @@ -93,7 +93,7 @@ export class EosCreateAccount implements CreateAccount {
}

// check that we have account resource options for a native account (not needed if we are recycling)
if (!recycleAccount && accountType === EosAccountType.Native) {
if (!recycleAccount && accountType === EosNewAccountType.Native) {
this.assertValidOptionsResources()
}

Expand Down Expand Up @@ -138,16 +138,16 @@ export class EosCreateAccount implements CreateAccount {
this._didRecycleAccount = true
} else {
switch (accountType) {
case EosAccountType.Native:
case EosNewAccountType.Native:
createAccountActions = composeAction(ChainActionType.AccountCreate, params)
break
case EosAccountType.NativeOre:
case EosNewAccountType.NativeOre:
createAccountActions = [composeAction(ChainActionType.OreCreateAccount, params)]
break
case EosAccountType.CreateEscrow:
case EosNewAccountType.CreateEscrow:
createAccountActions = [composeAction(ChainActionType.CreateEscrowCreate, params)]
break
case EosAccountType.VirtualNested:
case EosNewAccountType.VirtualNested:
// For a virual 'nested' account, we don't have a create account action
// instead, we will need to add permissions (below) to the parent account
createAccountActions = await this.composeCreateVirtualNestedActions()
Expand Down Expand Up @@ -278,11 +278,11 @@ export class EosCreateAccount implements CreateAccount {
let { publicKeys } = this._options || {}
const { owner, active } = publicKeys || {}
const { newKeysOptions } = this._options
const { newKeysPassword, newKeysSalt } = newKeysOptions || {}
const { password, salt } = newKeysOptions || {}

// generate new public keys and add to options.publicKeyss
if (!owner || !active) {
generatedKeys = await generateNewAccountKeysAndEncryptPrivateKeys(newKeysPassword, newKeysSalt, { publicKeys })
generatedKeys = await generateNewAccountKeysAndEncryptPrivateKeys(password, salt, { publicKeys })
this._generatedKeys = {
...this._generatedKeys,
accountKeys: generatedKeys,
Expand Down Expand Up @@ -334,8 +334,8 @@ export class EosCreateAccount implements CreateAccount {

private assertValidOptionNewKeys() {
const { newKeysOptions, publicKeys } = this._options
const { newKeysPassword, newKeysSalt } = newKeysOptions || {}
if (isNullOrEmpty(publicKeys) && (isNullOrEmpty(newKeysPassword) || isNullOrEmpty(newKeysSalt))) {
const { password, salt } = newKeysOptions || {}
if (isNullOrEmpty(publicKeys) && (isNullOrEmpty(password) || isNullOrEmpty(salt))) {
throwNewError('Invalid Option - You must provide either public keys or a password AND salt to generate new keys')
}
}
Expand All @@ -357,7 +357,7 @@ export class EosCreateAccount implements CreateAccount {
}

/** Account type to be created */
get accountType(): EosAccountType {
get accountType(): EosNewAccountType {
return this._accountType
}

Expand Down
10 changes: 6 additions & 4 deletions src/chains/eos_1_8/eosPermissionsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EosPublicKey,
EosActionStruct,
EosPermissionSimplified,
EosGenerateMissingKeysParams,
EosNewKeysOptions,
EosGeneratedPermissionKeys,
} from './models'
import { EosChainState } from './eosChainState'
Expand Down Expand Up @@ -81,6 +81,7 @@ export class PermissionsHelper {
authAccount: EosEntityName,
authPermission: EosEntityName,
permissionsToAdd: Partial<EosPermissionSimplified>[] | EosPermissionStruct[] = [],
appendKeyToExistingPermission: boolean = false
): EosActionStruct[] {
const updateAuthActions: EosActionStruct[] = []
let usePermissionsToAdd = permissionsToAdd
Expand All @@ -93,6 +94,7 @@ export class PermissionsHelper {
usePermissionsToAdd = permissionsToAdd as EosPermissionSimplified[]

const newPermissions: EosPermissionStruct[] = []
// TODO: if appendKeyToExistingPermission = true, add the new key to the existing permission's require_auth array
// collect an array of new permission objects
usePermissionsToAdd.forEach(p => {
const permissionToAdd = this.composePermission(
Expand Down Expand Up @@ -258,10 +260,10 @@ export class PermissionsHelper {
/** generate a keypair for any new permissions missing a public key */
static generateMissingKeysForPermissionsToAdd = async (
permissionsToAdd: Partial<EosPermissionSimplified>[],
params: EosGenerateMissingKeysParams,
newKeysOptions: EosNewKeysOptions,
) => {
const generatedKeys: EosGeneratedPermissionKeys[] = []
const { newKeysPassword, newKeysSalt } = params || {}
const { password, salt } = newKeysOptions || {}

if (isNullOrEmpty(permissionsToAdd)) {
return null
Expand All @@ -271,7 +273,7 @@ export class PermissionsHelper {
const keysToFix = permissionsToAdd.map(async p => {
if (!p.publicKey) {
const updatedPerm = p
const keys = await generateKeyPairAndEncryptPrivateKeys(newKeysPassword, newKeysSalt)
const keys = await generateKeyPairAndEncryptPrivateKeys(password, salt)
updatedPerm.publicKey = keys.public
updatedPerm.publicKeyWeight = 1
generatedKeys.push({ permissionName: updatedPerm.name, keyPair: keys })
Expand Down
7 changes: 7 additions & 0 deletions src/chains/eos_1_8/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './eosAccount'
export * from './eosCompose'
export * from './eosConstants'
export * from './eosCreateAccount'
export * from './eosCrypto'
export * from './eosErrors'
export * from './eosTransaction'
11 changes: 4 additions & 7 deletions src/chains/eos_1_8/models/accountModels.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EosEntityName, EosAsset } from './generalModels'
import { EosEntityName, EosAsset, EosNewKeysOptions } from './generalModels'
import { EosPublicKey } from './cryptoModels'

/** Type of account to craate */
export enum EosAccountType {
/** Type of account to create */
export enum EosNewAccountType {
/** Native account for chain tyep (EOS, Ethereum, etc.) */
Native = 'Native',
/** Native account on ORE chain */
Expand All @@ -23,10 +23,7 @@ export type EosCreateAccountOptions = {
owner?: EosPublicKey
active?: EosPublicKey
}
newKeysOptions?: {
newKeysPassword?: string
newKeysSalt?: string
}
newKeysOptions?: EosNewKeysOptions
oreOptions?: {
pricekey?: number // default = 1
referralAccountName?: EosEntityName // default = '' // aka referral
Expand Down
6 changes: 3 additions & 3 deletions src/chains/eos_1_8/models/generalModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type EosGeneratedPermissionKeys = {
keyPair: KeyPairEncrypted
}

export type EosGenerateMissingKeysParams = {
newKeysPassword?: string
newKeysSalt?: string
export type EosNewKeysOptions = {
password?: string
salt?: string
}
6 changes: 6 additions & 0 deletions src/chains/ethereum_1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './ethAccount'
export * from './ethCompose'
export * from './ethConstants'
export * from './ethCrypto'
export * from './ethErrors'
export * from './ethTransaction'
Loading