CreateAndSignD20TokenTx
code:CreateAndSignD20TokenTx.js
const EthUtil = require('ethereumjs-util')
const Web3 = require('web3')
const WEB3_PROVIDER = 'wss://ropsten.infura.io/ws'
// トークンのABI
const TOKEN_ABI = [{"constant":true,"inputs":[],"name":"name","outputs":{"name":"","type":"string"},"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":{"name":"spender","type":"address"},{"name":"value","type":"uint256"},"name":"approve","outputs":{"name":"","type":"bool"},"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":{"name":"","type":"uint256"},"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":{"name":"from","type":"address"},{"name":"to","type":"address"},{"name":"value","type":"uint256"},"name":"transferFrom","outputs":{"name":"","type":"bool"},"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"rate","outputs":{"name":"","type":"uint256"},"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":{"name":"amount","type":"uint256"},"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":{"name":"","type":"uint8"},"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":{"name":"to","type":"address"},"name":"mint","outputs":{"name":"","type":"bool"},"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":{"name":"owner","type":"address"},"name":"balanceOf","outputs":{"name":"","type":"uint256"},"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":{"name":"delegatedTx","type":"bytes"},{"name":"sig","type":"bytes"},"name":"delegatedTransfer","outputs":{"name":"","type":"bool"},"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":{"name":"","type":"string"},"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":{"name":"to","type":"address"},{"name":"value","type":"uint256"},"name":"transfer","outputs":{"name":"","type":"bool"},"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"contractOwner","outputs":{"name":"","type":"address"},"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":{"name":"owner","type":"address"},{"name":"spender","type":"address"},"name":"allowance","outputs":{"name":"","type":"uint256"},"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":{"name":"owner","type":"address"},"name":"nonceOf","outputs":{"name":"","type":"uint256"},"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"},"name":"Transfer","type":"event"},{"anonymous":false,"inputs":{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"},"name":"Approval","type":"event"}] // トークンのコントラクトアドレス
const TOKEN_ADDRESS = '0x5c66afc60fa1fa2eeb5ea0ffae6236933571a9e0'
// 送金するトークンの量
const VALUE = Web3.utils.toWei('15')
// トークン送金先アドレス
const TO = '0x2890228d4478e2c3b0ebf5a38479e3396c1d6074'
// トークン送金元のアドレスと秘密鍵
const ADDRESS = '0x89c24a88bad4abe0a4f5b2eb5a86db1fb323832c'
const PRIVATE_KEY_STRING = '0x61ce8b95ca5fd6f55cd97ac60817777bdf64f1670e903758ce53efc32c3dffeb'
// web3を初期化
const web3 = new Web3(WEB3_PROVIDER)
// トークンのスマートコントラクトを初期化
const token = new web3.eth.Contract(TOKEN_ABI, TOKEN_ADDRESS)
// トークンを送信するためのトークントランザクションデータを作成
function createTokenTx(nonce, value, to, tokenAddress) {
const hexNonce = Web3.utils.padLeft(Web3.utils.numberToHex(nonce), 64)
const hexValue = Web3.utils.padLeft(Web3.utils.numberToHex(value), 64)
}
// ハッシュに署名をして独自フォーマットに整形します。
function signTokenTx(hash, privateKey) {
const sig = EthUtil.ecsign(hash, privateKey)
const r = EthUtil.bufferToHex(sig.r)
const s = EthUtil.bufferToHex(sig.s)
const v = Web3.utils.numberToHex(sig.v)
}
async function main() {
// トークンの nonce を取得
let tokenNonce = await token.methods.nonceOf(ADDRESS).call()
// トークントランザクションデータの作成
const tokenTx = createTokenTx(tokenNonce, VALUE, TO, TOKEN_ADDRESS)
console.log('tokenTx', tokenTx)
// トークントランザクションの Keccak256 ハッシュを取得
const hash = EthUtil.sha3(tokenTx)
// 署名を取得
const tokenSig = signTokenTx(hash, EthUtil.toBuffer(PRIVATE_KEY_STRING))
console.log('tokenSig', tokenSig)
}
main()
説明