ceremonyclient/node/protobufs/clock.go

86 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-09-03 23:47:09 +00:00
package protobufs
import (
"math/big"
"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/pkg/errors"
)
2024-02-13 07:04:56 +00:00
func (frame *ClockFrame) GetParentAndSelector() (
2023-09-03 23:47:09 +00:00
*big.Int,
*big.Int,
error,
) {
outputBytes := [516]byte{}
copy(outputBytes[:], frame.Output[:516])
selector, err := poseidon.HashBytes(outputBytes[:])
if err != nil {
2024-02-13 07:04:56 +00:00
return nil, nil, errors.Wrap(err, "get parent selector and distance")
2023-09-03 23:47:09 +00:00
}
if frame.FrameNumber == 0 {
2024-02-13 07:04:56 +00:00
return big.NewInt(0), selector, nil
2023-09-03 23:47:09 +00:00
}
parentSelector := new(big.Int).SetBytes(frame.ParentSelector)
2024-02-13 07:04:56 +00:00
return parentSelector, selector, nil
2023-09-03 23:47:09 +00:00
}
func (frame *ClockFrame) GetSelector() (*big.Int, error) {
outputBytes := [516]byte{}
copy(outputBytes[:], frame.Output[:516])
selector, err := poseidon.HashBytes(outputBytes[:])
if err != nil {
return nil, errors.Wrap(err, "get selector")
}
return selector, nil
}
func (frame *ClockFrame) GetPublicKey() ([]byte, error) {
if frame.FrameNumber == 0 {
return make([]byte, 32), nil
}
2023-09-25 02:43:35 +00:00
var pubkey []byte
2023-09-03 23:47:09 +00:00
ed448PublicKey := frame.GetPublicKeySignatureEd448()
if ed448PublicKey != nil {
pubkey = ed448PublicKey.PublicKey.KeyValue
} else {
return nil, errors.Wrap(
errors.New("no valid signature provided"),
"get address",
)
}
return pubkey, nil
}
func (frame *ClockFrame) GetAddress() ([]byte, error) {
if frame.FrameNumber == 0 {
return make([]byte, 32), nil
}
2023-09-25 02:43:35 +00:00
var pubkey []byte
2023-09-03 23:47:09 +00:00
ed448PublicKey := frame.GetPublicKeySignatureEd448()
if ed448PublicKey != nil {
pubkey = ed448PublicKey.PublicKey.KeyValue
} else {
return nil, errors.Wrap(
errors.New("no valid signature provided"),
"get address",
)
}
address, err := poseidon.HashBytes(pubkey)
if err != nil {
return nil, errors.Wrap(err, "get parent selector and distance")
}
addressBytes := address.Bytes()
addressBytes = append(make([]byte, 32-len(addressBytes)), addressBytes...)
return addressBytes, nil
}