2018-03-02 15:24:09 +00:00
|
|
|
package smartcontract
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2018-03-25 10:45:54 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
2019-12-03 14:05:06 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2018-03-25 10:45:54 +00:00
|
|
|
)
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// CreateMultiSigRedeemScript creates a script runnable by the VM.
|
2019-08-27 13:29:42 +00:00
|
|
|
func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, error) {
|
2018-03-25 10:45:54 +00:00
|
|
|
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 := new(bytes.Buffer)
|
2019-01-25 11:20:35 +00:00
|
|
|
if err := vm.EmitInt(buf, int64(m)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
sort.Sort(publicKeys)
|
|
|
|
for _, pubKey := range publicKeys {
|
2019-01-25 11:20:35 +00:00
|
|
|
if err := vm.EmitBytes(buf, pubKey.Bytes()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := vm.EmitInt(buf, int64(len(publicKeys))); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
if err := vm.EmitOpcode(buf, opcode.CHECKMULTISIG); err != nil {
|
2019-01-25 11:20:35 +00:00
|
|
|
return nil, err
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|