-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat(kadena-cli): add kadena-cli devnet commands #1140
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1ec3e18
feat(kadena-cli): Dynamic config
jessevanmuijden e9eacd3
feat(kadena-cli): use @kadena/client-utils
jessevanmuijden 3a64e41
wip(kadena-cli): refactor to standardized config
jessevanmuijden f1c01ef
fix(kadena-cli): account get-balance now works. But without types
alber70g e092658
fix(kadena-cli): add logger for command and config
alber70g d9afeed
wip(kadena-cli): wip
jessevanmuijden 890131f
wip
alber70g 4f4da26
fix(kadena-cli): createCommand now uses newArgs that include the resp…
alber70g 07ad0fe
wip(kadena-cli): refactor networks commands
jessevanmuijden f0b87d4
feat(kadena-cli): create account command
jessevanmuijden 8e95215
feat(kadena-cli): create account
jessevanmuijden f4282bb
feat(kadena-cli): add kadena-cli devnet commands
jessevanmuijden bfe0e2b
chore(kadena-cli): refactor devnet commands to createCommand pattern
jessevanmuijden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@kadena/kadena-cli': patch | ||
--- | ||
|
||
add devnet commands to kadena cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { ChainId } from '@kadena/client'; | ||
import { | ||
addData, | ||
execution, | ||
} from '@kadena/client/fp'; | ||
import { pipe } from 'ramda'; | ||
import { dirtyReadClient } from '../core/client-helpers'; | ||
import type { IClientConfig } from '../core/utils/helpers'; | ||
|
||
interface ICreatePrincipalCommandInput { | ||
keyset: { | ||
keys: string[]; | ||
pred: 'keys-all' | 'keys-two' | 'keys-one'; | ||
}; | ||
gasPayer: { account: string; publicKeys: string[] }; | ||
chainId: ChainId; | ||
} | ||
/** | ||
* @alpha | ||
*/ | ||
export const createPrincipalCommand = async ( | ||
inputs: ICreatePrincipalCommandInput, | ||
config: Omit<IClientConfig, 'sign'>, | ||
) => | ||
pipe( | ||
() => '(create-principal (read-keyset "ks"))', | ||
execution, | ||
addData('ks', inputs.keyset), | ||
dirtyReadClient(config), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
network: testnet | ||
networkId: testnet01 | ||
networkId: testnet04 | ||
networkHost: https://api.testnet.chainweb.com | ||
networkExplorerUrl: https://explorer.chainweb.com/testnet/tx/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/tools/kadena-cli/src/account/accountHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { getExistingAccounts, sanitizeFilename } from "../utils/helpers.js"; | ||
import { defaultAccountsPath, accountDefaults } from "../constants/accounts.js"; | ||
import { removeFile, writeFile } from "../utils/filesystem.js"; | ||
import { existsSync, readFileSync, type WriteFileOptions } from 'fs'; | ||
import yaml from 'js-yaml'; | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import { ChainId } from "@kadena/types"; | ||
|
||
export interface ICustomAccountsChoice { | ||
value: string; | ||
name?: string; | ||
description?: string; | ||
disabled?: boolean | string; | ||
} | ||
|
||
export interface IAccountCreateOptions { | ||
name: string; | ||
account: string; | ||
keyset: string; | ||
network: string; | ||
chainId: ChainId; | ||
} | ||
|
||
/** | ||
* Removes the given account from the accounts folder | ||
* | ||
* @param {Pick<IAccountCreateOptions, 'name'>} options - The account configuration. | ||
* @param {string} options.name - The name of the account. | ||
*/ | ||
export function removeAccount(options: Pick<IAccountCreateOptions, 'name'>): void { | ||
const { name } = options; | ||
jessevanmuijden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const sanitizedName = sanitizeFilename(name).toLowerCase(); | ||
const accountFilePath = path.join( | ||
defaultAccountsPath, | ||
`${sanitizedName}.yaml`, | ||
); | ||
|
||
removeFile(accountFilePath); | ||
} | ||
|
||
export function writeAccount(options: IAccountCreateOptions): void { | ||
const { name } = options; | ||
const sanitizedName = sanitizeFilename(name).toLowerCase(); | ||
const accountFilePath = path.join( | ||
defaultAccountsPath, | ||
`${sanitizedName}.yaml`, | ||
); | ||
|
||
writeFile( | ||
accountFilePath, | ||
yaml.dump(options), | ||
'utf8' as WriteFileOptions, | ||
); | ||
jessevanmuijden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export async function displayAccountsConfig(): Promise<void> { | ||
const log = console.log; | ||
const formatLength = 80; // Maximum width for the display | ||
|
||
const displaySeparator = (): void => { | ||
log(chalk.green('-'.padEnd(formatLength, '-'))); | ||
}; | ||
|
||
const formatConfig = ( | ||
key: string, | ||
value?: string, | ||
isDefault?: boolean, | ||
): string => { | ||
const valueDisplay = | ||
(value?.trim() ?? '') !== '' ? chalk.green(value!) : chalk.red('Not Set'); | ||
|
||
const defaultIndicator = | ||
isDefault === true ? chalk.yellow(' (Using defaults)') : ''; | ||
const keyValue = `${key}: ${valueDisplay}${defaultIndicator}`; | ||
const remainingWidth = | ||
formatLength - keyValue.length > 0 ? formatLength - keyValue.length : 0; | ||
return ` ${keyValue}${' '.repeat(remainingWidth)} `; | ||
}; | ||
|
||
|
||
const existingAccounts: ICustomAccountsChoice[] = await getExistingAccounts(); | ||
|
||
existingAccounts.forEach(({ value }) => { | ||
const filePath = path.join(defaultAccountsPath, `${value}.yaml`); | ||
if (! existsSync) { | ||
return; | ||
} | ||
|
||
const accountConfig = (yaml.load( | ||
readFileSync(filePath, 'utf8'), | ||
) as IAccountCreateOptions); | ||
|
||
displaySeparator(); | ||
log(formatConfig('Name', value)); | ||
log(formatConfig('Account', accountConfig.account)); | ||
log(formatConfig('Keyset', accountConfig.keyset)); | ||
log(formatConfig('Network', accountConfig.network)); | ||
log(formatConfig('Chain ID', accountConfig.chainId.toString())); | ||
}); | ||
|
||
displaySeparator(); | ||
}; | ||
|
||
export function loadAccountConfig(name: string): IAccountCreateOptions | never { | ||
const filePath = path.join(defaultAccountsPath, `${name}.yaml`); | ||
|
||
if (! existsSync(filePath)) { | ||
throw new Error('Account file not found.') | ||
} | ||
|
||
return (yaml.load( | ||
readFileSync(filePath, 'utf8'), | ||
) as IAccountCreateOptions); | ||
}; |
97 changes: 97 additions & 0 deletions
97
packages/tools/kadena-cli/src/account/createAccountCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { createAccount, createPrincipalAccount, createPrincipalCommand } from '@kadena/client-utils/coin'; | ||
import { createCommand } from '../utils/createCommand.js'; | ||
import { globalOptions } from '../utils/globalOptions.js'; | ||
import chalk from 'chalk'; | ||
import { ChainId, createSignWithKeypair } from '@kadena/client'; | ||
import { writeAccount } from './accountHelpers.js'; | ||
import { loadKeysetConfig } from '../keyset/keysetHelpers.js'; | ||
import { loadKeypairConfig } from '../keypair/keypairHelpers.js'; | ||
|
||
// eslint-disable-next-line @rushstack/typedef-var | ||
export const createAccountCommand = createCommand( | ||
'create', | ||
'Create account', | ||
[globalOptions.accountName(), globalOptions.keyset(), globalOptions.network(), globalOptions.chainId(), globalOptions.gasPayer()], | ||
async (config) => { | ||
try { | ||
const publicKeys = config.keysetConfig.publicKeys.split(',').map(value => value.trim()).filter(value => value.length); | ||
for (let keypair of config.keysetConfig.publicKeysFromKeypairs) { | ||
const keypairConfig = await loadKeypairConfig(keypair) | ||
publicKeys.push(keypairConfig.publicKey || ''); | ||
} | ||
|
||
const createPrincipal = await createPrincipalCommand({ | ||
keyset: { | ||
pred: config.keysetConfig.predicate as 'keys-all' | 'keys-two' | 'keys-one', | ||
keys: publicKeys, | ||
}, | ||
gasPayer: { account: 'dummy', publicKeys: [] }, | ||
chainId: config.chainId as ChainId, | ||
}, | ||
{ | ||
host: config.networkConfig.networkHost, | ||
defaults: { | ||
networkId: config.networkConfig.networkId, | ||
meta: { | ||
chainId: config.chainId as ChainId, | ||
} | ||
}, | ||
}); | ||
|
||
const account = await createPrincipal().execute(); | ||
|
||
writeAccount({ | ||
...config, | ||
name: config.account, | ||
account: account as string, | ||
}); | ||
|
||
console.log(chalk.green(`\nSaved the account configuration "${config.account}".\n`)); | ||
|
||
// @todo: make gas payer configuration more flexible. | ||
const gasPayerKeyset = await loadKeysetConfig(config.gasPayerConfig.keyset); | ||
// @todo: now it is simply assumed that the gas payer account is governed with a keypair | ||
const gasPayerKeypair = await loadKeypairConfig(gasPayerKeyset.publicKeysFromKeypairs.pop() || ''); | ||
|
||
// @todo: also allow other signing methods | ||
const signWithKeyPair = createSignWithKeypair({ | ||
publicKey: gasPayerKeypair.publicKey, | ||
secretKey: gasPayerKeypair.secretKey, | ||
}); | ||
|
||
const c = await createAccount( | ||
{ | ||
account: account as string, | ||
keyset: { | ||
pred: config.keysetConfig.predicate as 'keys-all' | 'keys-two' | 'keys-one', | ||
keys: publicKeys, | ||
}, | ||
gasPayer: { account: config.gasPayerConfig.account, publicKeys: [gasPayerKeypair.publicKey] }, | ||
chainId: config.chainId as ChainId, | ||
}, | ||
{ | ||
host: config.networkConfig.networkHost, | ||
defaults: { | ||
networkId: config.networkConfig.networkId, | ||
meta: { | ||
chainId: config.chainId as ChainId, | ||
} | ||
}, | ||
sign: signWithKeyPair, | ||
}, | ||
) | ||
|
||
const result = await c.execute(); | ||
|
||
console.log(result); | ||
|
||
if (result === 'Write succeeded') { | ||
console.log(chalk.green(`\nCreated account "${account}" guarded by keyset "${config.keyset}" on chain ${config.chainId} of "${config.network}".\n`)); | ||
} else { | ||
console.log(chalk.red(`\nFailed to created the account on "${config.network}".\n`)); | ||
} | ||
} catch (e) { | ||
console.log(chalk.red(e.message)); | ||
} | ||
}, | ||
); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
.kadena
shouldn't be checked in, right?