Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { KeyringPair } from "@polkadot/keyring/types";
import { Address, ContractInfo, Balance, Hash } from "@polkadot/types/interfaces";
import BN from "bn.js";
import { ALICE, BOB, CREATION_FEE, WSURL } from "./consts";
import {
callContract,
instantiate,
getContractStorage,
putCode
} from "./utils";
// This is a test account that is going to be created and funded each test.
const keyring = testKeyring({ type: "sr25519" });
const bobPair = keyring.getPair(BOB);
const randomSeed = randomAsU8a(32);
let testAccount: KeyringPair;
let api: ApiPromise;
beforeAll((): void => {
jest.setTimeout(30000);
});
beforeEach(
async (done): Promise<() => void> => {
api = await ApiPromise.create({ provider: new WsProvider(WSURL) });
testAccount = keyring.addFromSeed(randomSeed);
return api.tx.balances
.transfer(testAccount.address, CREATION_FEE.muln(3))
.signAndSend(bobPair, (result: SubmittableResult): void => {
if (
async function main () {
// Create account seed for Alice as fallback if generated mnemonic not valid
const seedAlice = 'Alice'.padEnd(32, ' ');
// Generate new public/secret keypair for Alice from the supplied seed
const { secretKey, publicKey } = naclKeypairFromSeed(stringToU8a(seedAlice));
// Encrypt message. Create Uint8Array's filled with random bytes of specified length
const secret = randomAsU8a(32);
const messagePreEncryption = stringToU8a('please send me DOTs');
const noncePreEncryption = randomAsU8a(24);
const { encrypted } = naclEncrypt(messagePreEncryption, secret, noncePreEncryption);
// Sign the message with a valid signature
const messageSignature = naclSign(encrypted, secretKey);
console.log(`Message signature: ${u8aToHex(messageSignature)}`);
// Validate that the message was correctly signed
const isValidSignature = naclVerify(encrypted, messageSignature, publicKey);
console.log(`Was the message correctly signed? ${isValidSignature}`);
}
// Create the API and wait until ready
const api = await ApiPromise.create();
// Create an instance of our testing keyring
// If you're using ES6 module imports instead of require, just change this line to:
// const keyring = testKeyring();
const keyring = testKeyring.default();
// Get the nonce for the admin key
const nonce = await api.query.system.accountNonce(ALICE);
// Find the actual keypair in the keyring
const alicePair = keyring.getPair(ALICE);
// Create a new random recipient
const recipient = keyring.addFromSeed(randomAsU8a(32)).address;
console.log('Sending', AMOUNT, 'from', alicePair.address, 'to', recipient, 'with nonce', nonce.toString());
// Do the transfer and track the actual status
api.tx.balances
.transfer(recipient, AMOUNT)
.signAndSend(alicePair, { nonce }, ({ events = [], status }) => {
console.log('Transaction status:', status.type);
if (status.isFinalized) {
console.log('Completed at block hash', status.asFinalized.toHex());
console.log('Events:');
events.forEach(({ phase, event: { data, method, section } }) => {
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
});
function newSeed (seed: string | undefined | null, seedType: SeedType): string {
switch (seedType) {
case 'bip':
return mnemonicGenerate();
case 'dev':
return DEV_PHRASE;
default:
return seed || u8aToHex(randomAsU8a());
}
}
private makeBlockHeader (prevNumber: BN): Header {
const blockNumber = prevNumber.addn(1);
return createType(this.registry, 'Header', {
digest: {
logs: []
},
extrinsicsRoot: randomAsU8a(),
number: blockNumber,
parentHash: blockNumber.isZero()
? new Uint8Array(32)
: bnToU8a(prevNumber, 256, false),
stateRoot: bnToU8a(blockNumber, 256, false)
});
}
function newSeed (seed: string | undefined | null, seedType: SeedType): string {
switch (seedType) {
case 'bip':
return mnemonicGenerate();
case 'dev':
return DEV_PHRASE;
default:
return seed || u8aToHex(randomAsU8a());
}
}