-
Notifications
You must be signed in to change notification settings - Fork 79
/
contract.go
64 lines (55 loc) · 2.1 KB
/
contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package smartcontract
import (
"fmt"
"sort"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"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"
)
// CreateMultiSigRedeemScript creates an "m out of n" type verification script
// where n is the length of publicKeys.
func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) {
if m < 1 {
return nil, fmt.Errorf("param m cannot be smaller or equal to 1 got %d", m)
}
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))
}
buf := io.NewBufBinWriter()
emit.Int(buf.BinWriter, int64(m))
sort.Sort(publicKeys)
for _, pubKey := range publicKeys {
emit.Bytes(buf.BinWriter, pubKey.Bytes())
}
emit.Int(buf.BinWriter, int64(len(publicKeys)))
emit.Syscall(buf.BinWriter, interopnames.SystemCryptoCheckMultisig)
return buf.Bytes(), nil
}
// 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)
m := GetDefaultHonestNodeCount(n)
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 := GetMajorityHonestNodeCount(n)
return CreateMultiSigRedeemScript(m, publicKeys)
}
// GetDefaultHonestNodeCount returns minimum number of honest nodes
// required for network of size n.
func GetDefaultHonestNodeCount(n int) int {
return n - (n-1)/3
}
// GetMajorityHonestNodeCount returns minimum number of honest nodes
// required for majority-style agreement.
func GetMajorityHonestNodeCount(n int) int {
return n - (n-1)/2
}