|
| 1 | +import { Cryptography, Hashing } from "@/encryption" |
| 2 | +import { |
| 3 | + BridgeOperation, |
| 4 | + BridgeOperationCompiled, |
| 5 | + SupportedChain, |
| 6 | + SupportedEVMChain, |
| 7 | + SupportedStablecoin, |
| 8 | + supportedEVMChains, |
| 9 | + SupportedNonEVMChain, |
| 10 | + supportedNonEVMChains, |
| 11 | +} from "./nativeBridgeTypes" |
| 12 | +import { Transaction } from "@/types/blockchain/Transaction" |
| 13 | +import { RPCRequest } from "@/types" |
| 14 | + |
| 15 | +export const methods = { |
| 16 | + /** |
| 17 | + * Validates the chain |
| 18 | + * @param chain |
| 19 | + * @param chainType |
| 20 | + * @param isOrigin (useful for error messages) |
| 21 | + */ |
| 22 | + validateChain ( |
| 23 | + chain: string, |
| 24 | + chainType: string, |
| 25 | + isOrigin: boolean, |
| 26 | + ) { |
| 27 | + const chainTypeStr = isOrigin ? "origin" : "destination" |
| 28 | + if (chainType === "EVM") { |
| 29 | + if (!supportedEVMChains.includes(chain as SupportedEVMChain)) { |
| 30 | + throw new Error( |
| 31 | + `Invalid ${chainTypeStr} chain: ${chain} is not a supported EVM`, |
| 32 | + ) |
| 33 | + } |
| 34 | + } else { |
| 35 | + if ( |
| 36 | + !supportedNonEVMChains.includes( |
| 37 | + chain as SupportedNonEVMChain, |
| 38 | + ) |
| 39 | + ) { |
| 40 | + throw new Error( |
| 41 | + `Invalid ${chainTypeStr} chain: ${chain} is not a supported chain`, |
| 42 | + ) |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + /** |
| 47 | + * Generates a new operation, ready to be sent to the node as a RPCRequest |
| 48 | + * TODO Implement the params |
| 49 | + * REVIEW Should we use the identity somehow or we keep using the private key? |
| 50 | + */ |
| 51 | + generateOperation( |
| 52 | + privateKey: string, |
| 53 | + publicKey: string, |
| 54 | + originChainType: SupportedChain, |
| 55 | + originChain: SupportedEVMChain | SupportedNonEVMChain, |
| 56 | + destinationChainType: SupportedChain, |
| 57 | + destinationChain: SupportedEVMChain | SupportedNonEVMChain, |
| 58 | + originAddress: string, |
| 59 | + destinationAddress: string, |
| 60 | + amount: string, |
| 61 | + token: SupportedStablecoin, |
| 62 | + ): RPCRequest { |
| 63 | + // Ensuring the chains are valid: throw an error if not |
| 64 | + this.validateChain(originChain, originChainType, true) |
| 65 | + this.validateChain(destinationChain, destinationChainType, false) |
| 66 | + // Defining the operation |
| 67 | + const operation: BridgeOperation = { |
| 68 | + demoAddress: publicKey, |
| 69 | + originChainType: originChainType, |
| 70 | + originChain: originChain, |
| 71 | + destinationChainType: destinationChainType, |
| 72 | + destinationChain: destinationChain, |
| 73 | + originAddress: originAddress, |
| 74 | + destinationAddress: destinationAddress, |
| 75 | + amount: amount, |
| 76 | + token: token, |
| 77 | + txHash: "", |
| 78 | + status: "empty", |
| 79 | + } |
| 80 | + // REVIEW Sign the operation |
| 81 | + let opHash = Hashing.sha256(JSON.stringify(operation)) |
| 82 | + let signature = Cryptography.sign(opHash, privateKey) |
| 83 | + let hexSignature = new TextDecoder().decode(signature) |
| 84 | + let nodeCallPayload: RPCRequest = { |
| 85 | + method: "nativeBridge", |
| 86 | + params: [operation, hexSignature], |
| 87 | + } |
| 88 | + return nodeCallPayload |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * |
| 93 | + * @param compiled operation |
| 94 | + * @param signature |
| 95 | + * @param rpc |
| 96 | + * @returns |
| 97 | + */ |
| 98 | + generateOperationTx(compiled: BridgeOperationCompiled): Transaction { |
| 99 | + // TODO Implement the transaction once we have the compiled operation |
| 100 | + // Preparing the known values for the transaction |
| 101 | + const tx: Transaction = { |
| 102 | + content: { |
| 103 | + type: "nativeBridge", |
| 104 | + data: ["nativeBridge", compiled], |
| 105 | + from: "", // TODO Implement from using the identity |
| 106 | + to: "", // TODO Same as from |
| 107 | + amount: 0, |
| 108 | + gcr_edits: [], |
| 109 | + nonce: 0, |
| 110 | + timestamp: Date.now(), |
| 111 | + transaction_fee: { // TODO Compile with the BridgeOperationCompiled object content |
| 112 | + network_fee: 0, |
| 113 | + rpc_fee: 0, |
| 114 | + additional_fee: 0, |
| 115 | + }, |
| 116 | + }, |
| 117 | + signature: { |
| 118 | + type: "ed25519", |
| 119 | + data: "", |
| 120 | + }, |
| 121 | + hash: "", |
| 122 | + status: "empty", |
| 123 | + blockNumber: 0, |
| 124 | + } |
| 125 | + // TODO Hash and sign the transaction |
| 126 | + return tx |
| 127 | + }, |
| 128 | +} |
0 commit comments