I've learned a lot today, and I couldn't have gotten this far so quickly without this StackExchange. I've almost got sending worked out, but I can't for the life of me figure out why this is failing at the point of injection with a "unrevealed_key" error.
I am aware that I should decode the forged transaction and reverify the values to ensure the remote node hasn't tried to change my transaction, but I removed that bit from this post for the sake of simplicity.
const send = (from, to, amount, sk) => {
sotez.rpc.getHead()
.then(head => {
const operation = {
branch: head.hash,
contents: [{
kind: 'transaction',
source: from,
fee: '50000',
counter: '31204',
gas_limit: '10200',
storage_limit: '0',
amount: amount,
destination: to,
}],
}
sotez.tezos.forge(head, operation)
.then(unsigned => {
const binary = unsigned.opbytes
sotez.crypto.sign(binary, sk, '0x03')
.then(signed => {
operation.signature = signed.edsig
sotez.rpc.inject(operation, signed.sbytes)
.then(result => {
console.log(result)
})
})
})
})
}
send('tz1htPf3VPXrHBTX1E7y3tBteib6hA9Teosj', 'tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5', '1000000', 'edskS6KrT1G365PsuQiMVvPgZCS1CKTC5EFc7N...')
Returns: [{"kind":"branch","id":"proto.003-PsddFKi3.contract.unrevealed_key","contract":"tz1htPf3VPXrHBTX1E7y3tBteib6hA9Teosj"}]
Edit:
So I need to reveal this account first. I modified my send method to check if the account has been revealed before, and if not then add the revealing to the list of operations. Now I get {"kind":"permanent","id":"proto.003-PsddFKi3.operation.invalid_signature"}
.
const send = (from, to, amount, keys, revealed) => {
tezos.rpc.getHead()
.then(head => {
const operation = {
branch: head.hash,
contents: [{
kind: 'transaction',
source: from,
fee: '50000',
counter: '31205',
gas_limit: '10200',
storage_limit: '0',
amount: amount,
destination: to
}],
}
if (!revealed) {
operation.contents.unshift({
kind: 'reveal',
fee: '1269',
counter: '31204',
public_key: keys.pk,
source: from,
gas_limit: '10000',
storage_limit: '0',
})
}
tezos.tezos.forge(head, operation)
.then(unsigned => {
const binary = unsigned.opbytes
tezos.crypto.sign(binary, keys.sk, '0x03')
.then(signed => {
operation.signature = signed.edsig
tezos.rpc.inject(operation, signed.sbytes)
.then(result => {
console.log(result)
})
})
})
})
}
send('tz1htPf3VPXrHBTX1E7y3tBteib6hA9Teosj', 'tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5', '1000000', keys, false)
Edit2: This worked, using sendOperation
const send = (from, to, amount, keys) => {
sotez.rpc.getHead()
.then(head => {
const operation = {
kind: 'transaction',
source: from,
fee: '50000',
gas_limit: '10200',
storage_limit: '0',
amount: amount,
destination: to
}
const params = {
from,
operation,
keys
}
sotez.rpc.sendOperation({from, operation, keys})
.then(result => {
console.log(result)
})
})
}
send('tz1htPf3VPXrHBTX1E7y3tBteib6hA9Teosj', 'tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5', '1000000', keys)