Skip to content

Instantly share code, notes, and snippets.

@Thegaram

Thegaram/index.js

Created Sep 18, 2020
Embed
What would you like to do?
cfx-lecture-3-examples
const Account = require('js-conflux-sdk/src/account');
const assert = require('assert');
const { Conflux, Drip, Message, util } = require('js-conflux-sdk');
async function main() {
// ---- generate random private key ----
const privkey = util.sign.randomPrivateKey();
console.log('private key:', util.format.hex(privkey));
// ---- derive public key ----
const pubkey = util.sign.privateKeyToPublicKey(privkey);
console.log('public key:', util.format.hex(pubkey));
// ---- derive address ----
const address = util.sign.publicKeyToAddress(pubkey);
console.log('address:', util.format.hex(address));
// ---- sign message ----
const msg = new Message('Hello! Sincerely, Peter');
msg.sign(privkey);
console.log('signed message:', msg)
// ---- check signature ----
let recovered = Message.recover(msg.signature, msg.hash)
assert.strictEqual(recovered, util.format.hex(pubkey))
// ---- detect tampering using signature ----
msg.message = 'Hello! Sincerely, ----- Hacker';
recovered = Message.recover(msg.signature, msg.hash)
assert.notStrictEqual(recovered, util.format.hex(pubkey))
// ---- create and sign transaction ----
const local_account = new Account({ privateKey: util.format.hex(privkey) });
const tx = local_account.signTransaction({
from: util.format.hex(address),
to: util.format.hex(address),
value: Drip.fromCFX(10).toDrip(),
nonce: '0x00',
gasPrice: '0x00',
gas: '0x00',
storageLimit: '0x00',
epochHeight: '0x00',
});
console.log('transaction:', tx);
console.log('transaction hash:', tx.hash);
// ---- send transaction ----
const conflux = new Conflux({ url: 'http://mainnet-jsonrpc.conflux-chain.org:12537' });
// TODO: import your Conflux Portal wallet
// follow: Account Details, Export Private Key
// make sure to add a '0x' prefix to your private key
// make sure your account has sufficient balance
const account = conflux.Account({ privateKey: util.format.hex(privkey) });
// set recipient
const to = '0x145834072064DcD9B931237B5aEe217c241e3644';
// set value to send
const value = Drip.fromCFX(10).toDrip();
const epochHeight = await conflux.getEpochNumber();
const estimate = await conflux.estimateGasAndCollateral({ to, value });
const gasPrice = await conflux.getGasPrice();
const nonce = await conflux.getNextNonce(account.address);
const status = await conflux.getStatus();
const txHash = await account.sendTransaction({
to,
value,
nonce,
gasPrice,
gas: estimate.gasUsed,
storageLimit: estimate.storageCollateralized,
chainId: status.chainId,
data: null,
epochHeight,
});
console.log('sending transaction, hash:', txHash);
// wait 10 seconds
await new Promise(resolve => setTimeout(resolve, 10 * 1000));
// get transaction
const transaction = await conflux.getTransactionByHash(txHash);
console.log('transaction:', transaction);
// wait 10 seconds
await new Promise(resolve => setTimeout(resolve, 10 * 1000));
// get receipt
const receipt = await conflux.getTransactionReceipt(txHash);
console.log('receipt', receipt);
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.