-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathclaimable_balance_id.go
More file actions
62 lines (55 loc) · 1.95 KB
/
Copy pathclaimable_balance_id.go
File metadata and controls
62 lines (55 loc) · 1.95 KB
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
package xdr
import (
"fmt"
"github.com/stellar/go-stellar-sdk/strkey"
)
func (e *EncodingBuffer) claimableBalanceCompressEncodeTo(cb ClaimableBalanceId) error {
if err := e.xdrEncoderBuf.WriteByte(byte(cb.Type)); err != nil {
return err
}
switch cb.Type {
case ClaimableBalanceIdTypeClaimableBalanceIdTypeV0:
_, err := e.xdrEncoderBuf.Write(cb.V0[:])
return err
default:
panic("Unknown type")
}
}
// EncodeToStrkey returns the strkey encoding of the given claimable balance id
// See SEP-23: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md
func (c ClaimableBalanceId) EncodeToStrkey() (string, error) {
switch c.Type {
case ClaimableBalanceIdTypeClaimableBalanceIdTypeV0:
hash := c.MustV0()
payload := make([]byte, 0, len(hash)+1)
payload = append(payload, byte(c.Type))
payload = append(payload, hash[:]...)
return strkey.Encode(strkey.VersionByteClaimableBalance, payload)
default:
return "", fmt.Errorf("unknown claimable balance id type: %v", c.Type)
}
}
// MustEncodeToStrkey returns the strkey encoding of the given claimable balance id
// See SEP-23: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md
func (c ClaimableBalanceId) MustEncodeToStrkey() string {
address, err := c.EncodeToStrkey()
if err != nil {
panic(err)
}
return address
}
// DecodeFromStrkey parses a strkey encoded address into the given claimable balance id
// See SEP-23: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md
func (c *ClaimableBalanceId) DecodeFromStrkey(address string) error {
payload, err := strkey.Decode(strkey.VersionByteClaimableBalance, address)
if err != nil {
return err
}
if ClaimableBalanceIdType(payload[0]) != ClaimableBalanceIdTypeClaimableBalanceIdTypeV0 {
return fmt.Errorf("invalid claimable balance id type: %v", payload[0])
}
c.Type = ClaimableBalanceIdTypeClaimableBalanceIdTypeV0
c.V0 = &Hash{}
copy(c.V0[:], payload[1:])
return nil
}