guardianset_vaa.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package devnet
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/ethereum/go-ethereum/ethclient"
  11. "go.uber.org/zap"
  12. "github.com/certusone/wormhole/node/pkg/ethereum/abi"
  13. "github.com/certusone/wormhole/node/pkg/supervisor"
  14. "github.com/certusone/wormhole/node/pkg/vaa"
  15. )
  16. // DevnetGuardianSetVSS returns a VAA signed by guardian-0 that adds all n validators.
  17. func DevnetGuardianSetVSS(n uint) *vaa.VAA {
  18. pubkeys := make([]common.Address, n)
  19. for n := range pubkeys {
  20. key := DeterministicEcdsaKeyByIndex(crypto.S256(), uint64(n))
  21. pubkeys[n] = crypto.PubkeyToAddress(key.PublicKey)
  22. }
  23. v := &vaa.VAA{
  24. Version: 1,
  25. GuardianSetIndex: 0,
  26. Timestamp: time.Unix(5000, 0),
  27. EmitterChain: 3,
  28. EmitterAddress: vaa.Address{
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
  30. },
  31. Payload: vaa.BodyGuardianSetUpdate{
  32. Keys: pubkeys,
  33. NewIndex: 1,
  34. }.Serialize(),
  35. }
  36. // The devnet is initialized with a single guardian (ethereum/migrations/1_initial_migration.js).
  37. key0 := DeterministicEcdsaKeyByIndex(crypto.S256(), 0)
  38. v.AddSignature(key0, 0)
  39. return v
  40. }
  41. // SubmitVAA submits a VAA to the devnet chain using well-known accounts and contract addresses.
  42. func SubmitVAA(ctx context.Context, rpcURL string, vaa *vaa.VAA) (*types.Transaction, error) {
  43. c, err := ethclient.DialContext(ctx, rpcURL)
  44. if err != nil {
  45. return nil, fmt.Errorf("dialing eth client failed: %w", err)
  46. }
  47. kt := GetKeyedTransactor(ctx)
  48. bridge, err := abi.NewAbi(GanacheBridgeContractAddress, c)
  49. if err != nil {
  50. panic(err)
  51. }
  52. b, err := vaa.Marshal()
  53. if err != nil {
  54. panic(err)
  55. }
  56. supervisor.Logger(ctx).Info("submitted VAA to Ethereum devnet", zap.Binary("binary", b))
  57. tx, err := bridge.SubmitNewGuardianSet(kt, b)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return tx, nil
  62. }
  63. // GetKeyedTransactor returns a transaction signer with the deterministic devnet key.
  64. func GetKeyedTransactor(ctx context.Context) *bind.TransactOpts {
  65. key, err := Wallet().PrivateKey(DeriveAccount(0))
  66. if err != nil {
  67. panic(err)
  68. }
  69. kt := bind.NewKeyedTransactor(key)
  70. kt.Context = ctx
  71. return kt
  72. }