GSWPUA-Day-7: Multisig account concept with example and use case, how to create a transaction group and likely error with description

in #algorand4 years ago (edited)
Before now, following previous tutorial, we have established connection(using REST APIs), generate
accounts(private key, address and/or public key conversion), transfer token between accounts and query the network for account information. Let's try some new stuffs.

src


==>Previously on<==

Srs 1 Srs 2 Srs 3
Srs 4 Srs 5 Srs 6


Multi-Signature Account

A fictional case study

A year ago, Raymond walked into a bank subsequent to a discussion he had with his girlfriend. They concluded to have a joint account and a separate account for daily spending. They are two different human and sexes with different mindsets and ways of thinking. Even though they loved each other yet, instincts reminded them of the dynamic nature of man. One of them could grow naughty overnight or become wild suddenly, hence the decision to abandon sentiments to approach a bank for a joint account as they planned towards the future.

Disappointingly, bank would not grant their request unless they would provide a certificate indicating legal wedding as a couple. But they were only trying to pool resources towards tying the nuptial nut. In nutshell, they couldn't find a way around it. She claimed to loved him but would not risk saving to his account. Same with him. Even if he had succumb to saving to her account, The breakup would have left him with lasting pain. But what if that happened?
On Algorand, several real-life issues have their paired solutions. MultiSig wallet would have been their best solution if awareness had reached them.


Experimenting real-life use case with Algorand solution

Last month, Raymond met Bob who is a smart contract developer on Algorand. He told him his past ordeal and currently experiencing same with his fiancee. Bob introduced him to Algorand solution. Now he is happy to want to implement this solution with his fiancee. They have agreed to create a multisig account with the following rules:

  • An account for savings.
  • Either of them can transfer fund to the savings account without fear that the other party will spend or withdraw without notifying the other.
  • None of them should be able to spend on trivialities.
  • They want a spending account
  • Two parties as signatures for approving withdrawal or any form of transfer.

Ok great, I understand what they need perfectly. Let's get to work.


Back in our code editor, I have created another file named multiSig.py. Multisig account have some resemblances with creating an individual account but its uniqueness is in 100% obedience to the predefined set of rules. We are going to integrate every line of instructions based on our client's demand.

image.png

Using our usual default account as Raymond's fiancee's account. We created an account for her and set it to wifeAddress, store her privateKey as wifePrivateKey. Notice we use a prefix wife right? Cool. This is because we are wishing them a happy married life in advance. In actual coding, you don't want to have the knowledge of her private keys. You only need to write the instructions in a way that will ask her as input.

  • Next we we call our already established connectToNetwork() function from connect.py file.
  • Get suggested parameters.
  • Call generateAccounts() function which launches 3 new accounts from generateaccount module, stores the result in accounts dictionary. Line 16 does this jobs and we only print the result to the console from 17.
  • req is number of signatories to the Multisig wallet.
  • 21 is required by inbuilt Multisig class that initiates multisig function.
  • From the 3 accounts we created from 16, we assigned first account as Raymond's account address and its private key in 23.

image.png

  • In line 27, we registered both parties' addresses as the required number of signatories.
  • They will be able to withdraw from savingsAccount to spendAccount only if both parties signatures are collected from 37 and 38, enjoined in 36 and broadcast in 39 where the connection is available.
  • When multisig transaction is initiated, a wallet is generate automatically by the network and its private key does not exist. Therefore, none of the parties can lobby for the key since it can be used to move funds away from the account. Isn't that cool? In 29, we are only printing the multisig account information, also, address which we represent as the savings account as the instruction says in 33.
  • 35 specifies the structure of fund withdrawal from savingAlcAddr (multisig account) to spendAccount with the amount to withdraw waiting to be signed

Interestingly, spending on trivialities will be as result of consensus ad idem. They can decide to save toward paying house rent, starting a business or project or do meaningful things. Business partners could see this as a way of resolving inherent partnership issues that the native or borrowed laws struggle to resolve. We didn't demand for any certificate except a good consideration(payment). The protocol removes the middleman, acts as trustee and it can be trusted with regards to what it is programmed to do.


How To Create Transaction Group.

Aside of creating individual account and multisig accounts, there are cases where working with transaction groups can be very useful and helpful. This model is often implemented in exchanges.

We will reuse most of our modules but for the pythonTutorial.py file. I created a file - pythonMain_day7.py to execute it.


What changed!.

  • Moved transaction parameters to a function create_transaction in a new file createTransaction.py

image.png


  • In sendtransaction.py file, created three different transactions from three accounts generated from create_transaction() we imported - lines 12 to 14, grouped them together - 17 to 19.

  • 16, the grouping is done using a list of transaction IDs generated from hash of calculating the trio.

  • 21 to 23 signs each of transactions with paired private keys.

  • Then we broadcast the group to the network enclosed as array of objects in a list.

image.png


Here we execute the files. Try it by copying the full codes from github. Please leave comment if you have any concern.

image.png


Output

image.png


Likely Error You Might Encounter

While practicing, you may like experience this error. Its an indication you are trying to send token from a newly generated account or account with no enough balance. You either did not give enough room for confirmation before printing out the current account status like I did here. Solution is to create enough room for confirmation which is usually around 5 seconds.

image.png


Happy coding. stay tuned for more tutorials.

Find more resources