Skip to content

Commit

Permalink
Merge branch 'master' into chore/update-beta-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
HJunyuan committed Jul 24, 2024
2 parents 0dbdadd + 239bdba commit 7736db3
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 29 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@
},
"dependencies": {
"@aws-crypto/sha256-universal": "^5.2.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/logger": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@govtechsg/jsonld": "^0.1.0",
"buffer": "^6.0.3",
"ajv-formats": "^2.1.1",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"debug": "^4.3.2",
"ethers": "^5.7.2",
"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
@@ -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 { Wallet } from "@ethersproject/wallet";

const wrappedDocumentV2 = rawWrappedDocumentV2 as v2.WrappedDocument;

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 "@ethersproject/abstract-signer";

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
@@ -1,7 +1,7 @@
import { signDocument, v3 } from "../../index";
import { SUPPORTED_SIGNING_ALGORITHM } from "../../shared/@types/sign";
import rawWrappedDocumentV3 from "../../../test/fixtures/v3/did-wrapped.json";
import { Wallet } from "ethers";
import { Wallet } from "@ethersproject/wallet";

const wrappedDocumentV3 = rawWrappedDocumentV3 as v3.WrappedDocument;

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,15 +7,15 @@ 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 "@ethersproject/abstract-signer";

/**
* @deprecated will be removed in the next major release in favour of OpenAttestation v4.0 (more info: https://github.com/Open-Attestation/open-attestation/tree/alpha)
*/
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
2 changes: 1 addition & 1 deletion src/3.0/validate/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const documentLoader = async (url: string) => {
for (const url of preloadedContextList) {
contexts.set(
url,
fetch(url, { headers: { accept: "application/json" } }).then((res: any) => res.json())
fetch(url, { headers: { accept: "application/json, application/ld+json" } }).then((res: any) => res.json())
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers, Signer } from "ethers";
import { Signer } from "@ethersproject/abstract-signer";

import { getSchema } from "./shared/ajv";
import * as utils from "./shared/utils";
Expand Down Expand Up @@ -110,7 +110,7 @@ export function obfuscate<T extends WrappedDocument<OpenAttestationDocument>>(
export async function signDocument<T extends v2.OpenAttestationDocument | v3.OpenAttestationDocument>(
document: WrappedDocument<T> | SignedWrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
keyOrSigner: SigningKey | Signer
): Promise<SignedWrappedDocument<T>> {
// rj was worried it could happen deep in the code, so I moved it to the boundaries
if (!SigningKey.guard(keyOrSigner) && !Signer.isSigner(keyOrSigner)) {
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,8 +10,8 @@ import {
WrappedDocument as WrappedDocumentV3,
} from "../../3.0/types";
import { Literal, Static, String } from "runtypes";
import { ethers } from "ethers";
import { V4OpenAttestationDocument, V4SignedWrappedDocument, V4WrappedDocument } from "../../4.0/types";
import { isHexString } from "@ethersproject/bytes";

export type OpenAttestationDocument = OpenAttestationDocumentV2 | OpenAttestationDocumentV3 | V4OpenAttestationDocument;
export type WrappedDocument<T extends OpenAttestationDocument> = T extends OpenAttestationDocumentV2
Expand All @@ -35,7 +35,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 "@ethersproject/abstract-signer";
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,16 @@
import { Wallet, utils, ethers } from "ethers";
import { SigningFunction, SigningKey, SigningOptions } from "../../../@types/sign";
import { Signer } from "@ethersproject/abstract-signer";
import { arrayify } from "@ethersproject/bytes";
import { Wallet } from "@ethersproject/wallet";

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 +20,6 @@ export const sign: SigningFunction = (
} else {
signer = keyOrSigner;
}
return signer.signMessage(options.signAsString ? message : utils.arrayify(message));
// see https://docs.ethers.org/v6/migrating/ under hex-conversion
return signer.signMessage(options.signAsString ? message : arrayify(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 { Signer } from "@ethersproject/abstract-signer";
import { SigningFunction, SigningOptions, SigningKey } from "../../shared/@types/sign";
import { defaultSigners } from "./signatureSchemes";
import { ethers } 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
7 changes: 5 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 @@ -13,14 +13,17 @@ import { v4Diagnose } from "../../4.0/diagnose";

export type Version = "2.0" | "3.0" | "4.0";

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

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 7736db3

Please sign in to comment.