Type/to search
Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
Capability Matrix
Centralized Exchanges
Decentralized Exchanges (DEX)
Prediction Markets
Securities and Futures
On-chain & Custom

Computes hash digests and HMACs, signs with the private key configured on the exchange object, and so on. Its parameters are the same as those of the Encode function. On a Web3 exchange object it is commonly used for keccak256 hashes, such as method selectors and EIP-712 digests.

exchange.IO(k, algo, inputFormat, outputFormat, data)
exchange.IO(k, algo, inputFormat, outputFormat, data, keyFormat, key)

Examples

Compute a method selector, an HMAC and an ABI encoding, and sign a hash with the configured private key.

javascript
function main() { // Method selector: the first 4 bytes of keccak256("transfer(address,uint256)") var h = exchange.IO("hash", "keccak256", "raw", "hex", "transfer(address,uint256)") Log("selector:", "0x" + h.slice(0, 8)) // Output: 0xa9059cbb // Hash hex data; the output has no 0x Log(exchange.IO("hash", "keccak256", "hex", "hex", "0x1234")) // HMAC-SHA256: the last two arguments are the key format and the key Log(exchange.IO("hash", "sha256", "string", "base64", "message", "string", "secret")) // ABI-encode a uint256 Log(exchange.IO("hash", "abi.uint256", "string", "hex", "1000")) // Sign a 32-byte hash with the configured key: 65 bytes r‖s‖v, v is 0 or 1 Log(exchange.IO("hash", "sign", "hex", "hex", h)) }
python
def main(): # Method selector: the first 4 bytes of keccak256("transfer(address,uint256)") h = exchange.IO("hash", "keccak256", "raw", "hex", "transfer(address,uint256)") Log("selector:", "0x" + h[:8]) # Output: 0xa9059cbb # Hash hex data; the output has no 0x Log(exchange.IO("hash", "keccak256", "hex", "hex", "0x1234")) # HMAC-SHA256: the last two arguments are the key format and the key Log(exchange.IO("hash", "sha256", "string", "base64", "message", "string", "secret")) # ABI-encode a uint256 Log(exchange.IO("hash", "abi.uint256", "string", "hex", "1000")) # Sign a 32-byte hash with the configured key: 65 bytes r‖s‖v, v is 0 or 1 Log(exchange.IO("hash", "sign", "hex", "hex", h))
rust
fn main() { // Method selector: the first 4 bytes of keccak256("transfer(address,uint256)") let hv = exchange.IO(("hash", "keccak256", "raw", "hex", "transfer(address,uint256)")); let Ok(hv) = hv else { return }; let h = hv.as_str().unwrap_or("").to_string(); Log!("selector:", format!("0x{}", &h[..8])); // Output: 0xa9059cbb // Hash hex data; the output has no 0x Log!(exchange.IO(("hash", "keccak256", "hex", "hex", "0x1234"))); // HMAC-SHA256: the last two arguments are the key format and the key; // pass a Vec<Arg> when there are more than 6 arguments let args: Vec<Arg> = ["hash", "sha256", "string", "base64", "message", "string", "secret"] .iter().map(|s| Arg::Str(s.to_string())).collect(); Log!(exchange.IO(args)); // ABI-encode a uint256 Log!(exchange.IO(("hash", "abi.uint256", "string", "hex", "1000"))); // Sign a 32-byte hash with the configured key: 65 bytes r‖s‖v, v is 0 or 1 Log!(exchange.IO(("hash", "sign", "hex", "hex", h.as_str()))); }

Returns

TypeDescription

string / object

Returns the result encoded according to outputFormat; unzip returns an object keyed by file name. Returns a null value on failure, with the error in GetLastError().

Arguments

NameTypeRequiredDescription

k

string

Yes

Function of exchange.IO(); hash means hashing, signing and similar computations.

algo

string

Yes

Algorithm: digests (such as sha256 or keccak256, with HMAC support), format conversion (raw, string), signing (sign, signTx), ABI encoding (abi.<type>), and Ed25519, character sets, AES and compression; see the remarks for all values.

inputFormat

string

Yes

The format of data: raw, string (the bytes of the string), hex (0x prefix optional), base64, base64.url, base64.rawurl.

outputFormat

string

Yes

The output format: hex (without 0x), base64, base64.url, base64.rawurl, raw, string (the output must be valid UTF-8 text).

data

string

Yes

The data to process, parsed according to inputFormat.

keyFormat

string

No

The format of key, with the same values as inputFormat.

key

string

No

The key: the HMAC key, the private key used by sign (for example its hex when keyFormat is hex), or the ed25519 or AES key. Required for ed25519 and aes.*. An AES key is 16, 24 or 32 bytes; the IV is the first 16 bytes of the key and no padding is applied (cbc and ecb need data whose length is a multiple of 16).

See Also

Remarks

There must be 4 or 6 arguments (not counting hash).

The signature returned by sign has v as 0 or 1, as in earlier versions; to get r, s and v (v as 27 or 28) separately, or the EIP-2098 compact signature, for on-chain verification, use exchange.IO("sign", ...).

Works on both Ethereum (EVM) and TRON exchange objects.

Values of algo:

  • Digests: md4, md5, sha1, sha224, sha256, sha384 (same as sha512.384), sha512, sha512.224, sha512.256, keccak256 (same as sha3.keccak256), sha3.keccak512, sha3.224, sha3.256, sha3.384, sha3.512, ripemd160, blake2b.256, blake2b.512, blake2s.128, blake2s.256. With keyFormat and key, an HMAC is computed (a keyed MAC for blake2s).
  • raw, string: no computation, format conversion only.
  • sign: signs the 32-byte data with a secp256k1 private key and returns the 65-byte r‖s‖v, where v is 0 or 1; without key, the private key configured on the exchange object is used.
  • signTx: data is the JSON of a legacy transaction with the fields Nonce, GasPrice, GasLimit, To, Value, Data and ChainId (field names are case-insensitive, missing numbers count as 0), optionally with a Key field giving the private key; the signed transaction is returned.
  • abi.type: ABI-encodes one value, for example abi.uint256 or abi.address.
  • Also supported: ed25519 (and ed25519.seed, ed25519.sha512) signing, text.encoder.charset and text.decoder.charset charset conversion, aes.encrypt and aes.decrypt (optionally with .cbc, .ecb or .cfb, cbc by default), zlib, gzip, flate, br, zstd and lz4 followed by .deflate (compress) or .inflate (decompress), and unzip (returns an object keyed by file name).