|
| 1 | +package bridge |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/url" |
| 6 | + |
| 7 | + "github.com/xuperchain/xupercore/kernel/contract/bridge/pb" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + // XuperScheme define the xuper scheme |
| 12 | + XuperScheme = "xuper" |
| 13 | +) |
| 14 | + |
| 15 | +// CrossChainURI Standard |
| 16 | +// [scheme:][//chain_name][path][?query] |
| 17 | +// eg xuper://chain1?module=wasm&bcname=xuper&contract_name=counter&method_name=increase |
| 18 | +type CrossChainURI struct { |
| 19 | + *url.URL |
| 20 | +} |
| 21 | + |
| 22 | +// ParseCrossChainURI will parse uri to cross chain uri instance |
| 23 | +func ParseCrossChainURI(crossChainURI string) (*CrossChainURI, error) { |
| 24 | + uri, err := url.Parse(crossChainURI) |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + |
| 29 | + return &CrossChainURI{ |
| 30 | + uri, |
| 31 | + }, nil |
| 32 | +} |
| 33 | + |
| 34 | +// GetScheme return cross chain uri scheme |
| 35 | +func (ccu *CrossChainURI) GetScheme() string { |
| 36 | + return ccu.URL.Scheme |
| 37 | +} |
| 38 | + |
| 39 | +// GetChainName return cross chain uri chain name |
| 40 | +func (ccu *CrossChainURI) GetChainName() string { |
| 41 | + return ccu.URL.Host |
| 42 | +} |
| 43 | + |
| 44 | +// GetPath return cross chain uri path |
| 45 | +func (ccu *CrossChainURI) GetPath() string { |
| 46 | + return ccu.URL.Path |
| 47 | +} |
| 48 | + |
| 49 | +// GetQuery return cross chain uri query |
| 50 | +func (ccu *CrossChainURI) GetQuery() url.Values { |
| 51 | + return ccu.URL.Query() |
| 52 | +} |
| 53 | + |
| 54 | +// CrossChainScheme define the interface of CrossChainScheme |
| 55 | +type CrossChainScheme interface { |
| 56 | + GetName() string |
| 57 | + GetCrossQueryRequest(*CrossChainURI, []*pb.ArgPair, string, []string) (*pb.CrossQueryRequest, error) |
| 58 | +} |
| 59 | + |
| 60 | +// CrossXuperScheme define the xuper scheme |
| 61 | +type CrossXuperScheme struct { |
| 62 | +} |
| 63 | + |
| 64 | +// GetCrossQueryRequest return XupeScheme instance with CrossChainURI |
| 65 | +// [scheme:][//chain_name][?query] |
| 66 | +// eg xuper://chain1?module=wasm&bcname=xuper&contract_name=counter&method_name=increase |
| 67 | +func (cxs *CrossXuperScheme) GetCrossQueryRequest(crossChainURI *CrossChainURI, |
| 68 | + argPair []*pb.ArgPair, initiator string, authRequire []string) (*pb.CrossQueryRequest, error) { |
| 69 | + if initiator == "" { |
| 70 | + return nil, fmt.Errorf("GetCrossQueryRequest initiator is nil") |
| 71 | + } |
| 72 | + |
| 73 | + querys := crossChainURI.GetQuery() |
| 74 | + module := querys.Get("module") |
| 75 | + bcname := querys.Get("bcname") |
| 76 | + contractName := querys.Get("contract_name") |
| 77 | + methodName := querys.Get("method_name") |
| 78 | + if module == "" || bcname == "" || contractName == "" || methodName == "" { |
| 79 | + return nil, fmt.Errorf("GetCrossQueryRequest query is nil") |
| 80 | + } |
| 81 | + args := make(map[string][]byte) |
| 82 | + for _, arg := range argPair { |
| 83 | + args[arg.GetKey()] = arg.GetValue() |
| 84 | + } |
| 85 | + |
| 86 | + crossQueryRequest := &pb.CrossQueryRequest{ |
| 87 | + Bcname: bcname, |
| 88 | + Initiator: initiator, |
| 89 | + AuthRequire: authRequire, |
| 90 | + Request: &pb.InvokeRequest{ |
| 91 | + ModuleName: module, |
| 92 | + ContractName: contractName, |
| 93 | + MethodName: methodName, |
| 94 | + Args: args, |
| 95 | + }, |
| 96 | + } |
| 97 | + return crossQueryRequest, nil |
| 98 | +} |
| 99 | + |
| 100 | +// GetName return cross xuper scheme name |
| 101 | +func (cxs *CrossXuperScheme) GetName() string { |
| 102 | + return XuperScheme |
| 103 | +} |
| 104 | + |
| 105 | +// GetChainScheme return chain scheme by scheme |
| 106 | +func GetChainScheme(scheme string) CrossChainScheme { |
| 107 | + switch scheme { |
| 108 | + case XuperScheme: |
| 109 | + return &CrossXuperScheme{} |
| 110 | + default: |
| 111 | + return &CrossXuperScheme{} |
| 112 | + } |
| 113 | +} |
0 commit comments