eosjs icon indicating copy to clipboard operation
eosjs copied to clipboard

how to write a multiple asynchronous signature program use eosjs?

Open lingyiliu016 opened this issue 8 years ago • 35 comments

When making a trade on line, this trade often requires multiple signatures asynchronously. I read your eosjs example, but I didn't found any of multiple asynchronous signature example. I don't know how to write this program ,so give me a example about this topic ,please! Thank you very much! Let me describe scene of trade: Sellers sell goods on the market, when the buyer see the goods that he needs, he want to buy it. The market is similar to an online mall, Buyers and sellers can't see each other. Let me describe this scene in more detail. Tom want to sell his bicycle on an online mall, Kate want to buy this bicycle at web. but closIng this deal needs Tom's signature and Kate's signature. Firstly Kate initiated a request that need Tom's signature and Kate's signature, Kate then signed this deal. The website reminds Tom that this deal needs to be signed in 1 hour, Tom signed the deal 10 minutes later. Lastly Tom submitted this trade at web. so deal closed. Tom selled this bicycle and got 10 dollars, and Kate get the bicycle and payed 10 dollars. The above scenario has been implemented through the command line now. here is command: Firstly,cleos multisig propose Tom '[{"actor":"Kate","permission":"active"},{"actor":"Tom","permission":"active"}]' '[{"actor":"Kate","permission":"active"},{"actor":"Tom","permission":"active"}]' contract_setter trade '{"bicycleId":"1","buyer":"Kate","seller":"Tom"}' Kate -p Kate Secondly, cleos multisig approve Tom Kate '{"actor":"Kate","permission":"active"}' -p Kate Thirdly, cleos multisig approve Tom Kate '{"actor":"Tom","permission":"active"}' -p Kate Lastly, cleos multisig exec Tom Kate Tom

lingyiliu016 avatar Aug 03 '18 09:08 lingyiliu016

Have you looked at the multi sig contract?

chris-allnutt avatar Aug 03 '18 09:08 chris-allnutt

I found some clues in Eosjs's ReadMe, but there are no examples. 20180803170151 Can you give me some suggestion about this? You don't have to give a complete example, just give the key point. Help me please.

lingyiliu016 avatar Aug 03 '18 09:08 lingyiliu016

@chris-allnutt I know how to complete a transaction in command line, but I don't know how to write a program by eosjs. Can you give me some suggestion?

lingyiliu016 avatar Aug 03 '18 09:08 lingyiliu016

Multi sig has 3 parts.

  1. Transaction proposed.
  2. Party one approves transaction
  3. Party 2 approves transaction

It's in the main repo under contracts eosio.msig

chris-allnutt avatar Aug 03 '18 09:08 chris-allnutt

@chris-allnutt Are you referring to the code(C++) of eos?

lingyiliu016 avatar Aug 03 '18 09:08 lingyiliu016

Yes. You would have to look at the c++ contract, we don't provide any type of helper methods for this.

chris-allnutt avatar Aug 03 '18 09:08 chris-allnutt

@chris-allnutt I want to know how to develop by eosjs. I know the theory of muti signature asynchronous . But I don't know how to call interface of cleos multisig by eosjs.

lingyiliu016 avatar Aug 03 '18 09:08 lingyiliu016

@chris-allnutt Can I understand that eosjs calls musisig just like calling the action of a normal contract? for example: cleos multisig propose boy.acnt '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' genius.acnt borrow '{"tokenId":"1","borrower":"girl.acnt","lender":"boy.acnt"}' girl.acnt -p girl.acnt multisig is contract ,propose is an action of contract, boy.acnt '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' genius.acnt borrow '{"tokenId":"1","borrower":"girl.acnt","lender":"boy.acnt"}' girl.acnt -p girl.acnt is parameter of propose? It Maybe wrong! Here is the code. 20180803184520 It means that propose has no parameter. so what is boy.acnt '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' genius.acnt borrow '{"tokenId":"1","borrower":"girl.acnt","lender":"boy.acnt"}' girl.acnt -p girl.acnt?

lingyiliu016 avatar Aug 03 '18 10:08 lingyiliu016

Cleos has options like: --print-request .. cleos push action has a --json and --dont-broadcast switch but since your using multisig I think you'll have to just live with --print-request..

Turn that option on and you'll see everything cleos is doing for you and then you'll just need to put the transaction into eosjs..

jcalfee avatar Aug 04 '18 22:08 jcalfee

I did what you said! The following is the details. Firstly, I typed cleos --print-request multisig propose boy.acnt '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' genius.acnt borrow '{"tokenId":"1","borrower":"girl.acnt","lender":"boy.acnt"}' girl.acnt -p girl.acnt on the commend line terminal.( with --print-request) Secondly I got the information as bellow. 1111

lingyiliu016 avatar Aug 06 '18 11:08 lingyiliu016

Thirdly ,I read the eosio.msig.cpp file. Here is part of the content. 20180806193555 According to my understanding, calling msig propose is just calling a action of contract, So I just need to call the pose like calling a normal action. According to the above picture frame content, the action is propose, the parameter is proposer,proposal_name,requested and trx. is it right?just like the following picture(javascript) 20180806194238

lingyiliu016 avatar Aug 06 '18 11:08 lingyiliu016

I want to confirm whether my javascript content is right? give me an answer! Lastly I want to know the parameter of trx . As my understanding, the parameter of trx is the /v1/wallet/sign_transaction's parameters , is right? just as 20180806194858 so the right parameter of trx is 20180806195721

so the right program is 20180806195444

Is all right? To me, I need to calculate the parameter of trx.

lingyiliu016 avatar Aug 06 '18 11:08 lingyiliu016

As my understanding, the content of trx is /v1/wallet/sign_transaction, when I run javascript program , it means /v1/chain/push_transaction. is it right? @jcalfee So I just need to calculate ref_block_num、ref_block_prefix、actions.data. Your suggestion is important to me, thank you very much!

lingyiliu016 avatar Aug 06 '18 12:08 lingyiliu016

Turn off eosjs broadcast: false then go to send a transaction as usual and you'll receive a transaction back that you can use in your trx proposal. You can turn off sign: false too, after all it is a proposed transaction so I don't think you'll be signing it up-front. You may not need /v1/wallet/sign_transaction at all unless you actually want a server to sign something. Also, to make it easy, try to use forceActionDataHex: false so you can see the "data" as json instead of hex.

The source for parameters is the ABI. Look at the propose ABI action:

  "actions": [{
      "name": "propose",
      "type": "propose",
      "ricardian_contract": ""
},{

So that leads to the "propose" type (or struct): https://github.com/EOSIO/eos/blob/v1.1.3/contracts/eosio.msig/eosio.msig.abi#L56-L63

Also, this prints usage and example data in the console.log:

eos.contract('eosio.msig').then(msig => msig.propose())

You can run that command in the console here: https://jcalfee.github.io/eos.html

jcalfee avatar Aug 06 '18 13:08 jcalfee

@jcalfee Your suggestion gave me great help, thank you very much. I did what you said. Here are some specific details. This is the feedback of commandeos.contract('eosio.msig').then(msig => msig.propose()) at nodejs 2

I followed the prompts to write propose program. 3

when I write js program, I have some confusion. give me some suggestion How to understand "context_free_actions" and "actions" in "trx" ? What's the difference between "context_free_actions" and "actions"? Should I calculate "transaction_extensions"."data" or just null is ok?

lingyiliu016 avatar Aug 07 '18 03:08 lingyiliu016

When I run the program, error occurred! Details as follow! 20180807111345 20180807111404 "RangeError: Illegal offset: 0 <= 0 (+1) <= 0 action.data transaction.context_free_actions propose.trx action.data transaction.actions", I don't know what cause this error? how to solve it?

lingyiliu016 avatar Aug 07 '18 05:08 lingyiliu016

How to understand "context_free_actions" and "actions" in "trx" ? What's the difference between "context_free_actions" and "actions"? Should I calculate "transaction_extensions"."data" or just null is ok?

context_free_actions and context_free_actions can be removed or left as empty arrays..

jcalfee avatar Aug 07 '18 22:08 jcalfee

Do you means that "context_free_actions" can be empty arrays and "transaction_extensions" can be empty arrays. Just as follow:

"trx": {
        "expiration": "2018-08-09T10:02:39",
        "ref_block_num": 45743,
        "ref_block_prefix": 2268482204,
        "max_net_usage_words": 0,
        "max_cpu_usage_ms": 0,
        "delay_sec": 0,
        "context_free_actions": [],
        "actions": [
            {
                "account": "eosio.msig",
                "name": "propose",
                "authorization": [
                    {
                        "actor": "girl.acnt",
                        "permission": "active"
                    }
                ],
                "data": "01000000000000000400000000000000289a01000000000004454f53000000000000c8131910af630000007922033c3d18e68891e8a681e4b9b0e4bda0e982a3e58faae7b2bee781b5"
            }
        ],
        "transaction_extensions": []
    }

Is my understanding right?

lingyiliu016 avatar Aug 08 '18 03:08 lingyiliu016

I deployed a contract with the account market.acnt,which contains trade action . trade is executed by multiple asynchronous signature. Here are "proposer":"boy.acnt","proposal_name":"girl.acnt". the parameters of trade is

'{
    "id":"1",
    "tokenId":"4",
    "bidPrice":"10.5000 EOS",
    "seller":"girl.acnt",
    "buyer":"boy.acnt",
    "memo":"I want to buy the bicycle"
}'

Here is the shell command cleos multisig propose girl.acnt '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' '[{"actor":"girl.acnt","permission":"active"},{"actor":"boy.acnt","permission":"active"}]' market.acnt trade '{"id":"1","tokenId":"4","bidPrice":"10.5000 EOS","seller":"girl.acnt","buyer":"boy.acnt","memo":"I want to buy the bicycle"}' boy.acnt 100 -p boy.acnt I compute the data(bin) by v1/chain/abi_json_to_bin, here is the command: curl http://127.0.0.1:8888/v1/chain/abi_json_to_bin -X POST -d '{"code":"market.acnt", "action":"trade", "args":{"id": 1,"tokenId": 4,"bidPrice": "10.5000 EOS","seller": "girl.acnt","buyer": "boy.acnt","memo": "I want to buy the bicycle"}}' and get the data: "01000000000000000400000000000000289a01000000000004454f53000000000000c8131910af630000007922033c3d19492077616e7420746f20627579207468652062696379636c65" According to the definition of multisig propose abi and your suggestion, I write the program as follow:

eos.contract('eosio.msig').then(account => {
    account.propose({
    "proposer":"boy.acnt",
    "proposal_name":"girl.acnt",
    "requested":[
                    {"actor":"girl.acnt","permission":"active"},
                    {"actor":"boy.acnt","permission":"active"}
                ],
    "trx": {
        "expiration": "2018-08-11T15:28:57",
        "ref_block_num": 0,
        "ref_block_prefix": 0,
        "max_net_usage_words": 0,
        "max_cpu_usage_ms": 0,
        "delay_sec": 0,
        "context_free_actions": [],
        "actions": [
            {
                "account": "market.acnt",
                "name": "trade",
                "authorization": [
                    {
                        "actor": "girl.acnt",
                        "permission": "active"
                    },{
                        "actor": "boy.acnt",
                        "permission": "active"
                    }
                ],
                "data": "01000000000000000400000000000000289a01000000000004454f53000000000000c8131910af630000007922033c3d19492077616e7420746f20627579207468652062696379636c65"
            }
        ],
        "transaction_extensions": []
    }})
})

when I run the program by node.js, I got the error. The error is TypeError: Cannot read property 'abi' of undefined action.data transaction.actions propose.trx action.data transaction.actions. I think the format of propose is right, but node prompts that it cannot be readed by 'abi',why? I cannot understand. Here is the detail. give me some suggestion,please! 20180808115248

lingyiliu016 avatar Aug 08 '18 03:08 lingyiliu016

I considered eosio.multisig as an ordinary contract and call propose as calling an ordinary action, so at shell termial, I typed cleos push action eosio.msig propose propose_parameter1.json -p boy.acnt, it runs successfully 20180808151413 I can also review the table data. 20180808151721 This is the content of propose_parameter1.json

{
    "proposer":"boy.acnt",
    "proposal_name":"girl.acnt",
    "requested":[
                    {"actor":"girl.acnt","permission":"active"},
                    {"actor":"boy.acnt","permission":"active"}
                ],
    "trx": {
        "expiration": "2018-08-11T15:28:57",
        "ref_block_num": 0,
        "ref_block_prefix": 0,
        "max_net_usage_words": 0,
        "max_cpu_usage_ms": 0,
        "delay_sec": 0,
        "context_free_actions": [],
        "actions": [
            {
                "account": "market.acnt",
                "name": "trade",
                "authorization": [
                    {"actor": "girl.acnt","permission": "active"},
                    {"actor": "boy.acnt","permission": "active"}
                ],
                "data": "01000000000000000400000000000000289a01000000000004454f53000000000000c8131910af630000007922033c3d19492077616e7420746f20627579207468652062696379636c65"
            }
        ],
        "transaction_extensions": []
    }
}

here is eosjs function

eos.contract('eosio.msig').then(account => {
    account.propose({
    "proposer":"boy.acnt",
    "proposal_name":"girl.acnt",
    "requested":[
                    {"actor":"girl.acnt","permission":"active"},
                    {"actor":"boy.acnt","permission":"active"}
                ],
    "trx": {
        "expiration": "2018-08-11T15:28:57",
        "ref_block_num": 0,
        "ref_block_prefix": 0,
        "max_net_usage_words": 0,
        "max_cpu_usage_ms": 0,
        "delay_sec": 0,
        "context_free_actions": [],
        "actions": [
            {
                "account": "market.acnt",
                "name": "trade",
                "authorization": [
                    {"actor": "girl.acnt","permission": "active"},
                    {"actor": "boy.acnt","permission": "active"}
                ],
                "data": "01000000000000000400000000000000289a01000000000004454f53000000000000c8131910af630000007922033c3d19492077616e7420746f20627579207468652062696379636c65"
            }
        ],
        "transaction_extensions": []
    }},options)
})

@jcalfee,one is executed successfully and another in eosjs runs failed! I think complicated parameters in propose cause this error! So maybe this is eosjs mechanism that cannot deal with complicated parameters . I hope you can give me the solution is eosjs,give me a example (call propose ) that can run successfully!

lingyiliu016 avatar Aug 08 '18 07:08 lingyiliu016