Skip to content

Instantly share code, notes, and snippets.

@laalaguer
Created January 13, 2020 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laalaguer/1a7d9f9e0993c83ffcc84b766c3498ae to your computer and use it in GitHub Desktop.
Save laalaguer/1a7d9f9e0993c83ffcc84b766c3498ae to your computer and use it in GitHub Desktop.
User's side Snippet
/**
* Attempt to commit a transaction to a contract.
*
* With a hefty wallet as "sponsor" + an empty wallet as "sender".
*
*/
const fetch = require("node-fetch");
const cry = require('thor-devkit/dist/cry')
const Transaction = require('thor-devkit/dist/transaction').Transaction
async function getSponosrSignature(sender, txBody) {
const url = 'http://localhost:3000/'
const response = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'sender': sender,
'txBody': txBody
})
})
const r = await response.json()
return r['sponsor_signature']
}
// run it!
(async function () {
// User private key.
const originPriv = Buffer.from(
'2a0cbfe49ea7c18e89b87be4237e1717823fc16b52dc02e91fb30af122fba9b3',
'hex'
)
// User public address: 0x881Ab2380017870C49a9A114806C05F3CFE406e2
const originAddress = cry.publicKeyToAddress(cry.secp256k1.derivePublicKey(originPriv))
// Construct transaction body.
const txBody = {
// Test-net: 0x27, Main-net: 0x4a.
chainTag: 0x27,
// After which block this tx should happen?
// 16 characters of block ID.
blockRef: '0x004984e1064ed410',
// Expires after 30 days since blockRef.
expiration: 30 * 8640,
// Call the contract method "increaseAmount"
clauses: [{
to: '0x6d48628bb5bf20e5b4e591c948e0394e0d5bb078',
value: 0,
data: '0x74f667c4'
}],
gasPriceCoef: 0,
gas: 50000,
dependsOn: null,
nonce: '0xa3b6232f', // Random number
// Must include this field to activate VIP-191.
reserved: {
features: 1
}
}
// Construct a transaction.
const tx = new Transaction(txBody)
// Construct the hash for signing.
const originHash = tx.signingHash()
// Construct the user signature.
const originSignature = cry.secp256k1.sign(originHash, originPriv)
// Fetch the sponsor signature.
const sponsorSignature = await getSponosrSignature(
'0x'+originAddress.toString('hex'),
txBody
)
// Compose a combined signature of user + sponsor.
const sig = Buffer.concat([
originSignature,
Buffer.from(sponsorSignature, 'hex')
])
// Mount on the combined signature.
tx.signature = sig
// Convert the tx to raw format.
const rawTx = '0x' + tx.encode().toString('hex')
// Submit the raw transaction by hand to the test-net.
const url = 'https://sync-testnet.vechain.org/transactions'
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({'raw': rawTx})
}).then(response => {
response.text().then(r => {console.log(r)})
}).catch(err => {
console.log('err', err)
})
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment