lockup.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package processor
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "github.com/ethereum/go-ethereum/crypto"
  6. "go.uber.org/zap"
  7. "github.com/certusone/wormhole/bridge/pkg/common"
  8. "github.com/certusone/wormhole/bridge/pkg/supervisor"
  9. "github.com/certusone/wormhole/bridge/pkg/vaa"
  10. )
  11. // handleLockup processes a lockup received from a chain and instantiates our deterministic copy of the VAA
  12. func (p *Processor) handleLockup(ctx context.Context, k *common.ChainLock) {
  13. supervisor.Logger(ctx).Info("lockup confirmed",
  14. zap.Stringer("source_chain", k.SourceChain),
  15. zap.Stringer("target_chain", k.TargetChain),
  16. zap.Stringer("source_addr", k.SourceAddress),
  17. zap.Stringer("target_addr", k.TargetAddress),
  18. zap.Stringer("token_chain", k.TokenChain),
  19. zap.Stringer("token_addr", k.TokenAddress),
  20. zap.Stringer("amount", k.Amount),
  21. zap.Stringer("txhash", k.TxHash),
  22. zap.Time("timestamp", k.Timestamp),
  23. )
  24. if p.gs == nil {
  25. p.logger.Warn("received observation, but we don't know the guardian set yet")
  26. return
  27. }
  28. us, ok := p.gs.KeyIndex(p.ourAddr)
  29. if !ok {
  30. p.logger.Error("we're not in the guardian set - refusing to sign",
  31. zap.Uint32("index", p.gs.Index),
  32. zap.Stringer("our_addr", p.ourAddr),
  33. zap.Any("set", p.gs.KeysAsHexStrings()))
  34. return
  35. }
  36. // All nodes will create the exact same VAA and sign its digest.
  37. // Consensus is established on this digest.
  38. v := &vaa.VAA{
  39. Version: vaa.SupportedVAAVersion,
  40. GuardianSetIndex: p.gs.Index,
  41. Signatures: nil,
  42. Timestamp: k.Timestamp,
  43. Payload: &vaa.BodyTransfer{
  44. Nonce: k.Nonce,
  45. SourceChain: k.SourceChain,
  46. TargetChain: k.TargetChain,
  47. SourceAddress: k.SourceAddress,
  48. TargetAddress: k.TargetAddress,
  49. Asset: &vaa.AssetMeta{
  50. Chain: k.TokenChain,
  51. Address: k.TokenAddress,
  52. Decimals: k.TokenDecimals,
  53. },
  54. Amount: k.Amount,
  55. },
  56. }
  57. // Generate digest of the unsigned VAA.
  58. digest, err := v.SigningMsg()
  59. if err != nil {
  60. panic(err)
  61. }
  62. // Sign the digest using our node's guardian key.
  63. s, err := crypto.Sign(digest.Bytes(), p.gk)
  64. if err != nil {
  65. panic(err)
  66. }
  67. p.logger.Info("observed and signed confirmed lockup",
  68. zap.Stringer("source_chain", k.SourceChain),
  69. zap.Stringer("target_chain", k.TargetChain),
  70. zap.Stringer("txhash", k.TxHash),
  71. zap.String("digest", hex.EncodeToString(digest.Bytes())),
  72. zap.String("signature", hex.EncodeToString(s)),
  73. zap.Int("our_index", us))
  74. p.broadcastSignature(v, s)
  75. }