Work in progress

This commit is contained in:
Matthew Aaron Raymer
2023-01-03 17:28:23 +08:00
parent 0ee35e4946
commit 6325bcbe35
7 changed files with 102 additions and 25 deletions

View File

@@ -4,8 +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'
import * as didJwt from "did-jwt";
import * as u8a from "uint8arrays";
/**
*
@@ -39,14 +39,13 @@ export const newIdentifier = (
};
};
/**
*
*
* @param {string} mnemonic
* @return {*} {[string, string, string, string]}
*/
const deriveAddress = (
export const deriveAddress = (
mnemonic: string
): [string, string, string, string] => {
const UPORT_ROOT_DERIVATION_PATH = "m/7696500'/0'/0'/0'";
@@ -61,7 +60,6 @@ const deriveAddress = (
return [address, privateHex, publicHex, UPORT_ROOT_DERIVATION_PATH];
};
/**
*
*
@@ -74,18 +72,19 @@ export const createIdentifier = (): string => {
return mnemonic;
};
/**
* Retreive an access token
*
* @param {IIdentifier} identifier
* @return {*}
* @return {*}
*/
const accessToken = async (identifier: IIdentifier) => {
export 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 input = privateKeyHex.startsWith("0x")
? privateKeyHex.substring(2)
: privateKeyHex;
const privateKeyBytes = u8a.fromString(input.toLowerCase(), "base16");
const signer = didJwt.ES256KSigner(privateKeyBytes, true);
@@ -94,6 +93,10 @@ const accessToken = async (identifier: IIdentifier) => {
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 });
const jwt: string = await didJwt.createJWT(uportTokenPayload, {
alg,
issuer: did,
signer,
});
return jwt;
};
};