Skip to content

ISCC - Utilities#

canonicalize(obj) #

Canonical, deterministic serialization of ISCC metadata.

We serialize ISCC metadata in a deterministic/reproducible manner by using JCS (RFC 8785) canonicalization.

ipfs_hash(stream) #

Create an IPFS hash for ISCC metadata.

We use a specialized base16 encoded CIDv1 with sha3-224 and chunksize 1048576 as hashing algorithm for ISCC metadata.

Example

With IPFS v0.11.0 this equals to:

$ipfs add --cid-version=1 --chunker=size-1048576 --hash=sha3-224 <myfile>
<my-cid>
$ipfs cid format -b=base16 <my-cid>

Note

The rationale for this trickery is that we want to be able to use an IPFS hash as an ERC-721/ERC-1155 uint256 _tokenID and also support ID substitution for the metadata URI. For details see discussion at OpenZeppelin Forum

Learn more about IPFS CIDv1 at ProtoSchool

Parameters:

Name Type Description Default
stream Stream

Data to be hashed (currently max 1048576)

required

Returns:

Type Description
str

A valid IPFS CIDv1 that can be used as token-id and metadata-uri

sliding_window(seq, width) #

Generate a sequence of equal "width" slices each advancing by one elemnt.

All types that have a length and can be sliced are supported (list, tuple, str ...). The result type matches the type of the input sequence. Fragment slices smaller than the width at the end of the sequence are not produced. If "witdh" is smaller than the input sequence than one element will be returned that is shorter than the requested width.

Parameters:

Name Type Description Default
seq Sequence

Sequence of values to slide over

required
width int

Width of sliding window in number of items

required

Returns:

Type Description
Union[Generat,]

A generator of window sized items

similarity(a, b) #

Calculate similarity of ISCC codes as a percentage value (0-100).

MainType, SubType, Version and Length of the codes must be the same.

Parameters:

Name Type Description Default
a

ISCC a

required
b

ISCC b

required

Returns:

Type Description
int

Similarity of ISCC a and b in percent (based on hamming distance)

distance(a, b) #

Calculate hamming distance of ISCC codes.

MainType, SubType, Version and Length of the codes must be the same.

Parameters:

Name Type Description Default
a

ISCC a

required
b

ISCC b

required

Returns:

Type Description
int

Hamming distanced in number of bits.

hamming_distance(a, b) #

Calculate hamming distance for binary hash digests of equal length.

Parameters:

Name Type Description Default
a bytes

binary hash digest

required
b bytes

binary hash digest

required

Returns:

Type Description
int

Hamming distance in number of bits.