Skip to content

Commit

Permalink
fix: migrate breaking v5 breaking fns
Browse files Browse the repository at this point in the history
  • Loading branch information
phanshiyu committed Jul 22, 2024
1 parent aa5fa6d commit 93ebe4c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/2.0/__tests__/sign.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SUPPORTED_SIGNING_ALGORITHM } from "../../shared/@types/sign";
import { signDocument, v2 } from "../../index";
import rawWrappedDocumentV2 from "../../../test/fixtures/v2/did-wrapped.json";
import { Wallet } from "ethers";
import { HDNodeWallet as Wallet } from "ethers";

const wrappedDocumentV2 = rawWrappedDocumentV2 as v2.WrappedDocument;

Expand All @@ -20,7 +20,7 @@ describe("v2", () => {
);
});
it("should sign a document with a wallet", async () => {
const wallet = Wallet.fromMnemonic(
const wallet = Wallet.fromPhrase(
"tourist quality multiply denial diary height funny calm disease buddy speed gold"
);
const { proof } = await signDocument(
Expand Down
2 changes: 1 addition & 1 deletion src/3.0/__tests__/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("v3", () => {
);
});
it("should sign a document with a wallet", async () => {
const wallet = Wallet.fromMnemonic(
const wallet = Wallet.fromPhrase(
"tourist quality multiply denial diary height funny calm disease buddy speed gold"
);
const { proof } = await signDocument(
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { obfuscateVerifiableCredential } from "./3.0/obfuscate";
import { WrapDocumentOptionV2, WrapDocumentOptionV3 } from "./shared/@types/wrap";
import { SchemaValidationError } from "./shared/utils";
import { SigningKey, SUPPORTED_SIGNING_ALGORITHM } from "./shared/@types/sign";
import { ethers, Signer } from "ethers";
import { ethers, AbstractSigner as Signer } from "ethers";
import { getSchema } from "./shared/ajv";

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function signDocument(
keyOrSigner: SigningKey | ethers.Signer
) {
// rj was worried it could happen deep in the code, so I moved it to the boundaries
if (!SigningKey.guard(keyOrSigner) && !Signer.isSigner(keyOrSigner)) {
if (!SigningKey.guard(keyOrSigner) && !(keyOrSigner instanceof Signer)) {
throw new Error(`Either a keypair or ethers.js Signer must be provided`);
}
switch (true) {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/@types/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
WrappedDocument as WrappedDocumentV3,
} from "../../3.0/types";
import { Literal, Static, String } from "runtypes";
import { ethers } from "ethers";
import { isHexString } from "ethers";

export type OpenAttestationDocument = OpenAttestationDocumentV2 | OpenAttestationDocumentV3;
export type WrappedDocument<T extends OpenAttestationDocument> = T extends OpenAttestationDocumentV2
Expand All @@ -30,7 +30,7 @@ export enum SchemaId {
}

export const OpenAttestationHexString = String.withConstraint(
(value) => ethers.utils.isHexString(`0x${value}`, 32) || `${value} has not the expected length of 32 bytes`
(value) => isHexString(`0x${value}`, 32) || `${value} has not the expected length of 32 bytes`
);

export const SignatureAlgorithm = Literal("OpenAttestationMerkleProofSignature2018");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Wallet, utils, ethers } from "ethers";
import { Wallet, ethers } from "ethers";
import { SigningFunction, SigningKey, SigningOptions } from "../../../@types/sign";

export const name = "Secp256k1VerificationKey2018";
Expand All @@ -18,5 +18,5 @@ export const sign: SigningFunction = (
} else {
signer = keyOrSigner;
}
return signer.signMessage(options.signAsString ? message : utils.arrayify(message));
return signer.signMessage(options.signAsString ? message : ethers.getBytes(message));
};
8 changes: 6 additions & 2 deletions src/shared/utils/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from "ethers";
import { Logger } from "@ethersproject/logger";
import { SchemaId } from "../@types/document";
import { validateSchema as validate } from "../validate";
import {
Expand All @@ -9,17 +9,21 @@ import {
import { ArrayProof, Signature, SignatureStrict } from "../../2.0/types";
import { getSchema } from "../ajv";
import { Kind, Mode } from "./@types/diagnose";
import { version } from "ethers";

type Version = "2.0" | "3.0";

//https://github.com/ethers-io/ethers.js/blob/ec1b9583039a14a0e0fa15d0a2a6082a2f41cf5b/packages/ethers/src.ts/ethers.ts#L36
const ethersLogger = new Logger(version);

interface DiagnoseError {
message: string;
}

const handleError = (debug: boolean, ...messages: string[]) => {
if (debug) {
for (const message of messages) {
logger.info(message);
ethersLogger.info(message);
}
}
return messages.map((message) => ({ message }));
Expand Down

0 comments on commit 93ebe4c

Please sign in to comment.