Skip to content
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

feat: ethers v6 latest #267

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,518 changes: 319 additions & 1,199 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
],
"license": "Apache-2.0",
"devDependencies": {
"@commitlint/cli": "^17.6.7",
"@commitlint/config-conventional": "^17.6.7",
"@commitlint/prompt": "^17.6.7",
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@commitlint/prompt": "^18.4.3",
"@rollup/plugin-json": "^4.1.0",
"@types/debug": "^4.1.7",
"@types/jest": "^29.5.3",
Expand Down Expand Up @@ -71,14 +71,14 @@
"rollup-plugin-commonjs": "^10.1.0",
"semantic-release": "^22.0.8",
"ts-jest": "^29.1.1",
"ts-node": "^9.1.1",
"ts-node": "^10.9.2",
"typescript": "^5.1.6"
},
"dependencies": {
"@govtechsg/jsonld": "^0.1.0",
"cross-fetch": "^3.1.5",
"debug": "^4.3.2",
"ethers": "^5.7.2",
"ethers": "^6.9.0",
"flatley": "^5.2.0",
"js-base64": "^3.6.1",
"js-sha3": "^0.8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/2.0/__tests__/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/2.0/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { sign } from "../shared/signer";
import { OpenAttestationDocument, SignedWrappedDocument } from "./types";
import { isSignedWrappedV2Document } from "../shared/utils";
import { SigningKey, SUPPORTED_SIGNING_ALGORITHM } from "../shared/@types/sign";
import { ethers } from "ethers";
import { Signer } from "ethers";

export const signDocument = async <T extends OpenAttestationDocument>(
document: SignedWrappedDocument<T> | WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: ethers.Signer | SigningKey
keyOrSigner: Signer | SigningKey
): Promise<SignedWrappedDocument<T>> => {
const merkleRoot = `0x${document.signature.merkleRoot}`;
const signature = await sign(algorithm, merkleRoot, keyOrSigner);
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/3.0/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
import { sign } from "../shared/signer";
import { SigningKey, SUPPORTED_SIGNING_ALGORITHM } from "../shared/@types/sign";
import { isSignedWrappedV3Document } from "../shared/utils";
import { ethers } from "ethers";
import { Signer } from "ethers";

export const signDocument = async <T extends OpenAttestationDocument>(
document: SignedWrappedDocument<T> | WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
keyOrSigner: SigningKey | Signer
): Promise<SignedWrappedDocument<T>> => {
if (isSignedWrappedV3Document(document)) throw new Error("Document has been signed");
const merkleRoot = `0x${document.proof.merkleRoot}`;
Expand Down
13 changes: 8 additions & 5 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 { Signer } from "ethers";
import { getSchema } from "./shared/ajv";

export function __unsafe__use__it__at__your__own__risks__wrapDocument<T extends OpenAttestationDocumentV3>(
Expand Down Expand Up @@ -74,24 +74,27 @@ export function obfuscate(document: any, fields: string[] | string) {
export const isSchemaValidationError = (error: any): error is SchemaValidationError => {
return !!error.validationErrors;
};
export const isSigner = (keyOrSigner: SigningKey | Signer): keyOrSigner is Signer => {
return (<Signer>keyOrSigner).getAddress !== undefined;
};

export async function signDocument<T extends v3.OpenAttestationDocument>(
document: v3.SignedWrappedDocument<T> | v3.WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
keyOrSigner: SigningKey | Signer
): Promise<v3.SignedWrappedDocument<T>>;
export async function signDocument<T extends v2.OpenAttestationDocument>(
document: v2.SignedWrappedDocument<T> | v2.WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
keyOrSigner: SigningKey | Signer
): Promise<v2.SignedWrappedDocument<T>>;
export async function signDocument(
document: any,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
keyOrSigner: SigningKey | 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) && !isSigner(keyOrSigner)) {
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
10 changes: 3 additions & 7 deletions src/shared/@types/sign.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ethers } from "ethers";
import { Signer } from "ethers";
import { Record, Static, String } from "runtypes";

export enum SUPPORTED_SIGNING_ALGORITHM {
Secp256k1VerificationKey2018 = "Secp256k1VerificationKey2018",
}
export interface SigningOptions {
signAsString?: boolean;
signer?: ethers.Signer;
signer?: Signer;
}
export const SigningKey = Record({
private: String,
Expand All @@ -15,8 +15,4 @@ export const SigningKey = Record({

export type SigningKey = Static<typeof SigningKey>;

export type SigningFunction = (
message: string,
key: SigningKey | ethers.Signer,
options?: SigningOptions
) => Promise<string>;
export type SigningFunction = (message: string, key: SigningKey | Signer, options?: SigningOptions) => Promise<string>;
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Wallet, utils, ethers } from "ethers";
import { Wallet, Signer, getBytes } from "ethers";
import { SigningFunction, SigningKey, SigningOptions } from "../../../@types/sign";

export const name = "Secp256k1VerificationKey2018";

export const sign: SigningFunction = (
message: string,
keyOrSigner: SigningKey | ethers.Signer,
keyOrSigner: SigningKey | Signer,
options: SigningOptions = {}
): Promise<string> => {
let signer: ethers.Signer;
let signer: Signer;
if (SigningKey.guard(keyOrSigner)) {
const wallet = new Wallet(keyOrSigner.private);
if (!keyOrSigner.public.toLowerCase().includes(wallet.address.toLowerCase())) {
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 : getBytes(message));
};
4 changes: 2 additions & 2 deletions src/shared/signer/signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SigningFunction, SigningOptions, SigningKey } from "../../shared/@types/sign";
import { defaultSigners } from "./signatureSchemes";
import { ethers } from "ethers";
import { Signer } from "ethers";

export const signerBuilder =
(signers: Map<string, SigningFunction>) =>
(alg: string, message: string, keyOrSigner: SigningKey | ethers.Signer, options?: SigningOptions) => {
(alg: string, message: string, keyOrSigner: SigningKey | Signer, options?: SigningOptions) => {
const signer = signers.get(alg);
if (!signer) throw new Error(`${alg} is not supported as a signing algorithm`);
return signer(message, keyOrSigner, options);
Expand Down
3 changes: 1 addition & 2 deletions src/shared/utils/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { logger } from "ethers";
import { SchemaId } from "../@types/document";
import { validateSchema as validate } from "../validate";
import {
Expand All @@ -19,7 +18,7 @@ interface DiagnoseError {
const handleError = (debug: boolean, ...messages: string[]) => {
if (debug) {
for (const message of messages) {
logger.info(message);
console.log(message);
}
}
return messages.map((message) => ({ message }));
Expand Down
Loading