From 352fec45d13690e2ddfd969bd483b07565302e80 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Thu, 15 Feb 2024 13:57:55 -0600 Subject: [PATCH] remove CryptoUtils --- cadence/contracts/CryptoUtils.cdc | 34 ------------------- .../transactions/evm/create_coa_and_fund.cdc | 8 ++--- .../create_flow_account_with_coa_and_fund.cdc | 13 +++---- .../evm/mint_and_fund_evm_address.cdc | 11 +++--- env.example | 1 - flow.json | 9 +---- lib/fclConfig.ts | 1 - lib/flow/account.ts | 5 ++- lib/publicConfig.ts | 3 -- 9 files changed, 18 insertions(+), 67 deletions(-) delete mode 100644 cadence/contracts/CryptoUtils.cdc diff --git a/cadence/contracts/CryptoUtils.cdc b/cadence/contracts/CryptoUtils.cdc deleted file mode 100644 index c464d80..0000000 --- a/cadence/contracts/CryptoUtils.cdc +++ /dev/null @@ -1,34 +0,0 @@ -/// Util methods to reconstruct HashAlgorithm and SignatureAlgorithm from their raw enum values -/// -access(all) contract CryptoUtils { - - access(all) fun getHashAlgo(fromRawValue: UInt8): HashAlgorithm? { - switch fromRawValue { - case HashAlgorithm.SHA2_256.rawValue: - return HashAlgorithm.SHA2_256 - case HashAlgorithm.SHA2_384.rawValue: - return HashAlgorithm.SHA2_384 - case HashAlgorithm.SHA3_256.rawValue: - return HashAlgorithm.SHA3_256 - case HashAlgorithm.SHA3_384.rawValue: - return HashAlgorithm.SHA3_384 - case HashAlgorithm.KMAC128_BLS_BLS12_381.rawValue: - return HashAlgorithm.KMAC128_BLS_BLS12_381 - case HashAlgorithm.KECCAK_256.rawValue: - return HashAlgorithm.KECCAK_256 - default: - return nil - } - } - - access(all) fun getSigAlgo(fromRawValue: UInt8): SignatureAlgorithm? { - switch fromRawValue { - case SignatureAlgorithm.ECDSA_P256.rawValue: - return SignatureAlgorithm.ECDSA_P256 - case SignatureAlgorithm.ECDSA_secp256k1.rawValue: - return SignatureAlgorithm.ECDSA_secp256k1 - default: - return nil - } - } -} \ No newline at end of file diff --git a/cadence/transactions/evm/create_coa_and_fund.cdc b/cadence/transactions/evm/create_coa_and_fund.cdc index 7976805..316073d 100644 --- a/cadence/transactions/evm/create_coa_and_fund.cdc +++ b/cadence/transactions/evm/create_coa_and_fund.cdc @@ -3,7 +3,8 @@ import "FlowToken" import "EVM" -/// Creates a COA and saves it in the signer's Flow account & passing the given value of Flow into FlowEVM +/// Creates a COA and saves it in the signer's Flow account (if one doesn't already exist at the expected path) and +/// transfers the given value of Flow into FlowEVM, funding with the signer's Flow Vault. /// transaction(amount: UFix64) { let sentVault: @FlowToken.Vault @@ -15,12 +16,11 @@ transaction(amount: UFix64) { ) ?? panic("Could not borrow reference to the owner's Vault!") self.sentVault <- vaultRef.withdraw(amount: amount) as! @FlowToken.Vault - if signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/EVM) != nil { + if signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/evm) == nil { signer.storage.save(<-EVM.createBridgedAccount(), to: /storage/evm) } - self.coa = signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/EVM) + self.coa = signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/evm) ?? panic("Could not borrow reference to the signer's COA!") - } execute { diff --git a/cadence/transactions/evm/create_flow_account_with_coa_and_fund.cdc b/cadence/transactions/evm/create_flow_account_with_coa_and_fund.cdc index 5df9e16..8de4f8b 100644 --- a/cadence/transactions/evm/create_flow_account_with_coa_and_fund.cdc +++ b/cadence/transactions/evm/create_flow_account_with_coa_and_fund.cdc @@ -3,10 +3,9 @@ import "FlowToken" import "EVM" -import "CryptoUtils" - -/// Creates a Flow account with a COA and saves it in the created account. The provided amount is then deposited into -/// the Flow account at the ratio provided and the remaining amount is deposited into the newly created COA. +/// Creates a Flow account with a COA and saves it in the created account. The provided amount is then minted, +/// deposited into the Flow account at the specified ratio and the remaining amount is deposited into the newly +/// created COA. /// transaction( publicKey: String, @@ -22,10 +21,12 @@ transaction( prepare(signer: auth(Storage) &Account) { self.newAccount = Account(payer: signer) - let signatureAlgorithm = CryptoUtils.getSigAlgo(fromRawValue: sigAlgorithm) + let signatureAlgorithm = SignatureAlgorithm(sigAlgorithm) ?? panic("Invalid SignatureAlgorithm") - let hashAlgorithm = CryptoUtils.getHashAlgo(fromRawValue: sigAlgorithm) + log(signatureAlgorithm) + let hashAlgorithm = HashAlgorithm(hashAlgorithm) ?? panic("Invalid HashAlgorithm") + log(hashAlgorithm) let key = PublicKey( publicKey: publicKey.decodeHex(), diff --git a/cadence/transactions/evm/mint_and_fund_evm_address.cdc b/cadence/transactions/evm/mint_and_fund_evm_address.cdc index e471f95..696bece 100644 --- a/cadence/transactions/evm/mint_and_fund_evm_address.cdc +++ b/cadence/transactions/evm/mint_and_fund_evm_address.cdc @@ -11,12 +11,15 @@ transaction(to: EVM.EVMAddress, amount: UFix64, gasLimit: UInt64) { let tokenReceiver: &{FungibleToken.Receiver} let coa: &EVM.BridgedAccount - prepare(signer: auth(BorrowValue) &Account) { + prepare(signer: auth(Storage) &Account) { self.tokenAdmin = signer.storage.borrow<&FlowToken.Administrator>(from: /storage/flowTokenAdmin) ?? panic("Signer is not the token admin") self.tokenReceiver = signer.capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver) ?? panic("Unable to borrow receiver reference") + if signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/evm) == nil { + signer.storage.save(<-EVM.createBridgedAccount(), to: /storage/evm) + } self.coa = signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/evm) ?? panic("Could not borrow reference to the signer's COA!") } @@ -26,12 +29,6 @@ transaction(to: EVM.EVMAddress, amount: UFix64, gasLimit: UInt64) { let mintedVault <- minter.mintTokens(amount: amount) destroy minter - // TODO: REMOVE - let bytes = toStr.decodeHex() - let to = EVM.EVMAddress(bytes: [ - bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], - bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], bytes[16], bytes[17], bytes[18], bytes[19] - ]) self.coa.deposit(from: <-mintedVault) self.coa.call( to: to, diff --git a/env.example b/env.example index f491c60..fd34221 100644 --- a/env.example +++ b/env.example @@ -12,7 +12,6 @@ NEXT_PUBLIC_CONTRACT_FUNGIBLE_TOKEN=0xee82856bf20e2aa6 NEXT_PUBLIC_CONTRACT_FLOW_TOKEN=0x0ae53cb6e3f42a79 NEXT_PUBLIC_CONTRACT_FUSD=0xf8d6e0586b0a20c7 NEXT_PUBLIC_CONTRACT_EVM=0xf8d6e0586b0a20c7 -NEXT_PUBLIC_CONTRACT_CRYPTO_UTILS=0xf8d6e0586b0a20c7 NEXT_PUBLIC_GA_MEASUREMENT_ID= SIGNER_HASH_ALGO=SHA2_256 diff --git a/flow.json b/flow.json index d0b1e21..a7c0259 100644 --- a/flow.json +++ b/flow.json @@ -6,12 +6,6 @@ "emulator": "ee82856bf20e2aa6" } }, - "CryptoUtils": { - "source": "./cadence/contracts/CryptoUtils.cdc", - "aliases": { - "emulator": "f8d6e0586b0a20c7" - } - }, "EVM": { "source": "./cadence/contracts/EVM.cdc", "aliases": { @@ -108,8 +102,7 @@ "FungibleTokenMetadataViews", "Burner", "FUSD", - "ViewResolver", - "CryptoUtils" + "ViewResolver" ] } } diff --git a/lib/fclConfig.ts b/lib/fclConfig.ts index ddccd7f..5a63ca2 100644 --- a/lib/fclConfig.ts +++ b/lib/fclConfig.ts @@ -12,4 +12,3 @@ config() .put("0xFLOWTOKENADDRESS", publicConfig.contractFlowToken) .put("0xFUSDADDRESS", publicConfig.contractFUSD) .put("0xEVMADDRESS", publicConfig.contractEVM) - .put("0xCRYPTOUTILSADDRESS", publicConfig.contractCryptoUtils) diff --git a/lib/flow/account.ts b/lib/flow/account.ts index 8557458..9a819ff 100644 --- a/lib/flow/account.ts +++ b/lib/flow/account.ts @@ -7,7 +7,6 @@ import { sendTransaction } from "./send" const accountCreatedEventType = "flow.AccountCreated" const txCreateAccount = ` -import CryptoUtils from ${publicConfig.contractFlowToken} import FlowToken from ${publicConfig.contractFlowToken} import FungibleToken from ${publicConfig.contractFungibleToken} @@ -18,9 +17,9 @@ transaction(publicKey: String, flowTokenAmount: UFix64, sigAlgorithm: UInt8, has prepare(signer: auth(BorrowValue) &Account) { let account = Account(payer: signer) - let signatureAlgorithm = CryptoUtils.getSigAlgo(fromRawValue: sigAlgorithm) + let signatureAlgorithm = SignatureAlgorithm(sigAlgorithm) ?? panic("Invalid SignatureAlgorithm") - let hashAlgorithm = CryptoUtils.getHashAlgo(fromRawValue: sigAlgorithm) + let hashAlgorithm = HashAlgorithm(hashAlgorithm) ?? panic("Invalid HashAlgorithm") let key = PublicKey( diff --git a/lib/publicConfig.ts b/lib/publicConfig.ts index c3dd4b8..a07339d 100644 --- a/lib/publicConfig.ts +++ b/lib/publicConfig.ts @@ -30,9 +30,6 @@ if (!contractFUSD) throw "Missing NEXT_PUBLIC_CONTRACT_FUSD" const contractEVM = process.env.NEXT_PUBLIC_CONTRACT_EVM if (!contractEVM) throw "Missing NEXT_PUBLIC_CONTRACT_EVM" -const contractCryptoUtils = process.env.NEXT_PUBLIC_CONTRACT_CRYPTO_UTILS -if (!contractCryptoUtils) throw "Missing NEXT_PUBLIC_CONTRACT_CRYPTO_UTILS" - const walletDiscovery = process.env.NEXT_PUBLIC_WALLET_DISCOVERY // TODO: Integrate FCL wallets // if (!walletDiscovery) throw "Missing NEXT_PUBLIC_WALLET_DISCOVERY"