Files
node/wallet/network/primary/examples/sign-l1-validator-removal-genesis/main.go
T

142 lines
3.4 KiB
Go

//go:build grpc
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"context"
"log"
"net/netip"
"time"
"github.com/luxfi/metric"
"google.golang.org/protobuf/proto"
compression "github.com/luxfi/compress"
"github.com/luxfi/constants"
"github.com/luxfi/ids"
"github.com/luxfi/node/network/peer"
"github.com/luxfi/node/proto/platformvm"
"github.com/luxfi/node/proto/pb/sdk"
"github.com/luxfi/node/vms/platformvm/warp"
"github.com/luxfi/node/vms/platformvm/warp/payload"
"github.com/luxfi/node/wallet/network/primary"
p2psdk "github.com/luxfi/p2p"
"github.com/luxfi/sdk/info"
p2pmessage "github.com/luxfi/node/message"
warpmessage "github.com/luxfi/node/vms/platformvm/warp/message"
)
type simpleInboundHandler struct{}
func (h *simpleInboundHandler) HandleInbound(_ context.Context, msg p2pmessage.InboundMessage) {
log.Printf("received %s: %s", msg.Op(), msg.Message())
}
func main() {
uri := primary.LocalAPIURI
netID := ids.FromStringOrPanic("2DeHa7Qb6sufPkmQcFWG2uCd4pBPv9WB6dkzroiMQhd1NSRtof")
validationIndex := uint32(0)
infoClient := info.NewClient(uri)
networkID, err := infoClient.GetNetworkID(context.Background())
if err != nil {
log.Fatalf("failed to fetch network ID: %s\n", err)
}
validationID := netID.Append(validationIndex)
l1ValidatorRegistration, err := warpmessage.NewL1ValidatorRegistration(
validationID,
false,
)
if err != nil {
log.Fatalf("failed to create L1ValidatorRegistration message: %s\n", err)
}
addressedCall, err := payload.NewAddressedCall(
nil,
l1ValidatorRegistration.Bytes(),
)
if err != nil {
log.Fatalf("failed to create AddressedCall message: %s\n", err)
}
unsignedWarp, err := warp.NewUnsignedMessage(
networkID,
constants.PlatformChainID,
addressedCall.Bytes(),
)
if err != nil {
log.Fatalf("failed to create unsigned Warp message: %s\n", err)
}
justification := platformvm.L1ValidatorRegistrationJustification{
Preimage: &platformvm.L1ValidatorRegistrationJustification_ConvertNetworkToL1TxData{
ConvertNetworkToL1TxData: &platformvm.ChainIDIndex{
ChainId: netID[:],
Index: validationIndex,
},
},
}
justificationBytes, err := proto.Marshal(&justification)
if err != nil {
log.Fatalf("failed to create justification: %s\n", err)
}
p, err := peer.StartTestPeer(
context.Background(),
netip.AddrPortFrom(
netip.AddrFrom4([4]byte{127, 0, 0, 1}),
9651,
),
networkID,
&simpleInboundHandler{},
)
if err != nil {
log.Fatalf("failed to start peer: %s\n", err)
}
messageBuilder, err := p2pmessage.NewCreator(
metric.NewNoOpRegistry(),
compression.TypeZstd,
time.Hour,
)
if err != nil {
log.Fatalf("failed to create message builder: %s\n", err)
}
appRequestPayload, err := proto.Marshal(&sdk.SignatureRequest{
Message: unsignedWarp.Bytes(),
Justification: justificationBytes,
})
if err != nil {
log.Fatalf("failed to marshal SignatureRequest: %s\n", err)
}
appRequest, err := messageBuilder.Request(
constants.PlatformChainID,
0,
time.Hour,
p2psdk.PrefixMessage(
p2psdk.ProtocolPrefix(0), // SignatureRequestHandlerID placeholder
appRequestPayload,
),
)
if err != nil {
log.Fatalf("failed to create Request: %s\n", err)
}
p.Send(context.Background(), appRequest)
time.Sleep(5 * time.Second)
p.StartClose()
err = p.AwaitClosed(context.Background())
if err != nil {
log.Fatalf("failed to close peer: %s\n", err)
}
}