• R/O
  • HTTP
  • SSH
  • HTTPS

equity: 提交

Based on BUTXO Programming Language


Commit MetaInfo

修訂8b24b60ff0e5cd5ee490067023113fd4ff847e1e (tree)
時間2018-11-26 16:57:38
作者oysheng <33340252+oysheng@user...>
CommiterPaladz

Log Message

add the argument type "Sign" to support checking signature for message (#27)

add buildin function checkMsgSig

Change Summary

差異

--- a/compiler/builtins.go
+++ b/compiler/builtins.go
@@ -15,6 +15,7 @@ var builtins = []builtin{
1515 {"min", "MIN", []typeDesc{intType, intType}, intType},
1616 {"max", "MAX", []typeDesc{intType, intType}, intType},
1717 {"checkTxSig", "TXSIGHASH SWAP CHECKSIG", []typeDesc{pubkeyType, sigType}, boolType},
18+ {"checkMsgSig", "CHECKSIG", []typeDesc{pubkeyType, hashType, signType}, boolType},
1819 {"concat", "CAT", []typeDesc{nilType, nilType}, strType},
1920 {"concatpush", "CATPUSHDATA", []typeDesc{nilType, nilType}, strType},
2021 {"below", "BLOCKHEIGHT GREATERTHAN", []typeDesc{intType}, boolType},
--- a/compiler/checks.go
+++ b/compiler/checks.go
@@ -247,13 +247,13 @@ func typeCheckStatement(stat statement, contractValue ValueInfo, clauseName stri
247247 }
248248
249249 case *defineStatement:
250- if stmt.expr != nil && stmt.expr.typ(env) != stmt.variable.Type && !isHashSubtype(stmt.expr.typ(env)) {
250+ if stmt.expr != nil && stmt.expr.typ(env) != stmt.variable.Type && !(stmt.variable.Type == hashType && isHashSubtype(stmt.expr.typ(env))) {
251251 return fmt.Errorf("expression in define statement in clause \"%s\" has type \"%s\", must be \"%s\"",
252252 clauseName, stmt.expr.typ(env), stmt.variable.Type)
253253 }
254254
255255 case *assignStatement:
256- if stmt.expr.typ(env) != stmt.variable.Type && !isHashSubtype(stmt.expr.typ(env)) {
256+ if stmt.expr.typ(env) != stmt.variable.Type && !(stmt.variable.Type == hashType && isHashSubtype(stmt.expr.typ(env))) {
257257 return fmt.Errorf("expression in assign statement in clause \"%s\" has type \"%s\", must be \"%s\"",
258258 clauseName, stmt.expr.typ(env), stmt.variable.Type)
259259 }
--- a/compiler/cmd/equitycmd/equitycmd.go
+++ b/compiler/cmd/equitycmd/equitycmd.go
@@ -128,7 +128,7 @@ func main() {
128128 fmt.Fprintf(buf, "\t_contractArgs = append(_contractArgs, compiler.ContractArg{B: &%s})\n", param.Name)
129129 case "Integer":
130130 fmt.Fprintf(buf, "\t_contractArgs = append(_contractArgs, compiler.ContractArg{I: &%s})\n", param.Name)
131- case "Hash", "Program", "PublicKey", "Signature", "String":
131+ case "Hash", "Program", "PublicKey", "Signature", "Sign", "String":
132132 fmt.Fprintf(buf, "\t_contractArgs = append(_contractArgs, compiler.ContractArg{S: (*json.HexBytes)(&%s)})\n", param.Name)
133133 }
134134 }
@@ -273,6 +273,9 @@ func asGoParams(params []*compiler.Param) (goParams string, imports []string) {
273273 case "Signature":
274274 typ = "[]byte"
275275 strFlag = true
276+ case "Sign":
277+ typ = "[]byte"
278+ strFlag = true
276279 case "String":
277280 typ = "[]byte"
278281 strFlag = true
--- a/compiler/compile.go
+++ b/compiler/compile.go
@@ -122,7 +122,7 @@ func Instantiate(body []byte, params []*Param, recursive bool, args []ContractAr
122122 if arg.I == nil {
123123 return nil, fmt.Errorf("type mismatch in arg %d (want integer)", i)
124124 }
125- case assetType, hashType, progType, pubkeyType, sigType, strType:
125+ case assetType, hashType, progType, pubkeyType, sigType, signType, strType:
126126 if arg.S == nil {
127127 return nil, fmt.Errorf("type mismatch in arg %d (want string)", i)
128128 }
--- a/compiler/compile_test.go
+++ b/compiler/compile_test.go
@@ -227,6 +227,16 @@ contract TestConstantMath(result: Integer, hashByte: Hash, hashStr: Hash, outcom
227227 }
228228 `
229229
230+const VerifySignature = `
231+contract VerifySignature(sig1: Sign, sig2: Sign, msgHash: Hash) locks valueAmount of valueAsset {
232+ clause check(publicKey1: PublicKey, publicKey2: PublicKey) {
233+ verify checkMsgSig(publicKey1, msgHash, sig1)
234+ verify checkMsgSig(publicKey2, msgHash, sig2)
235+ unlock valueAmount of valueAsset
236+ }
237+}
238+`
239+
230240 func TestCompile(t *testing.T) {
231241 cases := []struct {
232242 name string
@@ -318,6 +328,11 @@ func TestCompile(t *testing.T) {
318328 TestConstantMath,
319329 "765779577a935a93887c0431323330aa887c06737472696e67aa887c91697b011493879a",
320330 },
331+ {
332+ "VerifySignature",
333+ VerifySignature,
334+ "5279557aac697c7bac",
335+ },
321336 }
322337
323338 for _, c := range cases {
--- a/compiler/types.go
+++ b/compiler/types.go
@@ -15,6 +15,7 @@ var (
1515 progType = typeDesc("Program")
1616 pubkeyType = typeDesc("PublicKey")
1717 sigType = typeDesc("Signature")
18+ signType = typeDesc("Sign")
1819 strType = typeDesc("String")
1920
2021 sha3StrType = typeDesc("Sha3(String)")
@@ -35,6 +36,7 @@ var types = map[string]typeDesc{
3536 string(progType): progType,
3637 string(pubkeyType): pubkeyType,
3738 string(sigType): sigType,
39+ string(signType): signType,
3840 string(strType): strType,
3941
4042 string(sha3StrType): sha3StrType,
--- a/equity/util/instance.go
+++ b/equity/util/instance.go
@@ -20,6 +20,7 @@ func InstantiateContract(contract *compiler.Contract, args []compiler.ContractAr
2020 return program, nil
2121 }
2222
23+// ConvertArguments convert input argument into contract argument
2324 func ConvertArguments(contract *compiler.Contract, args []string) ([]compiler.ContractArg, error) {
2425 var contractArgs []compiler.ContractArg
2526 for i, p := range contract.Params {
@@ -66,6 +67,17 @@ func ConvertArguments(contract *compiler.Contract, args []string) ([]compiler.Co
6667 }
6768 argument.S = (*chainjson.HexBytes)(&commonValue)
6869
70+ case "Sign":
71+ if len(args[i]) != 128 {
72+ return nil, errors.New("mismatch length for Sign argument")
73+ }
74+
75+ signValue, err := hex.DecodeString(args[i])
76+ if err != nil {
77+ return nil, err
78+ }
79+ argument.S = (*chainjson.HexBytes)(&signValue)
80+
6981 case "Program":
7082 program, err := hex.DecodeString(args[i])
7183 if err != nil {
Show on old repository browser