Secure the Solana Ecosystem (5) — Multi-Sig
In the previous blog, we discussed the account validation, which is important to access control, in Solana programs. However, for a decentralized DApp and to protect the potential private key leakage, multi-sig is very important. In this post, we will introduce the implementation of the Multi-Sig.
2. Concept of Muti-Sig
Multi-Sig is a digital signature scheme that allows a group of users to sign a single transaction. The transaction could be a call to a privilege function (e.g., mint), a fund transfer instruction, etc. The mechanism of multi-sig is that given
n parties with their own private keys, at least
m of the private keys must sign to execute the transaction. This can make the funds in DeFi much safe and can protect the potential risk like private key leakage and rug pull.
3. Usage of Multi-Sig
In Solana, there is a multi-sig program from Serum, which is quite similar to the logic of the multi-sig developed by OpenZeppelin. This allows multiple users to sign a transaction fully on-chain. However, if you can collect all the required signatures off-chain, the process can be easier. To illustrate it, we add the multi-sig functionality in the test code from the last post. All the test code can be found here.
3.1 Code Review
In the last introduced project, we replace the admin of the
config account with a
multisig account. In this case, to perform the instruction
Unlock, the client will have to include enough signatures of the valid owners in the transaction.
We add a struct named
Multisig consisting of four attributes. They are the number of signatures required to execute the instruction, the total number of valid signers, the status of the account (whether it's initialized or not), and the array of valid signers' public keys.
Correspondingly, we add the instruction
instruction.rs as well. Note that it receives the argument
u8) from the client side. This value will be feed into the function
InitializeMultisig() receives the created accounts and the value
m. To prevent the account from being re-initialized by malicious users, we validate whether the account has already been initialized (line 385-388). After that, we init the multi-sig account by assigning the value
n and the public key of the signers.
Lock(), we check whether the admin account of the
config account is a multi-sig one. If so, it will validate and count the signatures passed in (line 151 to 158). Once the number of valid signatures reaches or exceeds the required amount, doors will be locked (line 163 to 168). The implementation of the multi-sig in the function
Unlock() is similar to
We deployed the program on testnet and it can be find in the following link.
3.2 Send Transaction
keys includes four accounts. They are the multisig account and three valid signer's accounts. Note that one of the signers is also the fee payer of the transaction. We set the value of
m to 2, which means two of the three signers' signatures is required to execute privilege functions.
We also set the admin of the
config account as the
multisig account by passing the public key of the
multisig account in the instruction data.
lock(), in addition to the
config account (line 410), we pass in the
multisig account (line 411), and two (at least) valid signers' accounts (line 412 - line 413). We set the attribute
isSigner to be true for the signers. Again, the logic of the function
unlock()is similar to it.
All the test transactions are listed below. The whole process is
3.3 Test Transaction
To test if the multi-sig really works, we modify the script of the client.
As shown above, we only pass in one signer account, and provide only one valid signature.
We noticed that the door cannot be unlocked due to insufficient signatures.
In this article, we introduce the simple implementation of the multi-sig in Solana. It’s for the scenario that you are able to collect the signatures from multiple users off-chain. The scenario that the transactions are required to be signed fully on-chain will be introduced later. Keep following, and we will share more in the upcoming posts.