Skip to content

Instantly share code, notes, and snippets.

@profbiss
Created April 28, 2023 11:16
Show Gist options
  • Save profbiss/aefc257c55695b5f9b2b2bf21484c381 to your computer and use it in GitHub Desktop.
Save profbiss/aefc257c55695b5f9b2b2bf21484c381 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ts-node
const { initWasm, TW, KeyStore } = require("@trustwallet/wallet-core");
const { Request } = require("node-fetch");
async function main() {
const start = new Date().getTime();
console.log(`Initializing Wasm...`);
const core = await initWasm();
const { CoinType, HexCoding, HDWallet, AnyAddress, AnySigner } = core;
console.log(`Done in ${new Date().getTime() - start} ms`);
const wallet = HDWallet.createWithMnemonic("*************", "")
const key = wallet.getKeyForCoin(CoinType.tron);
const pubKey = key.getPublicKeySecp256k1(false);
const address = AnyAddress.createWithPublicKey(pubKey, CoinType.tron);
const wallet2 = HDWallet.createWithMnemonic("*************", "")
const key2 = wallet2.getKeyForCoin(CoinType.tron);
const pubKey2 = key2.getPublicKeySecp256k1(false);
const address2 = AnyAddress.createWithPublicKey(pubKey2, CoinType.tron);
console.log();
const from = address.description()
const to = address2.description()
let now_block = await fetch("https://nile.trongrid.io/wallet/getnowblock", {method: "POST"}).
then(resp => resp.json()).catch(err => console.error(err))
console.log("NOW", now_block);
let options = {
method: 'POST',
headers: {accept: 'application/json', 'content-type': 'application/json'},
body: JSON.stringify({num: now_block.block_header.raw_data.number-10})
};
let prev_block = await fetch("https://nile.trongrid.io/wallet/getblockbynum", options).
then(resp => resp.json()).catch(err => console.error(err))
console.log("prev", prev_block)
let transaction = TW.Tron.Proto.Transaction.fromObject({
timestamp: prev_block.block_header.raw_data.timestamp,
expiration: prev_block.block_header.raw_data.timestamp + (10*60*60*1000),
feeLimit: 100000000,
transferAsset: TW.Tron.Proto.TransferAssetContract.fromObject({
assetName: 'TRX',
ownerAddress: from,
toAddress: to,
amount: 10000,
}),
blockHeader: TW.Tron.Proto.BlockHeader.fromObject({
txTrieRoot: HexCoding.decode(prev_block.block_header.raw_data.txTrieRoot),
parentHash: HexCoding.decode(prev_block.block_header.raw_data.parentHash),
witness_address: HexCoding.decode(prev_block.block_header.raw_data.witness_address),
number: prev_block.block_header.raw_data.number,
version: prev_block.block_header.raw_data.version,
timestamp: prev_block.block_header.raw_data.timestamp,
})
})
console.log("transaction", transaction);
let input = TW.Tron.Proto.SigningInput.fromObject({
transaction: transaction,
privateKey: key.data()
})
let result = AnySigner.sign(TW.Tron.Proto.SigningInput.encode(input).finish(), CoinType.tron)
console.log("result", result)
let output = TW.Tron.Proto.SigningOutput.decode(result)
console.log("output", output)
console.log("output_json", output.toJSON())
options = {
method: 'POST',
headers: {accept: 'application/json', 'content-type': 'application/json'},
body: output.toJSON().json
};
console.log("body",options.body);
fetch('https://nile.trongrid.io/wallet/broadcasttransaction', options)
.then(response => { console.log("souce resp", response); return response.json();})
.then(response => console.log("resp",response))
.catch(err => console.error("Err",err));
wallet.delete();
key.delete();
pubKey.delete();
address.delete();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment