[support]Unable to use ecc.sign to get correct signatures to push transaction
Hi, I am able to use rpc api to sign transaction and push transaction for a transfer but unable to use eosjs to do the same thing. It appears to me that the correct signature should be the result of a bunch of parameters like expiration, ref_block_num, actions and so on. However in eosjs, ecc.sign only requires private key and a data and returns the signature which I cannot use for pushing transactions, it will just throw exception below.
{
"code": 500,
"message": "Internal Service Error",
"error": {
"code": 3090003,
"name": "unsatisfied_authorization",
"what": "provided keys, permissions, and delays do not satisfy declared authorizations",
"details": [
{
"message": "transaction declares authority '{\"actor\":\"tester\",\"permission\":\"active\"}', but does not have signatures for it under a provided delay of 0 ms",
"file": "authorization_manager.cpp",
"line_number": 405,
"method": "check_authorization"
}
]
}
}
I use rpc api sign_transaction with the parameters below:
[
{
"expiration":"2018-05-22T07:07:35.909",
"ref_block_num":27384,
"ref_block_prefix":762384622,
"max_net_usage_words":0,
"max_cpu_usage_ms":0,
"delay_sec":0,
"context_free_actions":[
],
"actions":[
{
"account":"eosio.token",
"name":"transfer",
"authorization":[
{
"actor":"tester",
"permission":"active"
}
],
"data":"000000005c95b1ca00000000007015d6102700000000000004454f5300000000087472616e73666572"
}
],
"transaction_extensions":[
],
"signatures":[
],
"context_free_data":[
]
},
[
"EOS6vUxEV4e8TjEc33Fyq9FfZvgjEf32iBEPguwePcthkBaRdhcgw"
],
"0000000000000000000000000000000000000000000000000000000000000000"
]
And use ecc.sign(data, privatekey), data is as below:
{
"expiration":"2018-05-22T07:07:35.909",
"ref_block_num":27384,
"ref_block_prefix":762384622,
"max_net_usage_words":0,
"max_cpu_usage_ms":0,
"delay_sec":0,
"context_free_actions":[
],
"actions":[
{
"account":"eosio.token",
"name":"transfer",
"authorization":[
{
"actor":"tester",
"permission":"active"
}
],
"data":"000000005c95b1ca00000000007015d6102700000000000004454f5300000000087472616e73666572"
}
],
"transaction_extensions":[
],
"signatures":[
],
"context_free_data":[
]
}
I compared the signatures I got from the two methods above but they are totally different. What format of data should I give to ecc.sign() to get the exact same signature as rpc api?
I am also trying the way below to broadcast a transaction, it appears to me that the expiration is set to in one minute, is there a way to manually set expiration time and is is necessary? I am not sure if one minute is enough to deal with the crowded network. Still want to know the answer to my first comment. Thanks.
let privateKey = '5J9BXswXxGTcW6UMbPLUeRYCRsDUXb5zMMBJSCjqbT6G8majHEj'
let keyProvider = [privateKey]
const eos = eoslib.Localnet({keyProvider})
let result = await eos.transaction({
actions: [
{
account: 'eosio.token',
name: 'transfer',
authorization: [{
actor: 'tester',
permission: 'active'
}],
data: {
from: 'tester',
to: 'user',
quantity: '1.0000 EOS',
memo: ''
}
}
]
})
- you can turn broadcast off and you'll get a signed transaction back from eosjs .. The signature depends on the version too, recently an additional field was added to the signed digest. If you match eosjs to nodeosd in the version table (in the README) then the signature should be valid.
- See https://github.com/EOSIO/eosjs#configuration
expireInSeconds
I have the same issue as the OP. I get a signed transaction if I turn broadcast off, but then how do I use eosjs to push the transaction?
await eos.pushTransaction({
compression: 'none',
transaction: tr,
signatures: sigs
})
@cindyssun5 it is tricky.. I'm working on a full example..
@jcalfee - Thanks, I think my confusion is just whether I should be using eos.pushTransaction or eos.transfer to broadcast transaction onto the blockchain.
This is probably the most common use-case and is working for me in this test environment:
chainId = null
keyProvider = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'
Eos=require('eosjs'); 1
(async function() {
// ONLINE
// Prepare headers
expireInSeconds = 60 * 60 // 1 hour
eos = Eos(/* {httpEndpoint: 'https://..'} */)
info = await eos.getInfo({})
chainDate = new Date(info.head_block_time + 'Z')
expiration = new Date(chainDate.getTime() + expireInSeconds * 1000)
expiration = expiration.toISOString().split('.')[0]
block = await eos.getBlock(info.last_irreversible_block_num)
headers = {
expiration,
ref_block_num: info.last_irreversible_block_num & 0xFFFF,
ref_block_prefix: block.ref_block_prefix
}
// OFFLINE (bring `headers`)
// All keys in keyProvider will sign.
eos = Eos({
httpEndpoint: null, chainId, keyProvider,
transactionHeaders: (exp, cb) => cb(null, headers)
})
transfer = await eos.transfer('inita', 'initb', '1.0000 SYS', '')
transferTransaction = transfer.transaction
// ONLINE (bring `transferTransaction`)
eos = Eos(/* {httpEndpoint: 'https://..'} */)
processedTransaction = await eos.pushTransaction(transferTransaction)
console.log('processedTransaction', processedTransaction)
})().catch(e => console.error(e)).then(r => result = r)
Does anyone has a solution for the initial question of the OP?
@cindyssun5 like this:
chain = {
mainnet: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
testnet: '038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca',
sysnet: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f'
}
/**
Other httpEndpoint's: https://www.eosdocs.io/resources/apiendpoints
*/
eos = Eos({
httpEndpoint: 'http://127.0.0.1:8888',
chainId: chain.sysnet,
keyProvider: privateKey
})
Then, unless you turn broadcast off, all function calls create and sign transactions.