onflow-cadence

Can a byte array be converted to an Address in Cadence


Is there way to construct an Address from a byte array of UInt8 in Cadence?

Docs have this example:

let someAddress: Address = 0x436164656E636521
someAddress.toBytes()

What I've tried:

let hexStr = String.encodeHex([0,0,0,0,0,0,0,1])
let addrStr = "0x".concat(hexStr) // gives a string in format '0x0000000000000001' but how cast to Address type?

Possible to do UInt8 byte array to Address?

The use case is to build an Address in a tx from an RLP-encoded data function argument.


Solution

  • This contract has some utilities for working with Addresses and parsing them from other types. Sending in a string here works for instance.

    https://github.com/green-goo-dao/flow-utils/blob/main/cadence/contracts/AddressUtils.cdc