-
Notifications
You must be signed in to change notification settings - Fork 1
/
authorize.go
87 lines (71 loc) · 2.8 KB
/
authorize.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/assetcloud/chain/types"
mixTy "github.com/assetcloud/plugin/plugin/dapp/mix/types"
"github.com/consensys/gnark-crypto/ecc"
"github.com/pkg/errors"
)
func (a *action) authParamCheck(exec, symbol string, input *mixTy.AuthorizeCircuit) error {
//check tree rootHash exist
treeRootHash := input.TreeRootHash.GetWitnessValue(ecc.BN254)
exist, err := checkTreeRootHashExist(a.db, exec, symbol, mixTy.Str2Byte(treeRootHash.String()))
if err != nil {
return errors.Wrapf(err, "roothash=%s not found,exec=%s,symbol=%s", treeRootHash.String(), exec, symbol)
}
if !exist {
return errors.Wrapf(mixTy.ErrTreeRootHashNotFound, "roothash=%s", treeRootHash.String())
}
//authorize key should not exist
authHash := input.AuthorizeHash.GetWitnessValue(ecc.BN254)
authKey := calcAuthorizeHashKey(authHash.String())
_, err = a.db.Get(authKey)
if err == nil {
return errors.Wrapf(mixTy.ErrAuthorizeHashExist, "auth=%s", authHash.String())
}
if !isNotFound(err) {
return errors.Wrapf(err, "get auth=%s", authHash.String())
}
return nil
}
func (a *action) authorizePubInputs(exec, symbol string, proof *mixTy.ZkProofInfo) (*mixTy.AuthorizeCircuit, error) {
var input mixTy.AuthorizeCircuit
err := mixTy.ConstructCircuitPubInput(proof.PublicInput, &input)
if err != nil {
return nil, errors.Wrapf(err, "setCircuitPubInput")
}
err = a.authParamCheck(exec, symbol, &input)
if err != nil {
return nil, err
}
return &input, nil
}
/*
1. verify(zk-proof)
2, check tree root hash exist
3, check authorize pubkey hash in config or not
4. check authorize hash if exist in authorize pool
5. set authorize hash and authorize_spend hash
*/
func (a *action) Authorize(authorize *mixTy.MixAuthorizeAction) (*types.Receipt, error) {
execer, symbol := mixTy.GetAssetExecSymbol(a.api.GetConfig(), authorize.AssetExec, authorize.AssetSymbol)
input, err := a.authorizePubInputs(execer, symbol, authorize.ProofInfo)
if err != nil {
return nil, err
}
//zk-proof校验
err = zkProofVerify(a.db, authorize.ProofInfo, mixTy.VerifyType_AUTHORIZE)
if err != nil {
return nil, err
}
receipt := &types.Receipt{Ty: types.ExecOk}
authNullHash := input.AuthorizeHash.GetWitnessValue(ecc.BN254)
r := makeReceipt(calcAuthorizeHashKey(authNullHash.String()), mixTy.TyLogAuthorizeSet, &mixTy.ExistValue{Nullifier: authNullHash.String(), Exist: true})
mergeReceipt(receipt, r)
authSpendHash := input.AuthorizeSpendHash.GetWitnessValue(ecc.BN254)
r = makeReceipt(calcAuthorizeSpendHashKey(authSpendHash.String()), mixTy.TyLogAuthorizeSpendSet, &mixTy.ExistValue{Nullifier: authSpendHash.String(), Exist: true})
mergeReceipt(receipt, r)
return receipt, nil
}