-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jsonld-credential support (#718)
Signed-off-by: Karim Stekelenburg <[email protected]>
- Loading branch information
1 parent
5f91738
commit ea34c47
Showing
101 changed files
with
10,508 additions
and
242 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { VerificationMethod } from '../modules/dids' | ||
|
||
export interface LdKeyPairOptions { | ||
id: string | ||
controller: string | ||
} | ||
|
||
export abstract class LdKeyPair { | ||
public readonly id: string | ||
public readonly controller: string | ||
public abstract type: string | ||
|
||
public constructor(options: LdKeyPairOptions) { | ||
this.id = options.id | ||
this.controller = options.controller | ||
} | ||
|
||
public static async generate(): Promise<LdKeyPair> { | ||
throw new Error('Not implemented') | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
public static async from(verificationMethod: VerificationMethod): Promise<LdKeyPair> { | ||
throw new Error('Abstract method from() must be implemented in subclass.') | ||
} | ||
|
||
public export(publicKey = false, privateKey = false) { | ||
if (!publicKey && !privateKey) { | ||
throw new Error('Export requires specifying either "publicKey" or "privateKey".') | ||
} | ||
const key = { | ||
id: this.id, | ||
type: this.type, | ||
controller: this.controller, | ||
} | ||
|
||
return key | ||
} | ||
|
||
public abstract fingerprint(): string | ||
|
||
public abstract verifyFingerprint(fingerprint: string): boolean | ||
|
||
public abstract signer(): { | ||
sign: (data: { data: Uint8Array | Uint8Array[] }) => Promise<Uint8Array | Array<Uint8Array>> | ||
} | ||
|
||
public abstract verifier(): { | ||
verify: (data: { data: Uint8Array | Uint8Array[]; signature: Uint8Array }) => Promise<boolean> | ||
} | ||
} |
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,123 @@ | ||
import type { Wallet } from '..' | ||
import type { Key } from './Key' | ||
import type { LdKeyPairOptions } from './LdKeyPair' | ||
|
||
import { VerificationMethod } from '../modules/dids' | ||
import { getKeyDidMappingByVerificationMethod } from '../modules/dids/domain/key-type/keyDidMapping' | ||
import { JsonTransformer } from '../utils' | ||
import { MessageValidator } from '../utils/MessageValidator' | ||
import { Buffer } from '../utils/buffer' | ||
|
||
import { LdKeyPair } from './LdKeyPair' | ||
|
||
interface WalletKeyPairOptions extends LdKeyPairOptions { | ||
wallet: Wallet | ||
key: Key | ||
} | ||
|
||
export function createWalletKeyPairClass(wallet: Wallet) { | ||
return class WalletKeyPair extends LdKeyPair { | ||
public wallet: Wallet | ||
public key: Key | ||
public type: string | ||
|
||
public constructor(options: WalletKeyPairOptions) { | ||
super(options) | ||
this.wallet = options.wallet | ||
this.key = options.key | ||
this.type = options.key.keyType | ||
} | ||
|
||
public static async generate(): Promise<WalletKeyPair> { | ||
throw new Error('Not implemented') | ||
} | ||
|
||
public fingerprint(): string { | ||
throw new Error('Method not implemented.') | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
public verifyFingerprint(fingerprint: string): boolean { | ||
throw new Error('Method not implemented.') | ||
} | ||
|
||
public static async from(verificationMethod: VerificationMethod): Promise<WalletKeyPair> { | ||
const vMethod = JsonTransformer.fromJSON(verificationMethod, VerificationMethod) | ||
MessageValidator.validateSync(vMethod) | ||
const { getKeyFromVerificationMethod } = getKeyDidMappingByVerificationMethod(vMethod) | ||
const key = getKeyFromVerificationMethod(vMethod) | ||
|
||
return new WalletKeyPair({ | ||
id: vMethod.id, | ||
controller: vMethod.controller, | ||
wallet: wallet, | ||
key: key, | ||
}) | ||
} | ||
|
||
/** | ||
* This method returns a wrapped wallet.sign method. The method is being wrapped so we can covert between Uint8Array and Buffer. This is to make it compatible with the external signature libraries. | ||
*/ | ||
public signer(): { sign: (data: { data: Uint8Array | Uint8Array[] }) => Promise<Uint8Array> } { | ||
// wrap function for conversion | ||
const wrappedSign = async (data: { data: Uint8Array | Uint8Array[] }): Promise<Uint8Array> => { | ||
let converted: Buffer | Buffer[] = [] | ||
|
||
// convert uint8array to buffer | ||
if (Array.isArray(data.data)) { | ||
converted = data.data.map((d) => Buffer.from(d)) | ||
} else { | ||
converted = Buffer.from(data.data) | ||
} | ||
|
||
// sign | ||
const result = await wallet.sign({ | ||
data: converted, | ||
key: this.key, | ||
}) | ||
|
||
// convert result buffer to uint8array | ||
return Uint8Array.from(result) | ||
} | ||
|
||
return { | ||
sign: wrappedSign.bind(this), | ||
} | ||
} | ||
|
||
/** | ||
* This method returns a wrapped wallet.verify method. The method is being wrapped so we can covert between Uint8Array and Buffer. This is to make it compatible with the external signature libraries. | ||
*/ | ||
public verifier(): { | ||
verify: (data: { data: Uint8Array | Uint8Array[]; signature: Uint8Array }) => Promise<boolean> | ||
} { | ||
const wrappedVerify = async (data: { | ||
data: Uint8Array | Uint8Array[] | ||
signature: Uint8Array | ||
}): Promise<boolean> => { | ||
let converted: Buffer | Buffer[] = [] | ||
|
||
// convert uint8array to buffer | ||
if (Array.isArray(data.data)) { | ||
converted = data.data.map((d) => Buffer.from(d)) | ||
} else { | ||
converted = Buffer.from(data.data) | ||
} | ||
|
||
// verify | ||
return wallet.verify({ | ||
data: converted, | ||
signature: Buffer.from(data.signature), | ||
key: this.key, | ||
}) | ||
} | ||
return { | ||
verify: wrappedVerify.bind(this), | ||
} | ||
} | ||
|
||
public get publicKeyBuffer(): Uint8Array { | ||
return new Uint8Array(this.key.publicKey) | ||
} | ||
} | ||
} |
Oops, something went wrong.