Skip to content

Commit 3361b70

Browse files
authored
Merge pull request #1517 from onflow/brian-doyle/add-cadence-plus-coa
Add info on creating both accounts at once
2 parents c0dcb13 + 72b9e11 commit 3361b70

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/blockchain-development-tutorials/cross-vm-apps/interacting-with-coa.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,45 @@ transaction() {
135135
}
136136
```
137137

138+
### Creating a Cadence Account and COA together
139+
140+
It is possible to create a new Cadence account and COA within the same transaction. This transaction will need to be signed and paid for by another account, but any account will do. A common process is to set up a backend service to handle this function.
141+
142+
:::info
143+
144+
During the singular transaction in which an account is created, the `AuthAccount` object for the newly created account is present. As a result, the creating account can access and modify the new account's storage **only** during this transaction.
145+
146+
:::
147+
148+
First, you'll need to use the CLI to [generate keys](../../build/tools/flow-cli/keys/generate-keys.md) for the new account. Then, simply run the following transaction to create the Cadence Account and COA at once.
149+
150+
:::warning
151+
152+
This is a very minimal example. You may wish to set up vaults and perform other actions during account creation.
153+
154+
:::
155+
156+
```cadence
157+
import Crypto
158+
159+
transaction(publicKeys: [Crypto.KeyListEntry]) {
160+
prepare(signer: auth(BorrowValue) &Account) {
161+
162+
let newAccount = Account(payer: signer)
163+
164+
for key in publicKeys {
165+
newAccount.keys.add(publicKey: key.publicKey, hashAlgorithm: key.hashAlgorithm, weight: key.weight)
166+
}
167+
168+
let coa <- EVM.createCadenceOwnedAccount()
169+
let coaPath = /storage/evm
170+
newAccount.storage.save(<-coa, to: coaPath)
171+
let coaCapability = newAccount.capabilities.storage.issue<&EVM.CadenceOwnedAccount>(coaPath)
172+
newAccount.capabilities.publish(coaCapability, at: /public/evm)
173+
}
174+
}
175+
```
176+
138177
## Getting the EVM Address of a COA
139178

140179
To get the EVM address of a COA, you can use the `address` function from the `EVM` contract. This function returns the

0 commit comments

Comments
 (0)