forked from Open-Attestation/oa-verify
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide custom resolver option for did
closes Open-Attestation#162
- Loading branch information
1 parent
da39a6d
commit ab3b082
Showing
8 changed files
with
152 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { ethers } from "ethers"; | ||
import { v3 } from "@govtechsg/open-attestation"; | ||
import { openAttestationDidIdentityProof } from "../verifiers/issuerIdentity/did/didIdentityProof"; | ||
import { verificationBuilder } from "../verifiers/verificationBuilder"; | ||
import { INFURA_API_KEY } from "../config"; | ||
import { createResolver, EthrResolverConfig, resolve } from "./resolver"; | ||
|
||
const didDoc = { | ||
"@context": "https://w3id.org/did/v1", | ||
id: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
publicKey: [ | ||
{ | ||
id: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457#controller", | ||
type: "Secp256k1VerificationKey2018", | ||
controller: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
ethereumAddress: "0x0ce1854a3836daf9130028cf90d6d35b1ae46457", | ||
}, | ||
], | ||
authentication: [ | ||
{ | ||
type: "Secp256k1SignatureAuthentication2018", | ||
publicKey: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457#controller", | ||
}, | ||
], | ||
}; | ||
|
||
// this document has been created using: | ||
// issuer address: 0x0ce1854a3836daf9130028cf90d6d35b1ae46457; | ||
// issuer private key: 0xeff5bd53a7df36912370a14c25c79f102736377883528414a2f82cdd1610c938; | ||
// issuer public key: 0x04a515ac97b41c22021a9655b24e49d590f984c9c1f9f4e60baee7845d06e40631452eb79964e65ad0e07eac715154139d6b5314eec4793d3d45f1320b7609462f; | ||
|
||
const v3DidSigned = { | ||
version: "https://schema.openattestation.com/3.0/schema.json", | ||
"@context": [ | ||
"https://www.w3.org/2018/credentials/v1", | ||
"https://schema.org", | ||
"https://schemata.openattestation.com/com/openattestation/1.0/OpenAttestation.v3.json", | ||
], | ||
issuanceDate: "2010-01-01T19:23:24Z", | ||
issuer: { | ||
id: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
name: "anyway", | ||
}, | ||
type: ["VerifiableCredential", "Name"], | ||
credentialSubject: { | ||
id: "did:example:1234", | ||
name: "John", | ||
}, | ||
openAttestationMetadata: { | ||
proof: { | ||
type: "OpenAttestationProofMethod", | ||
method: "DID", | ||
value: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
revocation: { | ||
type: "NONE", | ||
}, | ||
}, | ||
identityProof: { | ||
type: "DID", | ||
identifier: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
}, | ||
}, | ||
proof: { | ||
type: "OpenAttestationMerkleProofSignature2018", | ||
proofPurpose: "assertionMethod", | ||
targetHash: "a1c29778310a0c6d6a8f614bb689e10771a78b4d94f0c9289e5a448804f46c24", | ||
proofs: [], | ||
merkleRoot: "a1c29778310a0c6d6a8f614bb689e10771a78b4d94f0c9289e5a448804f46c24", | ||
salts: | ||
"W3sidmFsdWUiOiJjYWZjZGMyNDEwNmQ3OTcxODhkOTNlN2Y3YTQzMmNlMGY3ZjRjNjljZTRkNTZkODMxMjJhNTllNzAwZGEzYzY4IiwicGF0aCI6InZlcnNpb24ifSx7InZhbHVlIjoiYmE2NzNhNjU5M2U5MGVmZDQxYjFmODQ1ZmE2N2VmODQ1ODA5YjdiY2Q3MTk3NDkxNDRkZWQxNmRmZGFmNGEzZSIsInBhdGgiOiJAY29udGV4dFswXSJ9LHsidmFsdWUiOiIyMjY3ZDAyMGZjZjQzYjcxZG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Njc1MmUwNTUxYTc4ZjUyYWMzZDQ1MzFkODEiLCJwYXRoIjoib3BlbkF0dGVzdGF0aW9uTWV0YWRhdGEuaWRlbnRpdHlQcm9vZi5pZGVudGlmaWVyIn1d", | ||
privacy: { | ||
obfuscated: [], | ||
}, | ||
key: "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457#controller", | ||
signature: | ||
"0x03767d1f4f66c332c63ad2ed38a64334614759f1dc69c295a3bda1299b90a4ff00200761683dbb24e41d6bf2f237c9985dbd6eb21afad4c62831ba08e9d177fb1b", | ||
}, | ||
} as v3.SignedWrappedDocument; | ||
|
||
const customConfig: EthrResolverConfig = { | ||
networks: [{ name: "ropsten", rpcUrl: `https://ropsten.infura.io/v3/${INFURA_API_KEY}` }], | ||
}; | ||
|
||
describe("custom resolver", () => { | ||
it("should resolve did using resolver", async () => { | ||
const did = await resolve( | ||
"did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
createResolver({ ethrResolverConfig: customConfig }) | ||
); | ||
expect(did).toEqual(didDoc); | ||
}); | ||
|
||
it("should resolve did using verifier", async () => { | ||
const verify = verificationBuilder([openAttestationDidIdentityProof], { | ||
provider: new ethers.providers.JsonRpcProvider({ url: customConfig.networks[0].rpcUrl }), | ||
resolver: createResolver({ ethrResolverConfig: customConfig }), | ||
}); | ||
const fragment = await verify(v3DidSigned); | ||
expect(fragment[0]).toMatchInlineSnapshot(` | ||
Object { | ||
"data": Object { | ||
"did": "did:ethr:ropsten:0x0cE1854a3836daF9130028Cf90D6d35B1Ae46457", | ||
"verified": true, | ||
}, | ||
"name": "OpenAttestationDidIdentityProof", | ||
"status": "VALID", | ||
"type": "ISSUER_IDENTITY", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
import { Resolver, DIDDocument } from "did-resolver"; | ||
import { Resolver, DIDDocument, PublicKey } from "did-resolver"; | ||
import { getResolver as ethrGetResolver } from "ethr-did-resolver"; | ||
import { getResolver as webGetResolver } from "web-did-resolver"; | ||
import NodeCache from "node-cache"; | ||
import { INFURA_API_KEY } from "../config"; | ||
|
||
export interface EthrResolverConfig { | ||
networks: Array<{ | ||
name: string; | ||
registry?: string; | ||
rpcUrl: string; | ||
}>; | ||
} | ||
|
||
const providerConfig = { | ||
networks: [{ name: "mainnet", rpcUrl: `https://mainnet.infura.io/v3/${INFURA_API_KEY}` }], | ||
}; | ||
|
||
const didResolutionCache = new NodeCache({ stdTTL: 5 * 60 }); // 5 min | ||
|
||
export const resolver = new Resolver({ | ||
...ethrGetResolver(providerConfig), | ||
...webGetResolver(), | ||
}); | ||
const defaultResolver = new Resolver({ ...ethrGetResolver(providerConfig), ...webGetResolver() }); | ||
|
||
export const createResolver = ({ ethrResolverConfig }: { ethrResolverConfig?: EthrResolverConfig }): Resolver => { | ||
return ethrResolverConfig | ||
? new Resolver({ ...ethrGetResolver(ethrResolverConfig), ...webGetResolver() }) | ||
: defaultResolver; | ||
}; | ||
|
||
export const resolve = async (didUrl: string): Promise<DIDDocument> => { | ||
export const resolve = async (didUrl: string, resolver?: Resolver): Promise<DIDDocument> => { | ||
const cachedResult = didResolutionCache.get<DIDDocument>(didUrl); | ||
if (cachedResult) return cachedResult; | ||
const did = await resolver.resolve(didUrl); | ||
const did = resolver ? await resolver.resolve(didUrl) : await defaultResolver.resolve(didUrl); | ||
didResolutionCache.set(didUrl, did); | ||
return did; | ||
}; | ||
|
||
export const getPublicKey = async (did: string, key: string) => { | ||
const { publicKey } = await resolve(did); | ||
export const getPublicKey = async (did: string, key: string, resolver?: Resolver): Promise<PublicKey | undefined> => { | ||
const { publicKey } = await resolve(did, resolver); | ||
return publicKey.find((k) => k.id.toLowerCase() === key.toLowerCase()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters