Getting start with Algorand using Python Day-7: Multisig account concept with example and use case, how to create a transaction group and likely error with description
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.
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. 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
- Get suggested parameters.
generateAccounts()function which launches 3 new accounts from
generateaccountmodule, stores the result in
16does this jobs and we only print the result to the console from
reqis number of signatories to the Multisig wallet.
21is required by inbuilt
Multisigclass that initiates
- From the 3 accounts we created from
16, we assigned first account as Raymond’s account address and its private key in
- In line
27, we registered both parties’ addresses as the required number of signatories.
- They will be able to withdraw from
spendAccountonly if both parties signatures are collected from
38, enjoined in
36and broadcast in
39where 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
35specifies the structure of fund withdrawal from
savingAlcAddr(multisig account) to
spendAccountwith 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.
- Moved transaction parameters to a function
create_transactionin a new file
sendtransaction.pyfile, created three different transactions from three accounts generated from
create_transaction()we imported – lines
14, grouped them together –
16, the grouping is done using a list of transaction IDs generated from hash of calculating the trio.
23signs each of transactions with paired private keys.
- Then we broadcast the group to the network enclosed as array of objects in a list.
Here we execute the files. Try it by copying the full codes from github. Please leave comment if you have any concern.
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.
Happy coding. stay tuned for more tutorials. Find more resources