Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main() {
// Construct
const wsProvider = new WsProvider('ws://127.0.0.1:8546');
const api = await ApiPromise.create({ provider: wsProvider });
// Simple transaction
const keyring = new Keyring({type: 'sr25519' });
const aliceKey = keyring.addFromUri('//Alice', { name: 'Alice default' });
console.log(`${aliceKey.meta.name}: has address ${aliceKey.address} with publicKey [${aliceKey.publicKey}]`);
const ADDR_Bob = '0x90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22';
const transfer = await api.tx.balances.transfer(ADDR_Bob, 12345)
.signAndSend(aliceKey, {era: 0, blockHash: '0x64597c55a052d484d9ff357266be326f62573bb4fbdbb3cd49f219396fcebf78', blockNumber:0, genesisHash: '0x64597c55a052d484d9ff357266be326f62573bb4fbdbb3cd49f219396fcebf78', nonce: 1, tip: 0, transactionVersion: 1});
console.log(`hxHash ${transfer}`);
}
async function main () {
// Instantiate the API
const api = await ApiRx.create().toPromise();
// Create an instance of the keyring
const keyring = new Keyring({ type: 'sr25519' });
// Add Alice to our keyring (with the known seed for the account)
const alice = keyring.addFromUri('//Alice');
// Create a extrinsic, transferring 12345 units to Bob.
const subscription = api.tx.balances
// create transfer
.transfer(BOB, 12345)
// Sign and send the transcation
.signAndSend(alice)
// Subscribe to the status updates of the transfer
.subscribe(({ status }) => {
if (status.isFinalized) {
console.log(`Successful transfer of 12345 from Alice to Bob with hash ${status.asFinalized.toHex()}`);
subscription.unsubscribe();
} else {
async function main () {
// Instantiate the API
const api = await ApiPromise.create();
// Constuct the keying after the API (crypto has an async init)
const keyring = new Keyring({ type: 'sr25519' });
// Add Alice to our keyring with a hard-deived path (empty phrase, so uses dev)
const alice = keyring.addFromUri('//Alice');
// Create a extrinsic, transferring 12345 units to Bob
const transfer = api.tx.balances.transfer(BOB, 12345);
// Sign and send the transaction using our account
const hash = await transfer.signAndSend(alice);
console.log('Transfer sent with hash', hash.toHex());
}
async function main () {
// Instantiate the API
const api = await ApiPromise.create(); // default provider
// Constuct the keying after the API (crypto has an async init)
const keyring = new Keyring({ type: 'sr25519' });
// Add Alice to our keyring with a hard-derived path (empty phrase, so uses dev)
const alice = keyring.addFromUri('//Alice');
// Get nonce for account
const nonce = await api.query.system.accountNonce(alice.address);
// Get current block
const signedBlock = await api.rpc.chain.getBlock();
// Get current block height and hash
const currentHeight = signedBlock.block.header.number;
const blockHash = signedBlock.block.header.hash;
// NOTE By default the API will send mortal transactions, only explicitly construct
// if you wish to override the defaults
export default async function cmdSign (account: string, seed: string, type: Curves, [payload]: string[]): Promise {
await cryptoWaitReady();
const keyring = new Keyring({ type });
const pair = keyring.createFromUri(seed);
const signature = pair.sign(hexToU8a(payload));
const prefix = new Uint8Array(curvePrefixes[type]);
console.log(`Signature: ${u8aToHex(u8aConcat(prefix, signature))}`);
process.exit(0);
}
async function makeTx ({ fn, log }: CallInfo): Promise {
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 account = keyring.createFromUri(seed, {}, sign as 'ed25519');
return fn(...params).signAndSend(account, (result: SubmittableResult): void => {
log(result);
if (result.isFinalized) {
process.exit(0);
}
});
}