-
Notifications
You must be signed in to change notification settings - Fork 2
/
erc721.go
55 lines (43 loc) · 1.23 KB
/
erc721.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
package caip
import (
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
)
type ERC721AssetID struct {
EVMAssetID
}
func NewERC721AssetID(chainID ChainID, namespace, reference string) (ERC721AssetID, error) {
aID := ERC721AssetID{EVMAssetID{AssetID: AssetID{chainID, namespace, reference}}}
if err := aID.Validate(); err != nil {
return ERC721AssetID{}, err
}
return aID, nil
}
func UnsafeERC721AssetID(chainID ChainID, namespace, reference string) ERC721AssetID {
aID := AssetID{chainID, namespace, reference}
return ERC721AssetID{EVMAssetID{AssetID: aID}}
}
func (a ERC721AssetID) Validate() error {
if err := a.EVMAssetID.Validate(); err != nil {
return err
}
split := strings.Split(a.Reference, "/")
if ok := common.IsHexAddress(split[0]); !ok {
return fmt.Errorf("invalid eth address: %s", split[0])
}
if a.AssetID.Namespace != "erc721" {
return fmt.Errorf("invalid asset namespace: %s", a.AssetID.Namespace)
}
if len(split) > 1 {
if _, ok := new(big.Int).SetString(split[1], 10); !ok {
return fmt.Errorf("invalid token id: %s", split[1])
}
}
return nil
}
func (a ERC721AssetID) Address() common.Address {
split := strings.Split(a.Reference, "/")
return common.HexToAddress(split[0])
}