|
| 1 | +package ocr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 9 | + "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" |
| 10 | + "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" |
| 11 | + ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/chainlink-ton/pkg/txm" |
| 14 | + |
| 15 | + "github.com/xssnick/tonutils-go/address" |
| 16 | + "github.com/xssnick/tonutils-go/tlb" |
| 17 | + "github.com/xssnick/tonutils-go/ton/wallet" |
| 18 | + "github.com/xssnick/tonutils-go/tvm/cell" |
| 19 | +) |
| 20 | + |
| 21 | +// ToEd25519CalldataFunc is a function that takes in the OCR3 report and Ed25519 signature data and processes them. |
| 22 | +// It returns the contract name, method name, and arguments for the on-chain contract call. |
| 23 | +// The ReportWithInfo bytes field is also decoded according to the implementation of this function, |
| 24 | +// the commit and execute plugins have different representations for this data. |
| 25 | +// Ed25519 signatures are 96 bytes long (64 bytes signature + 32 bytes public key). |
| 26 | +type ToEd25519CalldataFunc func( |
| 27 | + rawReportCtx [2][32]byte, |
| 28 | + report ocr3types.ReportWithInfo[[]byte], |
| 29 | + signatures [][96]byte, |
| 30 | + codec ccipocr3.ExtraDataCodec, |
| 31 | +) (contract string, method string, args any, err error) |
| 32 | + |
| 33 | +type RawReportContext3Func func(configDigest [32]byte, seqNr uint64) [2][32]byte |
| 34 | + |
| 35 | +var _ ocr3types.ContractTransmitter[[]byte] = &ccipTransmitter{} |
| 36 | + |
| 37 | +type ccipTransmitter struct { |
| 38 | + txm txm.TxManager |
| 39 | + offrampAddress string |
| 40 | + toEd25519CalldataFn ToEd25519CalldataFunc |
| 41 | + rawReportContextFn RawReportContext3Func |
| 42 | + extraDataCodec ccipocr3.ExtraDataCodec |
| 43 | + lggr logger.Logger |
| 44 | +} |
| 45 | + |
| 46 | +func NewCCIPTransmitter( |
| 47 | + txm txm.TxManager, |
| 48 | + lggr logger.Logger, |
| 49 | +) (ocr3types.ContractTransmitter[[]byte], error) { |
| 50 | + if txm == nil || lggr == nil { |
| 51 | + return nil, errors.New("invalid transmitter args") |
| 52 | + } |
| 53 | + |
| 54 | + return &ccipTransmitter{ |
| 55 | + txm: txm, |
| 56 | + lggr: lggr, |
| 57 | + }, nil |
| 58 | +} |
| 59 | + |
| 60 | +func (c *ccipTransmitter) FromAccount(context.Context) (ocrtypes.Account, error) { |
| 61 | + w := c.txm.GetClient().Wallet |
| 62 | + return ocrtypes.Account(w.Address().StringRaw()), nil |
| 63 | +} |
| 64 | + |
| 65 | +func (c *ccipTransmitter) Transmit( |
| 66 | + ctx context.Context, |
| 67 | + configDigest ocrtypes.ConfigDigest, |
| 68 | + seqNr uint64, |
| 69 | + reportWithInfo ocr3types.ReportWithInfo[[]byte], |
| 70 | + sigs []ocrtypes.AttributedOnchainSignature, |
| 71 | +) error { |
| 72 | + if len(sigs) > 32 { |
| 73 | + return errors.New("too many signatures, maximum is 32") |
| 74 | + } |
| 75 | + |
| 76 | + rawReportCtx := c.rawReportContextFn(configDigest, seqNr) |
| 77 | + |
| 78 | + signatures := make([][96]byte, 0, len(sigs)) |
| 79 | + for _, sig := range sigs { |
| 80 | + if len(sig.Signature) != 96 { |
| 81 | + return fmt.Errorf("invalid ed25519 signature length, expected 96, got %d", len(sig.Signature)) |
| 82 | + } |
| 83 | + var fixedSig [96]byte |
| 84 | + copy(fixedSig[:], sig.Signature) |
| 85 | + signatures = append(signatures, fixedSig) |
| 86 | + } |
| 87 | + |
| 88 | + _, method, args, err := c.toEd25519CalldataFn(rawReportCtx, reportWithInfo, signatures, c.extraDataCodec) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to generate call data: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + body, ok := args.(*cell.Cell) |
| 94 | + if !ok { |
| 95 | + return fmt.Errorf("expected args to be *cell.Cell, got %T", args) |
| 96 | + } |
| 97 | + |
| 98 | + w := c.txm.GetClient().Wallet |
| 99 | + request := txm.Request{ |
| 100 | + Mode: wallet.PayGasSeparately, |
| 101 | + FromWallet: w, |
| 102 | + ContractAddress: *address.MustParseAddr(c.offrampAddress), |
| 103 | + Body: body, |
| 104 | + Amount: tlb.MustFromTON("0.05"), |
| 105 | + } |
| 106 | + |
| 107 | + c.lggr.Infow("Submitting transaction", "address", c.offrampAddress, "method", method) |
| 108 | + |
| 109 | + if err := c.txm.Enqueue(request); err != nil { |
| 110 | + return fmt.Errorf("failed to submit transaction via txm: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
0 commit comments