/
address.go
190 lines (163 loc) · 6.76 KB
/
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package types
import (
"encoding"
"golang.org/x/crypto/sha3"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/address"
"github.com/oasisprotocol/oasis-core/go/common/encoding/bech32"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/ed25519"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/secp256k1"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/sr25519"
)
var (
// AddressV0Ed25519Context is the unique context for v0 Ed25519-based addresses.
// It is shared with the consensus layer addresses on purpose.
AddressV0Ed25519Context = staking.AddressV0Context
// AddressV0Secp256k1EthContext is the unique context for v0 secp256k1-based addresses.
AddressV0Secp256k1EthContext = address.NewContext("oasis-runtime-sdk/address: secp256k1eth", 0)
// AddressV0Sr25519Context is the unique context for v0 Sr25519-based addresses.
AddressV0Sr25519Context = address.NewContext("oasis-runtime-sdk/address: sr25519", 0)
// AddressV0MultisigContext is the unique context for v0 multisig addresses.
AddressV0MultisigContext = address.NewContext("oasis-runtime-sdk/address: multisig", 0)
// AddressV0ModuleContext is the unique context for v0 module addresses.
AddressV0ModuleContext = address.NewContext("oasis-runtime-sdk/address: module", 0)
// AddressBech32HRP is the unique human readable part of Bech32 encoded
// staking account addresses.
AddressBech32HRP = staking.AddressBech32HRP
_ encoding.BinaryMarshaler = Address{}
_ encoding.BinaryUnmarshaler = (*Address)(nil)
_ encoding.TextMarshaler = Address{}
_ encoding.TextUnmarshaler = (*Address)(nil)
)
// SignatureAddressSpec is information for signature-based authentication and public key-based
// address derivation.
type SignatureAddressSpec struct {
// Ed25519 address derivation compatible with the consensus layer.
Ed25519 *ed25519.PublicKey `json:"ed25519,omitempty"`
// Secp256k1Eth is ethereum-compatible address derivation from Secp256k1 public keys.
Secp256k1Eth *secp256k1.PublicKey `json:"secp256k1eth,omitempty"`
// Sr25519 address derivation.
Sr25519 *sr25519.PublicKey `json:"sr25519,omitempty"`
}
// PublicKey returns the public key of the authentication/address derivation specification.
func (as *SignatureAddressSpec) PublicKey() PublicKey {
switch {
case as.Ed25519 != nil:
return PublicKey{PublicKey: as.Ed25519}
case as.Secp256k1Eth != nil:
return PublicKey{PublicKey: as.Secp256k1Eth}
case as.Sr25519 != nil:
return PublicKey{PublicKey: as.Sr25519}
}
return PublicKey{}
}
// NewSignatureAddressSpecEd25519 creates a new address specification for an Ed25519 public key.
func NewSignatureAddressSpecEd25519(pk ed25519.PublicKey) SignatureAddressSpec {
return SignatureAddressSpec{Ed25519: &pk}
}
// NewSignatureAddressSpecSecp256k1Eth creates a new Ethereum-compatible address specification for
// an Secp256k1 public key.
func NewSignatureAddressSpecSecp256k1Eth(pk secp256k1.PublicKey) SignatureAddressSpec {
return SignatureAddressSpec{Secp256k1Eth: &pk}
}
// NewSignatureAddressSpecSr25519 creates a new address specification for an Sr25519 public key.
func NewSignatureAddressSpecSr25519(pk sr25519.PublicKey) SignatureAddressSpec {
return SignatureAddressSpec{Sr25519: &pk}
}
// Address is the account address.
type Address address.Address
// MarshalBinary encodes an address into binary form.
func (a Address) MarshalBinary() ([]byte, error) {
return (address.Address)(a).MarshalBinary()
}
// UnmarshalBinary decodes a binary marshaled address.
func (a *Address) UnmarshalBinary(data []byte) error {
return (*address.Address)(a).UnmarshalBinary(data)
}
// MarshalText encodes an address into text form.
func (a Address) MarshalText() ([]byte, error) {
return (address.Address)(a).MarshalBech32(AddressBech32HRP)
}
// UnmarshalText decodes a text marshaled address.
func (a *Address) UnmarshalText(text []byte) error {
return (*address.Address)(a).UnmarshalBech32(AddressBech32HRP, text)
}
// Equal compares vs another address for equality.
func (a Address) Equal(cmp Address) bool {
return (address.Address)(a).Equal((address.Address)(cmp))
}
// String returns the string representation of an address.
func (a Address) String() string {
bech32Addr, err := bech32.Encode(AddressBech32HRP.String(), a[:])
if err != nil {
return "[malformed]"
}
return bech32Addr
}
// ConsensusAddress converts this address into a consensus-layer address type.
func (a Address) ConsensusAddress() staking.Address {
return (staking.Address)((address.Address)(a))
}
// NewAddress creates a new address from the given signature address specification.
func NewAddress(spec SignatureAddressSpec) (a Address) {
var (
ctx address.Context
pkData []byte
)
switch {
case spec.Ed25519 != nil:
ctx = AddressV0Ed25519Context
pkData, _ = spec.Ed25519.MarshalBinary()
case spec.Secp256k1Eth != nil:
ctx = AddressV0Secp256k1EthContext
// Use a scheme such that we can compute Secp256k1 addresses from Ethereum
// addresses as this makes things more interoperable.
h := sha3.NewLegacyKeccak256()
untaggedPk, _ := spec.Secp256k1Eth.MarshalBinaryUncompressedUntagged()
h.Write(untaggedPk)
pkData = h.Sum(nil)[32-20:]
case spec.Sr25519 != nil:
ctx = AddressV0Sr25519Context
pkData, _ = spec.Sr25519.MarshalBinary()
default:
panic("address: unsupported public key type")
}
return (Address)(address.NewAddress(ctx, pkData))
}
// NewAddressRaw creates a new address from passed address context and data.
func NewAddressRaw(ctx address.Context, data []byte) Address {
return (Address)(address.NewAddress(ctx, data))
}
// NewAddressForModule creates a new address for a specific module and raw kind.
func NewAddressForModule(module string, kind []byte) Address {
moduleBytes := []byte(module)
sepBytes := []byte(".")
data := make([]byte, 0, len(moduleBytes)+len(kind)+1)
data = append(data,
moduleBytes...)
data = append(data,
sepBytes...)
data = append(data,
kind...,
)
return (Address)(address.NewAddress(AddressV0ModuleContext, data))
}
// NewAddressFromBech32 creates a new address from the given bech-32 encoded string.
//
// Panics in case of errors -- use UnmarshalText if you want to handle errors.
func NewAddressFromBech32(data string) (a Address) {
err := a.UnmarshalText([]byte(data))
if err != nil {
panic(err)
}
return
}
// NewAddressFromMultisig creates a new address from the given multisig configuration.
func NewAddressFromMultisig(config *MultisigConfig) Address {
return (Address)(address.NewAddress(AddressV0MultisigContext, cbor.Marshal(config)))
}
// NewAddressFromConsensus converts a consensus layer address into an address.
func NewAddressFromConsensus(addr staking.Address) Address {
return (Address)((address.Address)(addr))
}