node/pkg/db: add db.VaaIDFromString and vaa.StringToAddress helpers
commit-id:1fb9f998
This commit is contained in:
parent
28c713a6dd
commit
95fea79276
|
@ -5,6 +5,8 @@ import (
|
|||
"fmt"
|
||||
"github.com/certusone/wormhole/node/pkg/vaa"
|
||||
"github.com/dgraph-io/badger/v3"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
|
@ -17,6 +19,37 @@ type VAAID struct {
|
|||
Sequence uint64
|
||||
}
|
||||
|
||||
// VaaIDFromString parses a <chain>/<address>/<sequence> string into a VAAID.
|
||||
func VaaIDFromString(s string) (*VAAID, error) {
|
||||
parts := strings.Split(s, "/")
|
||||
if len(parts) != 3 {
|
||||
return nil, errors.New("invalid message id")
|
||||
}
|
||||
|
||||
emitterChain, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid emitter chain: %s", err)
|
||||
}
|
||||
|
||||
emitterAddress, err := vaa.StringToAddress(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid emitter address: %s", err)
|
||||
}
|
||||
|
||||
sequence, err := strconv.ParseUint(parts[2], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid sequence: %s", err)
|
||||
}
|
||||
|
||||
msgId := &VAAID{
|
||||
EmitterChain: vaa.ChainID(emitterChain),
|
||||
EmitterAddress: emitterAddress,
|
||||
Sequence: sequence,
|
||||
}
|
||||
|
||||
return msgId, nil
|
||||
}
|
||||
|
||||
func VaaIDFromVAA(v *vaa.VAA) *VAAID {
|
||||
return &VAAID{
|
||||
EmitterChain: v.EmitterChain,
|
||||
|
|
|
@ -342,3 +342,14 @@ func MustWrite(w io.Writer, order binary.ByteOrder, data interface{}) {
|
|||
panic(fmt.Errorf("failed to write binary data: %v", data).Error())
|
||||
}
|
||||
}
|
||||
|
||||
// StringToAddress converts a hex-encoded adress into a vaa.Address
|
||||
func StringToAddress(value string) (Address, error) {
|
||||
var address Address
|
||||
res, err := hex.DecodeString(value)
|
||||
if err != nil {
|
||||
return address, err
|
||||
}
|
||||
copy(address[:], res)
|
||||
return address, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue