Data Hasher
The @coinweb/data-hasher
is an in-house smart contract created for storing,
hashing and linking supplied file contents. It is adequately simple to
reconstruct the @coinweb/data-hasher
from the underlying
@coinweb/contract-kit
.
function dataHasherLogic(
issuer: ClaimIssuer,
encodedFiles: Uint8Array[]
): NewTx {
const storeOps = encodedFiles.map((file) => {
const body = uint8ToBase64(file);
const hash = hashV0(file);
return store(genericClaim(claimKey(hash, null), body, toHex(0)));
});
return continueTx([
passCwebFrom(issuer, 100 * storeOps.length + 100),
...storeOps,
]);
}
function logic(context: TxContext, _callInfo: CallContext): NewTx[] {
const self = contractIssuer(getContractId(context));
const files = getMethodArguments(context)[1].map((encodedFile) =>
base64ToUint8(encodedFile)
);
return [dataHasherLogic(self, files)];
}
The dataHasherLogic
function takes an array of the encoded files contents and
creates StoreOps
with the explicit file content for each file. The logic
function is the default method contract handler, that is executed when the
contract is being called.
note
The example
of how the @coinweb/data-hasher
is integrated can be helpful.