-
Notifications
You must be signed in to change notification settings - Fork 47
/
AMT.go
96 lines (85 loc) · 2.83 KB
/
AMT.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
88
89
90
91
92
93
94
95
96
// Copyright 2023 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package amtdeposit
import (
"bytes"
"embed"
"github.com/amazechain/amc/accounts/abi"
"github.com/amazechain/amc/common/crypto"
"github.com/amazechain/amc/common/hexutil"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/log"
"github.com/holiman/uint256"
"github.com/pkg/errors"
"math/big"
)
//go:embed abi.json
var abiJson embed.FS
var contractAbi abi.ABI
var depositSignature = crypto.Keccak256Hash([]byte("DepositEvent(bytes,uint256,bytes)"))
var withdrawnSignature = crypto.Keccak256Hash([]byte("WithdrawnEvent(uint256)"))
func init() {
var (
depositAbiCode []byte
err error
)
if depositAbiCode, err = abiJson.ReadFile("abi.json"); err != nil {
panic("Could not open abi.json")
}
if contractAbi, err = abi.JSON(bytes.NewReader(depositAbiCode)); err != nil {
panic("unable to parse AMT deposit contract abi")
}
}
type Contract struct {
}
func (Contract) WithdrawnSignature() types.Hash {
return withdrawnSignature
}
func (Contract) DepositSignature() types.Hash {
return depositSignature
}
func (c Contract) IsDepositAction(sigdata [4]byte) bool {
var (
method *abi.Method
err error
)
if method, err = contractAbi.MethodById(sigdata[:]); err != nil {
return false
}
if !bytes.Equal(method.ID, contractAbi.Methods["deposit"].ID) {
return false
}
return true
}
func (Contract) UnpackDepositLogData(data []byte) (publicKey []byte, signature []byte, depositAmount *uint256.Int, err error) {
var (
unpackedLogs []interface{}
overflow bool
)
//
if unpackedLogs, err = contractAbi.Unpack("DepositEvent", data); err != nil {
err = errors.Wrap(err, "unable to unpack logs")
return
}
//
if depositAmount, overflow = uint256.FromBig(unpackedLogs[1].(*big.Int)); overflow {
err = errors.New("unable to unpack amount")
return
}
publicKey, signature = unpackedLogs[0].([]byte), unpackedLogs[2].([]byte)
log.Debug("unpacked DepositEvent Logs", "publicKey", hexutil.Encode(unpackedLogs[0].([]byte)), "signature", hexutil.Encode(unpackedLogs[2].([]byte)), "message", hexutil.Encode(depositAmount.Bytes()))
return
}