-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unhandled exception introduced in v4.0.9 #74
Comments
A fair point, I should probably revert the change and re-release as a major, sorry for the disruption. |
@achingbrain before you do publish again as a major I should point out how the plain arrays are getting in. the stack trace is below. I think they're entering through
|
It's already gone out as v5. I think the problem is here - IIRC the |
Can you upgrade to multiformats instead of using multihashes? It supports the same suite of multihash prefixes but has the advantage of actually being maintained... The multihash section of the readme shows how to use it: https://www.npmjs.com/package/multiformats#multihash-hashers |
Happy to do it but I don't think I have write access in ion sdk. I made a ticket. I think @thehenrytsai or @csuwildcat could do it |
@achingbrain I went ahead and tried to migrate that package but I'm seeing a diff in the hashes produced in the test suite. diff --git a/lib/Multihash.ts b/lib/Multihash.ts
index 6fbea78..8563850 100644
--- a/lib/Multihash.ts
+++ b/lib/Multihash.ts
@@ -1,10 +1,9 @@
-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';
/**
@@ -17,9 +16,18 @@ export default class Multihash {
*/
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);
-
- return multihash;
+
+ let bytes: Uint8Array;
+ switch (hashAlgorithmInMultihashCode) {
+ case 18: // SHA256
+ let hasher = await sha256.digest(conventionalHash);
+ bytes = hasher.bytes;
+ break;
+ default:
+ throw new Error(`Unsupported hash algorithm code: ${hashAlgorithmInMultihashCode}`);
+ }
+
+ return bytes;
}
/**
@@ -81,13 +89,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, Diffs produced by the test suite:
|
It looks like you are double-hashing - So instead of: const conventionalHash = await this.hashAsNonMultihashBytes(content, hashAlgorithmInMultihashCode);
const multihash = multihashes.encode(conventionalHash, hashAlgorithmInMultihashCode as HashCode);
return multihash; You'd just do: let multihash
switch (hashAlgorithmInMultihashCode) {
case sha256.code:
multihash = await sha256.digest(content);
return multihash.bytes;
default:
throw new Error(`Unsupported hash algorithm code: ${hashAlgorithmInMultihashCode}`);
} |
Prior to v4.0.9 if plain Arrays were passed into uint8arrays's own
concat
it would convert those Arrays into a Uint8Array using theasUint8Array
helper.In v4.0.9 a change was introduced such that:
The problem is that
globalThis.Buffer.concat(arrays, length)
is passing plain arrays into Buffer.concat beforeasUint8Array
ever has a chance to convert them, which throws an unhandled exception:Error: [TypeError: "list" argument must be an Array of Buffers]
The fix would either be to revert the change in v4.0.9 or to convert each non-buffer into a Uint8Array first prior to calling
globalThis.Buffer.concat(arrays, length)
. I assume any performance gains will need to be re-evaluated.The text was updated successfully, but these errors were encountered: