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

Add ethereum to cli signer #220

Merged
merged 6 commits into from
Jul 30, 2021
Merged
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
58 changes: 38 additions & 20 deletions packages/api-cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { KeyringPair } from '@polkadot/keyring/types';
import type { Hash } from '@polkadot/types/interfaces';
import type { CallFunction, Codec } from '@polkadot/types/types';
import type { KeypairType } from '@polkadot/util-crypto/types';

import fs from 'fs';
import yargs from 'yargs';
Expand Down Expand Up @@ -73,7 +74,7 @@ interface Params {
ws: string;
}

const CRYPTO = ['ed25519', 'sr25519'];
const CRYPTO = ['ed25519', 'sr25519', 'ethereum'];

// retrieve and parse arguments - we do this globally, since this is a single command
const argv = yargs
Expand All @@ -84,7 +85,8 @@ const argv = yargs
.command('$0', `Usage: [options] <endpoint> <...params>
Example: query.system.account 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKv3gB
Example: query.substrate.code --info
Example: --seed "//Alice" tx.balances.transfer F7Gh 10000`)
Example: --seed "//Alice" tx.balances.transfer F7Gh 10000`
)
.middleware(hexMiddleware)
.middleware(jsonMiddleware)
.wrap(120)
Expand Down Expand Up @@ -151,10 +153,13 @@ async function getCallInfo (): Promise<CallInfo> {

const provider = new WsProvider(ws);
const api = await ApiPromise.create({ provider, types: readTypes() });
const apiExt = api as unknown as ApiExt;
const apiExt = (api as unknown) as ApiExt;
const [type, section, method] = endpoint.split('.') as [keyof ApiExt, string, string];

assert(['consts', 'derive', 'query', 'rpc', 'tx'].includes(type), `Expected one of consts, derive, query, rpc, tx, found ${type}`);
assert(
['consts', 'derive', 'query', 'rpc', 'tx'].includes(type),
`Expected one of consts, derive, query, rpc, tx, found ${type}`
);
assert(apiExt[type][section], `Cannot find ${type}.${section}`);
assert(apiExt[type][section][method], `Cannot find ${type}.${section}.${method}`);

Expand All @@ -163,14 +168,19 @@ async function getCallInfo (): Promise<CallInfo> {
return {
api,
fn,
log: (result: SubmittableResult | Codec | ApiCallFn): void => console.log(
JSON.stringify({
// eslint-disable-next-line @typescript-eslint/unbound-method
[method]: isFunction((result as Codec).toHuman)
? (result as Codec).toHuman()
: result
}, null, 2)
),
log: (result: SubmittableResult | Codec | ApiCallFn): void =>
console.log(
JSON.stringify(
{
// eslint-disable-next-line @typescript-eslint/unbound-method
[method]: isFunction((result as Codec).toHuman)
? (result as Codec).toHuman()
: result
},
null,
2
)
Comment on lines +179 to +182
Copy link
Member

Choose a reason for hiding this comment

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

Formatting here is quite weird, not sure why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Me neither. The linter doesn't complain

),
method,
section,
type
Expand All @@ -191,9 +201,7 @@ function logDetails ({ fn: { description, meta }, method, section }: CallInfo):
if (description) {
console.log(description);
} else if (meta) {
meta.documentation.forEach((doc: Text): void =>
console.log(doc.toString())
);
meta.documentation.forEach((doc: Text): void => console.log(doc.toString()));
} else {
console.log('No documentation available');
}
Expand All @@ -204,13 +212,21 @@ function logDetails ({ fn: { description, meta }, method, section }: CallInfo):
process.exit(0);
}

function isCrypto (type: string): type is KeypairType {
if (CRYPTO.includes(type)) {
return true;
}

return false;
}

// send a transaction
async function makeTx ({ api, fn, log }: CallInfo): Promise<(() => void) | Hash> {
assert(seed, 'You need to specify an account seed with tx.*');
assert(CRYPTO.includes(sign), `The crypto type can only be one of ${CRYPTO.join(', ')} found '${sign}'`);

const keyring = new Keyring();
const auth = keyring.createFromUri(seed, {}, sign as 'ed25519');
const auth = keyring.createFromUri(seed, {}, isCrypto(sign) ? sign : undefined);
let signable;

if (sudo) {
Expand All @@ -235,15 +251,17 @@ async function makeTx ({ api, fn, log }: CallInfo): Promise<(() => void) | Hash>
// make a derive, query or rpc call
// eslint-disable-next-line @typescript-eslint/require-await
async function makeCall ({ fn, log, method, type }: CallInfo): Promise<void> {
const isRpcSub = (type === 'rpc') && method.startsWith('subscribe');
const isRpcSub = type === 'rpc' && method.startsWith('subscribe');

return sub || isRpcSub
? fn(...params, log).then((): void => {
// ignore, we keep trucking on
})
: fn(...params).then(log).then((): void => {
process.exit(0);
});
: fn(...params)
.then(log)
.then((): void => {
process.exit(0);
});
}

// our main entry point - from here we call out
Expand Down