smartcontract: support creation of majority multisig scripts

This commit is contained in:
Evgenii Stratonikov 2020-08-21 09:30:07 +03:00
parent e0104679b5
commit 51cebe9e47
2 changed files with 53 additions and 0 deletions

View file

@ -44,3 +44,11 @@ func CreateDefaultMultiSigRedeemScript(publicKeys keys.PublicKeys) ([]byte, erro
m := n - (n-1)/3 m := n - (n-1)/3
return CreateMultiSigRedeemScript(m, publicKeys) return CreateMultiSigRedeemScript(m, publicKeys)
} }
// CreateMajorityMultiSigRedeemScript creates an "m out of n" type verification script
// using publicKeys length with m set to majority.
func CreateMajorityMultiSigRedeemScript(publicKeys keys.PublicKeys) ([]byte, error) {
n := len(publicKeys)
m := n - (n-1)/2
return CreateMultiSigRedeemScript(m, publicKeys)
}

View file

@ -83,3 +83,48 @@ func TestCreateDefaultMultiSigRedeemScript(t *testing.T) {
} }
checkM(7) checkM(7)
} }
func TestCreateMajorityMultiSigRedeemScript(t *testing.T) {
var validators = make([]*keys.PublicKey, 0)
var addKey = func() {
key, err := keys.NewPrivateKey()
require.NoError(t, err)
validators = append(validators, key.PublicKey())
}
var checkM = func(m int) {
validScript, err := CreateMultiSigRedeemScript(m, validators)
require.NoError(t, err)
defaultScript, err := CreateMajorityMultiSigRedeemScript(validators)
require.NoError(t, err)
require.Equal(t, validScript, defaultScript)
}
// 1 out of 1
addKey()
checkM(1)
// 2 out of 2
addKey()
checkM(2)
// 3 out of 4
addKey()
addKey()
checkM(3)
// 4 out of 6
addKey()
addKey()
checkM(4)
// 5 out of 8
addKey()
addKey()
checkM(5)
// 6 out of 10
addKey()
addKey()
checkM(6)
}