-
Notifications
You must be signed in to change notification settings - Fork 10
/
enterprise_address.go
65 lines (55 loc) · 1.56 KB
/
enterprise_address.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
package libada
import (
"github.com/Bitrue-exchange/libada-go/internal/bech32"
"github.com/fxamacker/cbor/v2"
)
type EnterpriseAddress struct {
Network Network
Payment StakeCredential
}
// NewEnterpriseAddress creates Enterprise type address with public key
// the pubkey param can be pubkeyHash or 32 bytes pubkey or 64 bytes xpub
func NewEnterpriseAddress(pubkey []byte, kind StakeCredentialType, network Network) *EnterpriseAddress {
// KeyStakeCred.Data is Blake2b224 result of public key 32bytes
if len(pubkey) >= 32 {
pubkey = Blake2b224(pubkey[:32])
}
return &EnterpriseAddress{
Network: network,
Payment: StakeCredential{
Kind: kind,
Data: pubkey,
},
}
}
func NewKeyedEnterpriseAddress(key []byte, network Network) *EnterpriseAddress {
return NewEnterpriseAddress(key, KeyStakeCredentialType, network)
}
func (e *EnterpriseAddress) Bytes() []byte {
buf := make([]byte, 29)
buf[0] = 0b0110_0000 | (byte(e.Payment.Kind) << 4) | (byte(e.Network) & 0xf)
copy(buf[1:], e.Payment.Data)
return buf
}
func (e *EnterpriseAddress) String() string {
res, _ := bech32.Encode(e.Prefix(), e.Bytes())
return res
}
func (e *EnterpriseAddress) Kind() AddressKind {
return EnterpriseAddressKind
}
func (e *EnterpriseAddress) MarshalCBOR() ([]byte, error) {
if len(e.Payment.Data) != Hash28Size {
return nil, ErrInvalidCredSize
}
return cbor.Marshal(e.Bytes())
}
func (e *EnterpriseAddress) GetNetwork() Network {
return e.Network
}
func (e *EnterpriseAddress) Prefix() string {
if e.Network == Testnet {
return B32Prefix + TestnetSuffix
}
return B32Prefix
}