2018-03-02 15:24:09 +00:00
|
|
|
package smartcontract
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2020-08-14 10:50:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2018-03-25 10:45:54 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 15:58:11 +00:00
|
|
|
// CreateMultiSigRedeemScript creates an "m out of n" type verification script
|
|
|
|
// where n is the length of publicKeys.
|
2019-08-27 13:29:42 +00:00
|
|
|
func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) {
|
2020-01-13 12:22:21 +00:00
|
|
|
if m < 1 {
|
2022-10-04 06:07:57 +00:00
|
|
|
return nil, fmt.Errorf("param m cannot be smaller than 1, got %d", m)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
if m > len(publicKeys) {
|
|
|
|
return nil, fmt.Errorf("length of the signatures (%d) is higher then the number of public keys", m)
|
|
|
|
}
|
|
|
|
if m > 1024 {
|
|
|
|
return nil, fmt.Errorf("public key count %d exceeds maximum of length 1024", len(publicKeys))
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:46:51 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Int(buf.BinWriter, int64(m))
|
2018-03-25 10:45:54 +00:00
|
|
|
sort.Sort(publicKeys)
|
|
|
|
for _, pubKey := range publicKeys {
|
2020-04-21 13:45:48 +00:00
|
|
|
emit.Bytes(buf.BinWriter, pubKey.Bytes())
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(buf.BinWriter, int64(len(publicKeys)))
|
2021-05-11 14:13:33 +00:00
|
|
|
emit.Syscall(buf.BinWriter, interopnames.SystemCryptoCheckMultisig)
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
2020-08-10 15:58:11 +00:00
|
|
|
|
|
|
|
// CreateDefaultMultiSigRedeemScript creates an "m out of n" type verification script
|
|
|
|
// using publicKeys length with the default BFT assumptions of (n - (n-1)/3) for m.
|
|
|
|
func CreateDefaultMultiSigRedeemScript(publicKeys keys.PublicKeys) ([]byte, error) {
|
|
|
|
n := len(publicKeys)
|
2020-10-07 12:58:33 +00:00
|
|
|
m := GetDefaultHonestNodeCount(n)
|
2020-08-10 15:58:11 +00:00
|
|
|
return CreateMultiSigRedeemScript(m, publicKeys)
|
|
|
|
}
|
2020-08-21 06:30:07 +00:00
|
|
|
|
|
|
|
// 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)
|
2021-02-02 09:34:15 +00:00
|
|
|
m := GetMajorityHonestNodeCount(n)
|
2020-08-21 06:30:07 +00:00
|
|
|
return CreateMultiSigRedeemScript(m, publicKeys)
|
|
|
|
}
|
2020-10-07 12:58:33 +00:00
|
|
|
|
|
|
|
// GetDefaultHonestNodeCount returns minimum number of honest nodes
|
|
|
|
// required for network of size n.
|
|
|
|
func GetDefaultHonestNodeCount(n int) int {
|
|
|
|
return n - (n-1)/3
|
|
|
|
}
|
2021-02-02 09:34:15 +00:00
|
|
|
|
|
|
|
// GetMajorityHonestNodeCount returns minimum number of honest nodes
|
|
|
|
// required for majority-style agreement.
|
|
|
|
func GetMajorityHonestNodeCount(n int) int {
|
|
|
|
return n - (n-1)/2
|
|
|
|
}
|