Skip to content

Instantly share code, notes, and snippets.

@pirumu
Created March 26, 2021 10:10
Show Gist options
  • Save pirumu/85063022e416f9ef02866086c8b0f482 to your computer and use it in GitHub Desktop.
Save pirumu/85063022e416f9ef02866086c8b0f482 to your computer and use it in GitHub Desktop.
Cancel ETH and ERC20 token TRX
const Web3 = require("web3");
const bip39 = require("bip39");
const hdkey = require("ethereumjs-wallet/hdkey");
const EncryptData = require("./ek");
const BN = require("bignumber.js");
const TX = require("ethereumjs-tx");
const UNIT_GWEI = 1000000000;
const RPCMainNetNode = "https://mainnet.infura.io/v3/key";
const initializationVector = "initializationVector";
const key = "key";
const web3 = new Web3(new Web3.providers.HttpProvider(RPCMainNetNode));
const cancelTrxHash = "";
const cipherText = "";
const mnemonic = new EncryptData(initializationVector, key).decrypt(cipherText);
console.log(mnemonic);
(async () => {
//BIP 39 support
seed = bip39.mnemonicToSeedSync(mnemonic);
let value = null;
if (typeof seed === "string") {
value = Buffer.from(seed);
} else if (Buffer.isBuffer(seed)) {
value = seed;
} else {
throw new Error("Seed must be Buffer or string");
}
let hdWallet = hdkey.fromMasterSeed(value);
//BIP 44 support
hdWallet = hdWallet.derivePath("m/44'/60'/0'/0/0");
const privateKey = hdWallet.getWallet().getPrivateKey();
const txCount = 0;
const sender = "";
const gwei = 50;
const network = "mainnet";
const gasPrice = new BN(UNIT_GWEI).multipliedBy(gwei);
const tx = new TX.Transaction(
{
nonce: web3.utils.toHex(txCount),
from: sender,
gasLimit: web3.utils.toHex("21000"),
gasPrice: web3.utils.toHex(`${gasPrice}`),
to: sender,
value: web3.utils.toHex("0"),
},
{ chain: network }
);
tx.sign(privateKey);
const serializedTx = tx.serialize();
const raw = "0x" + serializedTx.toString("hex");
// console.log(raw); return;
const tran = web3.eth.sendSignedTransaction(raw);
const transactionPromiseBuilder = () =>
new Promise((resolve, reject) => {
tran.on("error", (err) => reject(err));
tran.on("transactionHash", (hash) => resolve(hash));
});
const result = await transactionPromiseBuilder();
return result;
})();
"use strict";
const crypto = require('crypto');
class EncryptData {
constructor(initializationVector,key) {
// Initialization vector
this.resizedIV = Buffer.allocUnsafe(16);
const iv = crypto
.createHash("sha256")
.update(initializationVector)
.digest();
iv.copy(this.resizedIV);
// key
this.key = crypto
.createHash("sha256")
.update(key)
.digest();
}
/**
* Encrypt
*
* @param {*} mnemonic string in Japanese
* @returns encrypted string
* @memberof EncryptBusiness
*/
encrypt(mnemonic) {
const cipher = crypto.createCipheriv("aes256", this.key, this.resizedIV);
const msg = [];
// Convert mnemonic String to Buffer
const buffer = Buffer.from(mnemonic, "utf-8");
// Convert Buffer mnemonic to hex String -> encrypt
const hexString = buffer.toString("hex");
// Encryption Starting
msg.push(cipher.update(hexString, "binary", "hex"));
msg.push(cipher.final("hex"));
// Encryption Ended
return msg.join("");
}
/**
* Decrypt
*
* @param {*} ciphertext
* @returns mnemonic string in Japanese
* @memberof EncryptBusiness
*/
decrypt(ciphertext) {
const decipher = crypto.createDecipheriv("aes256", this.key, this.resizedIV);
const msg = [];
// Decryption Starting
msg.push(decipher.update(ciphertext, "hex", "binary"));
msg.push(decipher.final("binary"));
const rawString = msg.join("");
// Decryption Ended
const buffer = Buffer.from(rawString, "hex");
// Get mnemonic string
const mnemonic = buffer.toString("utf-8");
return mnemonic;
}
}
module.exports = EncryptData;
{
"dependencies": {
"bignumber.js": "^9.0.0",
"crypto": "^1.0.1",
"ethereumjs-tx": "^2.1.2",
"ethereumjs-wallet": "^0.6.3",
"web3": "^1.2.11",
"bip39": "^3.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment