From 0ee35e49465b404f6f35aa70d4c75ccf09167acf Mon Sep 17 00:00:00 2001 From: Matthew Aaron Raymer Date: Tue, 3 Jan 2023 12:35:41 +0800 Subject: [PATCH] Added variation of accessToken method carried over from endorser-mobile --- src/libs/crypto/index.ts | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/libs/crypto/index.ts b/src/libs/crypto/index.ts index 8068260..d386ef4 100644 --- a/src/libs/crypto/index.ts +++ b/src/libs/crypto/index.ts @@ -4,6 +4,8 @@ import { getRandomBytesSync } from "ethereum-cryptography/random"; import { entropyToMnemonic } from "ethereum-cryptography/bip39"; import { wordlist } from "ethereum-cryptography/bip39/wordlists/english"; import { HDNode } from "@ethersproject/hdnode"; +import * as didJwt from 'did-jwt'; +import * as u8a from 'uint8arrays' /** * @@ -37,7 +39,14 @@ export const newIdentifier = ( }; }; -export const deriveAddress = ( + +/** + * + * + * @param {string} mnemonic + * @return {*} {[string, string, string, string]} + */ +const deriveAddress = ( mnemonic: string ): [string, string, string, string] => { const UPORT_ROOT_DERIVATION_PATH = "m/7696500'/0'/0'/0'"; @@ -52,6 +61,7 @@ export const deriveAddress = ( return [address, privateHex, publicHex, UPORT_ROOT_DERIVATION_PATH]; }; + /** * * @@ -63,3 +73,27 @@ export const createIdentifier = (): string => { return mnemonic; }; + + +/** + * Retreive an access token + * + * @param {IIdentifier} identifier + * @return {*} + */ +const accessToken = async (identifier: IIdentifier) => { + const did: string = identifier.did; + const privateKeyHex: string = identifier.keys[0].privateKeyHex as string; + const input = privateKeyHex.startsWith('0x') ? privateKeyHex.substring(2) : privateKeyHex; + const privateKeyBytes = u8a.fromString(input.toLowerCase(), 'base16') + + const signer = didJwt.ES256KSigner(privateKeyBytes, true); + + const nowEpoch = Math.floor(Date.now() / 1000); + const endEpoch = nowEpoch + 60; // add one minute + + const uportTokenPayload = { exp: endEpoch, iat: nowEpoch, iss: did }; + const alg = undefined; // defaults to 'ES256K', more standardized but harder to verify vs ES256K-R + const jwt: string = await didJwt.createJWT(uportTokenPayload, { alg, issuer: did, signer }); + return jwt; +}; \ No newline at end of file