-
Notifications
You must be signed in to change notification settings - Fork 8
/
deeplink.go
109 lines (94 loc) 路 2.25 KB
/
deeplink.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
97
98
99
100
101
102
103
104
105
106
107
108
109
package transaction
import (
"encoding/base64"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"net/url"
)
type DeepLink struct {
Type Type
Data []byte
Payload []byte
Nonce *uint // optional
GasPrice *uint // optional
GasCoin *Coin // optional
}
func (d *DeepLink) CreateLink(pass string) (string, error) {
tx, err := d.Encode()
if err != nil {
return "", err
}
rawQuery := ""
if pass != "" {
rawQuery = fmt.Sprintf("p=%s", base64.RawStdEncoding.EncodeToString([]byte(pass)))
}
u := &url.URL{
Scheme: "https",
Host: "bip.to",
Path: fmt.Sprintf("/tx/%s", tx),
RawQuery: rawQuery,
}
return u.String(), nil
}
func (d *DeepLink) Encode() (string, error) {
src, err := rlp.EncodeToBytes(d)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(src), nil
}
func (d *DeepLink) setType(t Type) *DeepLink {
d.Type = t
return d
}
func (d *DeepLink) SetPayload(payload []byte) *DeepLink {
d.Payload = payload
return d
}
func (d *DeepLink) SetGasCoin(symbol string) *DeepLink {
gasCoin := Coin{}
d.GasCoin = &gasCoin
copy(d.GasCoin[:], symbol)
return d
}
func NewDeepLink(data DataInterface) (*DeepLink, error) {
d := new(DeepLink)
bytes, err := data.encode()
if err != nil {
return d, err
}
d.Data = bytes
switch data.(type) {
case *SendData:
return d.setType(TypeSend), nil
case *SellCoinData:
return d.setType(TypeSellCoin), nil
case *SellAllCoinData:
return d.setType(TypeSellAllCoin), nil
case *BuyCoinData:
return d.setType(TypeBuyCoin), nil
case *CreateCoinData:
return d.setType(TypeCreateCoin), nil
case *DeclareCandidacyData:
return d.setType(TypeDeclareCandidacy), nil
case *DelegateData:
return d.setType(TypeDelegate), nil
case *UnbondData:
return d.setType(TypeUnbond), nil
case *RedeemCheckData:
return d.setType(TypeRedeemCheck), nil
case *SetCandidateOnData:
return d.setType(TypeSetCandidateOnline), nil
case *SetCandidateOffData:
return d.setType(TypeSetCandidateOffline), nil
case *CreateMultisigData:
return d.setType(TypeCreateMultisig), nil
case *MultisendData:
return d.setType(TypeMultisend), nil
case *EditCandidateData:
return d.setType(TypeEditCandidate), nil
default:
return nil, errors.New("unknown transaction type")
}
}