Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function* accessWalletSaga(action) {
// debounce by 500ms
yield delay(500);
try {
const { payload } = action;
const privateKey: string = payload.privateKey;
const address = getAddressFromPrivateKey(privateKey);
const publicKey = getPubKeyFromPrivateKey(privateKey);
const { zilliqa } = yield select(getZilState);
zilliqa.wallet.addByPrivateKey(privateKey);
yield put({
type: consts.ACCESS_WALLET_SUCCEEDED,
payload: {
address,
publicKey,
privateKey
}
});
} catch (error) {
console.log(error);
yield put({ type: consts.ACCESS_WALLET_FAILED });
// These are set by the core protocol, and may vary per-chain.
// You can manually pack the bytes according to chain id and msg version.
// For more information: https://apidocs.zilliqa.com/?shell#getnetworkid
const chainId = 333; // chainId of the developer testnet
const msgVersion = 1; // current msgVersion
const VERSION = bytes.pack(chainId, msgVersion);
// Populate the wallet with an account
const privateKey =
'3375F915F3F9AE35E6B301B7670F53AD1A5BE15D8221EC7FD5E503F21D3450C8';
zilliqa.wallet.addByPrivateKey(privateKey);
const address = getAddressFromPrivateKey(privateKey);
console.log(`My account address is: ${address}`);
console.log(`My account bech32 address is: ${toBech32Address(address)}`);
async function testBlockchain() {
try {
// Get Balance
const balance = await zilliqa.blockchain.getBalance(address);
// Get Minimum Gas Price from blockchain
const minGasPrice = await zilliqa.blockchain.getMinimumGasPrice();
// Account balance (See note 1)
console.log(`Your account balance is:`);
console.log(balance.result);
console.log(`Current Minimum Gas Price: ${minGasPrice.result}`);
const myGasPrice = units.toQa('1000', units.Units.Li); // Gas Price that will be used by all transactions
console.log(`My Gas Price ${myGasPrice.toString()}`);
export function* accessWalletSaga(action) {
// debounce by 500ms
yield delay(500);
try {
const { payload } = action;
const privateKey: string = payload.privateKey;
const address = getAddressFromPrivateKey(privateKey);
const publicKey = getPubKeyFromPrivateKey(privateKey);
const { zilliqa } = yield select(getZilState);
zilliqa.wallet.addByPrivateKey(privateKey);
yield put({
type: consts.ACCESS_WALLET_SUCCEEDED,
payload: {
address,
publicKey,
privateKey
}
});
} catch (error) {
console.log(error);
yield put({ type: consts.ACCESS_WALLET_FAILED });
}
const decrypt = async (event) => {
try {
const { passphrase, keystoreV3 } = event.data;
const privateKey = await decryptPrivateKey(passphrase, keystoreV3);
// @ts-ignore
self.postMessage({ privateKey });
} catch (error) {
console.log(error);
// @ts-ignore
self.postMessage({ privateKey: undefined });
}
};
const encrypt = async (event) => {
try {
const { passphrase } = event.data;
const privateKey = schnorr.generatePrivateKey();
const keystoreJSON = await encryptPrivateKey('pbkdf2', privateKey, passphrase);
// @ts-ignore
self.postMessage({ keystoreJSON, privateKey });
} catch (error) {
console.log(error);
// @ts-ignore
self.postMessage({ keystoreJSON: undefined, privateKey: undefined });
}
};
const encrypt = async (event) => {
try {
const { passphrase } = event.data;
const privateKey = schnorr.generatePrivateKey();
const keystoreJSON = await encryptPrivateKey('pbkdf2', privateKey, passphrase);
// @ts-ignore
self.postMessage({ keystoreJSON, privateKey });
} catch (error) {
console.log(error);
// @ts-ignore
self.postMessage({ keystoreJSON: undefined, privateKey: undefined });
}
};
constructor(
factory: Contracts,
code?: string,
abi?: ABI,
address?: string,
init?: any,
state?: any,
) {
this.factory = factory;
this.provider = factory.provider;
this.signer = factory.signer;
// assume that we are accessing an existing contract
if (address) {
this.abi = abi;
this.address = normaliseAddress(address);
this.init = init;
this.state = state;
this.status = ContractStatus.Deployed;
} else {
// assume we're deploying
this.abi = abi;
this.code = code;
this.init = init;
this.status = ContractStatus.Initialised;
}
}
constructor(
params: TxParams,
provider: Provider,
status: TxStatus = TxStatus.Initialised,
toDS: boolean = false,
) {
// private members
this.version = params.version;
this.toAddr = normaliseAddress(params.toAddr);
this.nonce = params.nonce;
this.pubKey = params.pubKey;
this.amount = params.amount;
this.code = params.code || '';
this.data = params.data || '';
this.signature = params.signature;
this.gasPrice = params.gasPrice;
this.gasLimit = params.gasLimit;
this.receipt = params.receipt;
// public members
this.provider = provider;
this.status = status;
this.toDS = toDS;
this.blockConfirmation = 0;
this.eventEmitter = new EventEmitter();
}
sign(tx: Transaction): Promise {
if (tx.txParams && tx.txParams.pubKey) {
// attempt to find the address
const senderAddress = zcrypto.getAddressFromPublicKey(tx.txParams.pubKey);
if (!this.accounts[senderAddress]) {
throw new Error(
`Could not sign the transaction with ${senderAddress} as it does not exist`,
);
}
return this.signWith(tx, senderAddress);
}
if (!this.defaultAccount) {
throw new Error('This wallet has no default account.');
}
return this.signWith(tx, this.defaultAccount.address);
}
it('Should be able init wallet', async() => {
await accountControl.auth.setPassword(password)
currentAccount = await accountControl.initWallet(SEED)
expect(currentAccount.index).toBe(0)
expect(currentAccount.publicKey).toBeTruthy()
expect(verifyPrivateKey(currentAccount.privateKey)).toBeTruthy()
expect(currentAccount.balance).not.toBeNull()
expect(toChecksumAddress(currentAccount.address)).toBeTruthy()
})