-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/jsonld-credentials' of https://github.com/animo/ar…
…ies-framework-javascript into animo-feat/jsonld-credentials Signed-off-by: Karim <karim@animo.id>
- Loading branch information
Showing
101 changed files
with
10,525 additions
and
246 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
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) | ||
await MessageValidator.validate(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.