|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/cloudflare/circl/sign/ed448" |
| 13 | + libP2pCrypto "github.com/libp2p/go-libp2p/core/crypto" |
| 14 | + "github.com/pkg/errors" |
| 15 | + "github.com/spf13/cobra" |
| 16 | + "go.uber.org/zap" |
| 17 | + "source.quilibrium.com/quilibrium/monorepo/node/config" |
| 18 | + "source.quilibrium.com/quilibrium/monorepo/node/keys" |
| 19 | +) |
| 20 | + |
| 21 | +var crossMintCmd = &cobra.Command{ |
| 22 | + Use: "cross-mint", |
| 23 | + Short: "Signs a payload from the Quilibrium bridge to mint tokens on Ethereum L1 and prints the result to stdout", |
| 24 | + Long: `Signs a payload from the Quilibrium bridge to mint tokens on Ethereum L1 and prints the result to stdout": |
| 25 | + |
| 26 | + cross-mint <Payload> |
| 27 | + |
| 28 | + Payload – the hex-encoded payload from the Quilibrium bridge with optional 0x-prefix, must be specified |
| 29 | + `, |
| 30 | + Run: func(cmd *cobra.Command, args []string) { |
| 31 | + if len(args) != 1 { |
| 32 | + fmt.Printf("missing payload") |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + |
| 36 | + _, err := os.Stat(configDirectory) |
| 37 | + if os.IsNotExist(err) { |
| 38 | + fmt.Printf("config directory doesn't exist: %s", configDirectory) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + config, err := config.LoadConfig(configDirectory, "") |
| 43 | + if err != nil { |
| 44 | + fmt.Printf("invalid config directory: %s", configDirectory) |
| 45 | + os.Exit(1) |
| 46 | + } |
| 47 | + |
| 48 | + rawPeerKey, err := hex.DecodeString(config.P2P.PeerPrivKey) |
| 49 | + if err != nil { |
| 50 | + panic(errors.Wrap(err, "cross mint")) |
| 51 | + } |
| 52 | + |
| 53 | + peerKey, err := libP2pCrypto.UnmarshalEd448PrivateKey(rawPeerKey) |
| 54 | + if err != nil { |
| 55 | + panic(errors.Wrap(err, "cross mint")) |
| 56 | + } |
| 57 | + |
| 58 | + rawPeerKey, err = peerKey.Raw() |
| 59 | + if err != nil { |
| 60 | + panic(errors.Wrap(err, "cross mint")) |
| 61 | + } |
| 62 | + |
| 63 | + // TODO: support other key managers |
| 64 | + // Get the proving key |
| 65 | + // `config.Key.KeyStoreFile.Path` defaults to `.config/keys.yml`. |
| 66 | + // We do our best here to make sure the configuration value is taken into |
| 67 | + // account if it was changed. |
| 68 | + if !filepath.IsAbs(config.Key.KeyStoreFile.Path) { |
| 69 | + config.Key.KeyStoreFile.Path = filepath.Join( |
| 70 | + configDirectory, |
| 71 | + filepath.Base(config.Key.KeyStoreFile.Path), |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + logger, err := zap.NewProduction() |
| 76 | + if err != nil { |
| 77 | + panic(errors.Wrap(err, "cross mint")) |
| 78 | + } |
| 79 | + |
| 80 | + fileKeyManager := keys.NewFileKeyManager(config.Key, logger) |
| 81 | + provingKey, err := fileKeyManager.GetSigningKey(config.Engine.ProvingKeyId) |
| 82 | + if err != nil { |
| 83 | + panic(errors.Wrap(err, "cross mint")) |
| 84 | + } |
| 85 | + // Sign the payload |
| 86 | + result, err := CrossMint(&CrossMintArgs{ |
| 87 | + Payload: args[0], |
| 88 | + PeerKey: rawPeerKey, |
| 89 | + ProvingKey: provingKey.(ed448.PrivateKey), |
| 90 | + }) |
| 91 | + if err != nil { |
| 92 | + panic(errors.Wrap(err, "error cross minting")) |
| 93 | + } |
| 94 | + // Print the result |
| 95 | + jsonResult, err := json.Marshal(result) |
| 96 | + if err != nil { |
| 97 | + panic(errors.Wrap(err, "error marshaling result to json")) |
| 98 | + } |
| 99 | + fmt.Println(string(jsonResult)) |
| 100 | + }, |
| 101 | +} |
| 102 | + |
| 103 | +func init() { |
| 104 | + rootCmd.AddCommand(crossMintCmd) |
| 105 | +} |
| 106 | + |
| 107 | +// CrossMintArgs Arguments for the cross mint operation |
| 108 | +type CrossMintArgs struct { |
| 109 | + // Hex encoded payload with optional 0x prefix |
| 110 | + Payload string |
| 111 | + // The node's ed448 peer key |
| 112 | + PeerKey ed448.PrivateKey |
| 113 | + // The node's ed448 proving key |
| 114 | + ProvingKey ed448.PrivateKey |
| 115 | +} |
| 116 | + |
| 117 | +// CrossMintResult Result of the cross mint operation |
| 118 | +type CrossMintResult struct { |
| 119 | + // Base64 encoded peer public key |
| 120 | + PeerPublicKey string `json:"peerPublicKey"` |
| 121 | + // Base64 encoded signature of the payload with the peer private key |
| 122 | + PeerSignature string `json:"peerSignature"` |
| 123 | + // Base64 encoded prover public key |
| 124 | + ProverPublicKey string `json:"proverPublicKey"` |
| 125 | + // Base64 encoded signature of the payload with the prover private key |
| 126 | + ProverSignature string `json:"proverSignature"` |
| 127 | +} |
| 128 | + |
| 129 | +func CrossMint(args *CrossMintArgs) (*CrossMintResult, error) { |
| 130 | + rawPayload, err := decodeHexString(args.Payload) |
| 131 | + if err != nil { |
| 132 | + return nil, errors.Wrap(err, "cross mint") |
| 133 | + } |
| 134 | + |
| 135 | + peerSignature := ed448.Sign(args.PeerKey, rawPayload, "") |
| 136 | + peerPubKey, ok := args.PeerKey.Public().(ed448.PublicKey) |
| 137 | + if !ok { |
| 138 | + return nil, errors.Wrap( |
| 139 | + errors.New("error casting peer public key to ed448 public key"), |
| 140 | + "cross mint", |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + provingSignature := ed448.Sign( |
| 145 | + args.ProvingKey, |
| 146 | + rawPayload, |
| 147 | + "", |
| 148 | + ) |
| 149 | + provingPubKey, ok := args.ProvingKey.Public().(ed448.PublicKey) |
| 150 | + if !ok { |
| 151 | + return nil, errors.Wrap( |
| 152 | + errors.New("error casting proving public key to ed448 public key"), |
| 153 | + "cross mint", |
| 154 | + ) |
| 155 | + } |
| 156 | + return &CrossMintResult{ |
| 157 | + PeerPublicKey: base64.StdEncoding.EncodeToString(peerPubKey), |
| 158 | + PeerSignature: base64.StdEncoding.EncodeToString(peerSignature), |
| 159 | + ProverPublicKey: base64.StdEncoding.EncodeToString(provingPubKey), |
| 160 | + ProverSignature: base64.StdEncoding.EncodeToString(provingSignature), |
| 161 | + }, nil |
| 162 | +} |
| 163 | + |
| 164 | +// VerifyCrossMint Verify a cross-mint message. Returns true if both signatures |
| 165 | +// verify with the given public keys. |
| 166 | +func VerifyCrossMint(payload string, result *CrossMintResult) (bool, error) { |
| 167 | + payloadBytes, err := decodeHexString(payload) |
| 168 | + if err != nil { |
| 169 | + return false, err |
| 170 | + } |
| 171 | + peerPubKeyBytes, err := base64.StdEncoding.DecodeString(result.PeerPublicKey) |
| 172 | + if err != nil { |
| 173 | + return false, err |
| 174 | + } |
| 175 | + peerPubKey := ed448.PublicKey(peerPubKeyBytes) |
| 176 | + peerSignature, err := base64.StdEncoding.DecodeString(result.PeerSignature) |
| 177 | + if err != nil { |
| 178 | + return false, err |
| 179 | + } |
| 180 | + proverPubKeyBytes, err := base64.StdEncoding.DecodeString( |
| 181 | + result.ProverPublicKey, |
| 182 | + ) |
| 183 | + if err != nil { |
| 184 | + return false, err |
| 185 | + } |
| 186 | + proverPubKey := ed448.PublicKey(proverPubKeyBytes) |
| 187 | + proverSignature, err := base64.StdEncoding.DecodeString( |
| 188 | + result.ProverSignature, |
| 189 | + ) |
| 190 | + if err != nil { |
| 191 | + return false, err |
| 192 | + } |
| 193 | + peerSigOk := ed448.Verify(peerPubKey, payloadBytes, peerSignature, "") |
| 194 | + proverSigOk := ed448.Verify(proverPubKey, payloadBytes, proverSignature, "") |
| 195 | + return peerSigOk && proverSigOk, nil |
| 196 | +} |
| 197 | + |
| 198 | +func decodeHexString(hexStr string) ([]byte, error) { |
| 199 | + // Check if the string starts with '0x' and remove it |
| 200 | + if strings.HasPrefix(hexStr, "0x") { |
| 201 | + hexStr = hexStr[2:] |
| 202 | + } |
| 203 | + // Decode the hex string into bytes |
| 204 | + data, err := hex.DecodeString(hexStr) |
| 205 | + if err != nil { |
| 206 | + return nil, errors.Wrap(err, "error decoding hex string") |
| 207 | + } |
| 208 | + return data, nil |
| 209 | +} |
0 commit comments