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

all Uint8Array output in base64 #163

Merged
merged 11 commits into from
Jul 29, 2024
5 changes: 2 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { cliGetBalance } from './flows/balance/cli'
import { cliListAccounts } from './flows/manage-accounts/cli'
import { cliEntropyTransfer } from './flows/entropyTransfer/cli'
import { cliSign } from './flows/sign/cli'
import { stringify } from './common/utils'

const program = new Command()

Expand Down Expand Up @@ -112,9 +113,7 @@ program.command('sign')
})

function writeOut (result) {
const prettyResult = typeof result === 'object'
? JSON.stringify(result, null, 2)
: result
const prettyResult = stringify(result)
process.stdout.write(prettyResult)
}

Expand Down
8 changes: 7 additions & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export function stripHexPrefix (str: string): string {
return str
}

export function stringify (thing, indent = 2) {
return (typeof thing === 'object')
? JSON.stringify(thing, replacer, indent)
: thing
}

export function replacer (key, value) {
if (value instanceof Uint8Array) {
return Buffer.from(value).toString('base64')
Expand All @@ -14,7 +20,7 @@ export function replacer (key, value) {
}

export function print (...args) {
console.log(...args)
console.log(...args.map(arg => stringify(arg)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the core change
by default, print now converts UInt8Array => base64 so that output is not spammed with walls of Ints

}

// hardcoding for now instead of querying chain
Expand Down
10 changes: 5 additions & 5 deletions src/flows/entropyTransfer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export async function entropyTransfer ({ accounts, selectedAccount: selectedAcco
)
if (transferStatus.isFinalized) stopProgress()

print(
`\nTransaction successful: Sent ${amount} to ${recipientAddress}`
)
print('\nPress enter to return to main menu')
print('')
print(`Transaction successful: Sent ${amount} to ${recipientAddress}`)
print('')
print('Press enter to return to main menu')
} catch (error) {
stopProgress()
console.error('ERR:::', error);
}
}
}
3 changes: 2 additions & 1 deletion src/flows/manage-accounts/new-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export async function newAccount ({ accounts }, logger: EntropyLogger) {

const newAccount = await createAccount({ name, seed, path }, logger)

print(`New account:\n{\n\tname: ${newAccount.name}\n\taddress: ${newAccount.address}\n}`)
print('New account:')
print({ name: newAccount.name, address: newAccount.address })

accounts.push(newAccount)
return { accounts, selectedAccount: newAccount.address }
Expand Down
31 changes: 25 additions & 6 deletions src/flows/user-program-management/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ import { print } from "src/common/utils"

export function displayPrograms (programs): void {
programs.forEach((program, index) => {
print(
`${index + 1}. Pointer: ${
program.program_pointer
}, Config: ${JSON.stringify(program.program_config)}`
)
print(`${index + 1}.`)
print({
pointer: program.program_pointer,
config: parseProgramConfig(program.program_config)
})
print('')
})
}
}

function parseProgramConfig (rawConfig: unknown) {
if (typeof rawConfig !== 'string') return rawConfig
if (!rawConfig.startsWith('0x')) return rawConfig

const hex = rawConfig.slice(2)
const utf8 = Buffer.from(hex, 'hex').toString()
const output = JSON.parse(utf8)
Object.keys(output).forEach(key => {
output[key] = output[key].map(base64toHex)
})

return output
}

function base64toHex (base64: string): string {
return Buffer.from(base64, 'base64').toString('hex')
Comment on lines +14 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new feature.
NOTE: the program_config by default stores the addresses as base64, y'all asked for hex to make comparisons easier, so I've added that

}
1 change: 1 addition & 0 deletions src/flows/user-program-management/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ export async function userPrograms ({ accounts, selectedAccount: selectedAccount
return 'exit'
}
}

Loading