exchange.IO("hash", ...)
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
| Type | Description |
string / object | Returns the result encoded according to |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Function of |
algo | string | Yes | Algorithm: digests (such as |
inputFormat | string | Yes | The format of |
outputFormat | string | Yes | The output format: |
data | string | Yes | The data to process, parsed according to |
keyFormat | string | No | The format of |
key | string | No | The key: the HMAC key, the private key used by |
See Also
Web3 exchange.IO exchange.IO("sign") exchange.IO("signTypedData") exchange.IO("encodePacked") Encode
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 assha512.384),sha512,sha512.224,sha512.256,keccak256(same assha3.keccak256),sha3.keccak512,sha3.224,sha3.256,sha3.384,sha3.512,ripemd160,blake2b.256,blake2b.512,blake2s.128,blake2s.256. WithkeyFormatandkey, an HMAC is computed (a keyed MAC for blake2s). raw,string: no computation, format conversion only.sign: signs the 32-bytedatawith a secp256k1 private key and returns the 65-byter‖s‖v, where v is 0 or 1; withoutkey, the private key configured on the exchange object is used.signTx:datais the JSON of a legacy transaction with the fieldsNonce,GasPrice,GasLimit,To,Value,DataandChainId(field names are case-insensitive, missing numbers count as 0), optionally with aKeyfield giving the private key; the signed transaction is returned.abi.type: ABI-encodes one value, for exampleabi.uint256orabi.address.- Also supported:
ed25519(anded25519.seed,ed25519.sha512) signing,text.encoder.charsetandtext.decoder.charsetcharset conversion,aes.encryptandaes.decrypt(optionally with.cbc,.ecbor.cfb, cbc by default),zlib,gzip,flate,br,zstdandlz4followed by.deflate(compress) or.inflate(decompress), andunzip(returns an object keyed by file name).