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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateMultiSigRedeemScript will create 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-08-14 12:40:31 +00:00
|
|
|
if err := vm.EmitOpcode(buf, vm.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
|
|
|
|
}
|