2018-03-02 16:24:09 +01:00
|
|
|
package smartcontract
|
|
|
|
|
2018-03-25 12:45:54 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2019-08-27 16:29:42 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2020-02-03 17:46:51 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2020-02-03 17:24:57 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/emit"
|
2019-12-03 17:05:06 +03:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2018-03-25 12:45:54 +02:00
|
|
|
)
|
|
|
|
|
2019-10-22 17:56:03 +03:00
|
|
|
// CreateMultiSigRedeemScript creates a script runnable by the VM.
|
2019-08-27 16:29:42 +03:00
|
|
|
func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) {
|
2020-01-13 15:22:21 +03:00
|
|
|
if m < 1 {
|
2018-03-25 12:45:54 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-02-03 17:46:51 +03:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Int(buf.BinWriter, int64(m))
|
2018-03-25 12:45:54 +02:00
|
|
|
sort.Sort(publicKeys)
|
|
|
|
for _, pubKey := range publicKeys {
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Bytes(buf.BinWriter, pubKey.Bytes())
|
2018-03-25 12:45:54 +02:00
|
|
|
}
|
2020-02-03 17:46:51 +03:00
|
|
|
emit.Int(buf.BinWriter, int64(len(publicKeys)))
|
|
|
|
emit.Opcode(buf.BinWriter, opcode.CHECKMULTISIG)
|
2018-03-25 12:45:54 +02:00
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|