Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a new transaction for creating new account with coa #130

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions cadence/transactions/evm/create_new_account_with_coa.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Crypto

import "EVM"

/// Creates a new Flow Address with a single full-weight key and its EVM account, which is
/// a Cadence Owned Account (COA) stored in the account's storage.
///
transaction(
key: String, // key to be used for the account
signatureAlgorithm: UInt8, // signature algorithm to be used for the account
hashAlgorithm: UInt8, // hash algorithm to be used for the account
) {
let auth: auth(Storage, Keys, Capabilities) &Account

prepare(signer: auth(Storage, Keys, Capabilities) &Account) {
btspoony marked this conversation as resolved.
Show resolved Hide resolved
pre {
signatureAlgorithm == 1 || signatureAlgorithm == 2:
"Cannot add Key: Must provide a signature algorithm raw value that corresponds to "
.concat("one of the available signature algorithms for Flow keys.")
.concat("You provided ").concat(signatureAlgorithm.toString())
.concat(" but the options are either 1 (ECDSA_P256), 2 (ECDSA_secp256k1).")
hashAlgorithm == 1 || hashAlgorithm == 3:
"Cannot add Key: Must provide a hash algorithm raw value that corresponds to "
.concat("one of of the available hash algorithms for Flow keys.")
.concat("You provided ").concat(hashAlgorithm.toString())
.concat(" but the options are 1 (SHA2_256), 3 (SHA3_256).")
}

self.auth = signer
}

execute {
// Create a new public key
let publicKey = PublicKey(
publicKey: key.decodeHex(),
signatureAlgorithm: SignatureAlgorithm(rawValue: signatureAlgorithm)!
)

// Create a new account
let account = Account(payer: self.auth)

// Add the public key to the account
account.keys.add(
publicKey: publicKey,
hashAlgorithm: HashAlgorithm(rawValue: hashAlgorithm)!,
weight: 1000.0
)

// Create a new COA
let coa <- EVM.createCadenceOwnedAccount()

// Save the COA to the new account
let storagePath = StoragePath(identifier: "evm")!
let publicPath = PublicPath(identifier: "evm")!
account.storage.save<@EVM.CadenceOwnedAccount>(<-coa, to: storagePath)
let addressableCap = account.capabilities.storage.issue<&EVM.CadenceOwnedAccount>(storagePath)
account.capabilities.unpublish(publicPath)
account.capabilities.publish(addressableCap, at: publicPath)
}
}
Loading