Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Oct 1, 2024
1 parent 148b6f8 commit 657e6fa
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 55 deletions.
12 changes: 5 additions & 7 deletions src/account/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ function entropyAccountList () {
.description('List all accounts. Output is JSON of form [{ name, address, verifyingKeys }]')
.action(async () => {
// TODO: test if it's an encrypted account, if no password provided, throw because later on there's no protection from a prompt coming up
const storedConfig = await config.get()
const accounts = EntropyAccount.list(storedConfig)
const accounts = await config.get()
.then(storedConfig => EntropyAccount.list(storedConfig))
.catch(() => [])
// QUESTION: is dropping the error right? Maybe only if "There are currently no accounts"

cliWrite(accounts)
process.exit(0)
})
Expand All @@ -104,16 +107,11 @@ function entropyAccountRegister () {
// )
// )
.action(async (opts) => {
console.log('here 0')
console.log(opts)
// NOTE: loadEntropy throws if it can't find opts.account
const entropy: Entropy = await loadEntropy(opts.account, opts.endpoint)
const accountService = new EntropyAccount(entropy, opts.endpoint)

console.log('here 1')

const verifyingKey = await accountService.register()
console.log('here 2')
await addVerifyingKeyToAccountAndSelect(verifyingKey, opts.account)

cliWrite(verifyingKey)
Expand Down
5 changes: 1 addition & 4 deletions src/account/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ export async function entropyAccount (endpoint: string, storedConfig: EntropyCon
return
}
const { selectedAccount } = await inquirer.prompt(accountSelectQuestions(accounts))
await config.set({
...storedConfig,
selectedAccount: selectedAccount.address
})
await config.setSelectedAccount(selectedAccount)

print('Current selected account is ' + selectedAccount)
return
Expand Down
39 changes: 32 additions & 7 deletions src/account/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ export class EntropyAccount extends EntropyBase {
return EntropyAccount.import({ name, seed, path })
}

// WARNING: #create depends on #import => be careful modifying this function
static async import ({ name, seed, path }: AccountImportParams ): Promise<EntropyAccountConfig> {
// WARNING: #create currently depends on this => be careful modifying this function

await wasmGlobalsReady()
const keyring = new Keyring({ seed, path, debug: true })

const fullAccount = keyring.getAccount()
// TODO: sdk should create account on constructor
const { admin } = keyring.getAccount()
const data = fixData(fullAccount)
// const encryptedData = password ? passwordFlow.encrypt(data, password) : data

const data = fullAccount
const { admin } = keyring.getAccount()
delete admin.pair
// const encryptedData = password ? passwordFlow.encrypt(data, password) : data


return {
name,
address: admin.address,
Expand Down Expand Up @@ -90,7 +90,7 @@ export class EntropyAccount extends EntropyBase {
dispatchError.asModule
)
const { docs, name, section } = decoded

msg = `${section}.${name}: ${docs.join(' ')}`
} else {
// Other, CannotLookup, BadOrigin, no extra info
Expand All @@ -107,3 +107,28 @@ export class EntropyAccount extends EntropyBase {
})
}
}

// TODO: there is a bug in SDK that is munting this data
function fixData (data) {
if (data.admin) {
data.admin.pair.addressRaw = objToUint8Array(data.admin.pair.addressRaw)
data.admin.pair.secretKey = objToUint8Array(data.admin.pair.secretKey)
data.admin.pair.publicKey = objToUint8Array(data.admin.pair.publicKey)
}

if (data.registration) {
data.registration.pair.addressRaw = objToUint8Array(data.registration.pair.addressRaw)
data.registration.pair.secretKey = objToUint8Array(data.registration.pair.secretKey)
data.registration.pair.publicKey = objToUint8Array(data.registration.pair.publicKey)
}

return data
}

function objToUint8Array (obj) {
const values: any = Object.entries(obj)
.sort((a, b) => Number(a[0]) - Number(b[0])) // sort entries by keys
.map(entry => entry[1])

return new Uint8Array(values)
}
11 changes: 4 additions & 7 deletions src/account/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ export async function selectAndPersistNewAccount (newAccount: EntropyAccountConf
accounts.push(newAccount)
await config.set({
...storedConfig,
selectedAccount: newAccount.address
selectedAccount: newAccount.name
})
}

export async function addVerifyingKeyToAccountAndSelect (verifyingKey: string, accountNameOrAddress: string) {
const storedConfig = await config.get()
const account = findAccountByAddressOrName(storedConfig.accounts, accountNameOrAddress)
const { accounts } = await config.get()
const account = findAccountByAddressOrName(accounts, accountNameOrAddress)
if (!account) throw Error(`Unable to persist verifyingKey "${verifyingKey}" to unknown account "${accountNameOrAddress}"`)

account.data.registration.verifyingKeys.push(verifyingKey)

// persist to config, set selectedAccount
await config.set({
...storedConfig,
selectedAccount: account.address
})
await config.setSelectedAccount(account)
}

function validateSeedInput (seed) {
Expand Down
24 changes: 12 additions & 12 deletions src/common/utils-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function endpointOption () {
return endpoint
})
.default('ws://testnet.entropy.xyz:9944/')
// NOTE: argParser is only run IF an option is provided, so this cannot be 'test-net'
// NOTE: argParser only runs IF the -e/--endpoint option called, so this default cannot be 'test-net'
}

export function passwordOption (description?: string) {
Expand All @@ -61,20 +61,20 @@ export function accountOption () {
].join(' ')
)
.env('ENTROPY_ACCOUNT')
.argParser(async (account) => {
if (storedConfig && storedConfig.selectedAccount !== account) {
// Updated selected account in config with new address from this option
await config.set({
...storedConfig,
selectedAccount: account
})
}
.argParser(async (addressOrName) => {
// We try to map addressOrName to an account we have stored
if (!storedConfig) return addressOrName

return account
const account = findAccountByAddressOrName(storedConfig.accounts, addressOrName)
if (!account) return addressOrName

// If we find one, we set this account as the future default
await config.setSelectedAccount(account)

// We finally return the account name to be as consistent as possible (using name, not address)
return account.name
})
.default(storedConfig?.selectedAccount)
// TODO: display the *name* not address
// TODO: standardise whether selectedAccount is name or address.
}

export async function loadEntropy (addressOrName: string, endpoint: string, password?: string): Promise<Entropy> {
Expand Down
17 changes: 16 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import envPaths from 'env-paths'

import allMigrations from './migrations'
import { serialize, deserialize } from './encoding'
import { EntropyConfig } from './types'
import { EntropyConfig, EntropyAccountConfig } from './types'

const paths = envPaths('entropy-cryptography', { suffix: '' })
const CONFIG_PATH = join(paths.config, 'entropy-cli.json')
Expand Down Expand Up @@ -63,6 +63,7 @@ export async function get (configPath = CONFIG_PATH) {

export function getSync (configPath = CONFIG_PATH) {
const configStr = readFileSync(configPath, 'utf8')
// console.log('getSync', configPath, configStr)
return deserialize(configStr)
}

Expand All @@ -73,6 +74,20 @@ export async function set (config: EntropyConfig, configPath = CONFIG_PATH) {
await writeFile(configPath, serialize(config))
}

export async function setSelectedAccount (account: EntropyAccountConfig, configPath = CONFIG_PATH) {
const storedConfig = await get(configPath)

if (storedConfig.selectedAccount === account.name) return storedConfig
// no need for update

const newConfig = {
...storedConfig,
selectedAccount: account.name
}
await set(newConfig, configPath)
return newConfig
}

/* util */
function noop () {}
function assertConfigPath (configPath) {
Expand Down
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface EntropyConfig {
dev: string;
'test-net': string
}
// selectedAccount is account.name (alias) for the account
selectedAccount: string
'migration-version': string
}
Expand Down
8 changes: 0 additions & 8 deletions src/transfer/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ export function entropyTransferCommand () {
.addOption(endpointOption())
.addOption(passwordOption('Password for the source account (if required)'))
.action(async (destination, amount, opts) => {
console.log({ destination, amount, opts })

// TODO: destination as <name|address> ?
const entropy = await loadEntropy(opts.account, opts.endpoint)
.catch(err => {
// WIP here. SOMETHING is wrecking the config upstream
console.error("loadEntropy failed", err)
throw err
})

const transferService = new EntropyTransfer(entropy, opts.endpoint)

await transferService.transfer(destination, amount)
Expand Down
6 changes: 1 addition & 5 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ async function setupConfig () {

// set selectedAccount if we can
if (!storedConfig.selectedAccount && storedConfig.accounts.length) {
await config.set({
...storedConfig,
selectedAccount: storedConfig.accounts[0].address
})
storedConfig = await config.get()
storedConfig = await config.setSelectedAccount(storedConfig.accounts[0])
}

return storedConfig
Expand Down
7 changes: 3 additions & 4 deletions tests/cli.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ print () {

print "// ACCOUNT /////////////////////////////////////////////////"

# Errors (correct, but messy?)
# print "account ls:"
# entropy account ls | jq
print "account ls"
entropy account ls | jq
# TODO: change this to return [] ?

print "account create"
entropy account create naynay | jq
Expand Down Expand Up @@ -45,7 +45,6 @@ print "// TRANSFER ////////////////////////////////////////////////"

print "entropy transfer"
NAYNAY_ADDRESS=`entropy account ls | jq --raw-output ".[0].address"`
# NOTE: --raw-output is needed to drop the quotes
entropy transfer -a faucet ${NAYNAY_ADDRESS} 2.5
entropy balance naynay

Expand Down

0 comments on commit 657e6fa

Please sign in to comment.