Skip to content

Commit

Permalink
Merge pull request #40 from shamilovtim/tshamilov/remove-multihashes-…
Browse files Browse the repository at this point in the history
…bump-deps

remove multihashes. bump deps.
  • Loading branch information
csuwildcat authored Dec 12, 2023
2 parents 64cdfa3 + 94fc049 commit 1b03fc2
Show file tree
Hide file tree
Showing 8 changed files with 752 additions and 1,926 deletions.
3 changes: 2 additions & 1 deletion lib/IonDid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Encoder from './Encoder.js';
import IonCreateRequestModel from './models/IonCreateRequestModel.js';
import IonDocumentModel from './models/IonDocumentModel.js';
import IonRequest from './IonRequest.js';
import IonSdkConfig from './IonSdkConfig.js';
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class IonDid {
/**
* Computes the DID unique suffix given the encoded suffix data string.
*/
private static async computeDidUniqueSuffix (suffixData: object): Promise<string> {
private static async computeDidUniqueSuffix (suffixData: IonCreateRequestModel['suffixData']): Promise<string> {
const canonicalizedStringBytes = JsonCanonicalizer.canonicalizeAsBytes(suffixData);
const multihash = await Multihash.hash(canonicalizedStringBytes, IonSdkConfig.hashAlgorithmInMultihashCode);
const encodedMultihash = Encoder.encode(multihash);
Expand Down
2 changes: 1 addition & 1 deletion lib/IonSdkConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class IonSdkConfig {
/**
* Default hash algorithm used when hashing is performed.
*/
public static hashAlgorithmInMultihashCode = 18; // SHA256
public static hashAlgorithmInMultihashCode = 18; // 0x12 or SHA256

/**
* Maximum bytes for canonicalized delta.
Expand Down
1 change: 1 addition & 0 deletions lib/JsonCanonicalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class JsonCanonicalizer {
public static canonicalizeAsBytes (content: object): Uint8Array {
// We need to remove all properties with `undefined` as value so that JCS canonicalization will not produce invalid JSON.
const contentWithoutUndefinedProperties = JsonCanonicalizer.removeAllUndefinedProperties(content);
// @ts-expect-error because its a cjs package
const canonicalizedString: string = canonicalize.default(contentWithoutUndefinedProperties)!;
const contentBytes = Encoder.stringToBytes(canonicalizedString);
return contentBytes;
Expand Down
26 changes: 18 additions & 8 deletions lib/Multihash.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import * as multihashes from 'multihashes';
import Encoder from './Encoder.js';
import ErrorCode from './ErrorCode.js';
import { HashCode } from 'multihashes';
import IonError from './IonError.js';
import IonSdkConfig from './IonSdkConfig.js';
import JsonCanonicalizer from './JsonCanonicalizer.js';
import { decode } from 'multiformats/hashes/digest';
import { sha256 } from 'multiformats/hashes/sha2';

/**
* Class that performs hashing operations using the multihash format.
*/
export default class Multihash {
/**
* Hashes the content using the hashing algorithm specified.
* Multihashes the content using the hashing algorithm specified.
* @param hashAlgorithmInMultihashCode The hashing algorithm to use.
* @returns A multihash of the content.
*/
public static async hash (content: Uint8Array, hashAlgorithmInMultihashCode: number): Promise<Uint8Array> {
const conventionalHash = await this.hashAsNonMultihashBytes(content, hashAlgorithmInMultihashCode);
const multihash = multihashes.encode(conventionalHash, hashAlgorithmInMultihashCode as HashCode);

let multihash: Uint8Array;
switch (hashAlgorithmInMultihashCode) {
case 18: // SHA256
let hasher = await sha256.digest(content);
multihash = hasher.bytes;
break;
default:
throw new IonError(
ErrorCode.MultihashUnsupportedHashAlgorithm,
`Hash algorithm defined in multihash code ${hashAlgorithmInMultihashCode} is not supported.`
);
}

return multihash;
}

Expand Down Expand Up @@ -81,13 +91,13 @@ export default class Multihash {
* Checks if the given encoded hash is a multihash computed using the configured hashing algorithm.
*/
public static validateEncodedHashComputedUsingSupportedHashAlgorithm (
encodedMultihash: string,
encodedMultihash: string, // didSuffix
inputContextForErrorLogging: string
) {
let multihash;
const multihashBytes = Encoder.decodeAsBytes(encodedMultihash, inputContextForErrorLogging);
try {
multihash = multihashes.decode(multihashBytes);
multihash = decode(multihashBytes);
} catch {
throw new IonError(
ErrorCode.MultihashStringNotAMultihash,
Expand Down
Loading

0 comments on commit 1b03fc2

Please sign in to comment.