Skip to content

Utilities

The package exports the hash functions that Bitcoin uses. They take and return byte lists.

final data = utf8.encode('abc');
HEX.encode(sha256(data)); // ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
HEX.encode(sha256Twice(data)); // double SHA-256, used for Transaction and block IDs
HEX.encode(ripemd160(data)); // 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
HEX.encode(hash160(data)); // RIPEMD-160 of SHA-256, used in Addresses

The hash160 of a Public Key is the hash inside its Address:

final pubkeyHash = HEX.encode(hash160(HEX.decode(key.publicKey.toHex())));
pubkeyHash == key.toAddress(networkType: NetworkType.TEST).pubkeyHash160; // true

VarInt encodes the compact integers used for counts and lengths in the wire format.

VarInt.fromInt(100).encode(); // [100]: one byte below 0xfd
VarInt.fromInt(1000).encode(); // [0xfd, 0xe8, 0x03]: marker plus 2 bytes, little-endian
// Read one back from a stream of bytes (package:buffer, a dartsv dependency).
final reader = ByteDataReader()..add([0xfd, 0xe8, 0x03]);
VarInt.fromStream(reader).value; // 1000

For Addresses and WIF keys, we use Address and SVPrivateKey.fromWIF, which decode and check base58check themselves. Do note that the low-level decodeChecked function returns the payload as signed bytes (values from -128 to 127) in 3.1.2, which HEX.encode rejects. Convert with b & 0xff if you call it directly.