Browse Source

Added variation of accessToken method carried over from endorser-mobile

kb/add-usage-guide
Matthew Aaron Raymer 2 years ago
parent
commit
0ee35e4946
  1. 36
      src/libs/crypto/index.ts

36
src/libs/crypto/index.ts

@ -4,6 +4,8 @@ import { getRandomBytesSync } from "ethereum-cryptography/random";
import { entropyToMnemonic } from "ethereum-cryptography/bip39"; import { entropyToMnemonic } from "ethereum-cryptography/bip39";
import { wordlist } from "ethereum-cryptography/bip39/wordlists/english"; import { wordlist } from "ethereum-cryptography/bip39/wordlists/english";
import { HDNode } from "@ethersproject/hdnode"; 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 mnemonic: string
): [string, string, string, string] => { ): [string, string, string, string] => {
const UPORT_ROOT_DERIVATION_PATH = "m/7696500'/0'/0'/0'"; 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]; return [address, privateHex, publicHex, UPORT_ROOT_DERIVATION_PATH];
}; };
/** /**
* *
* *
@ -63,3 +73,27 @@ export const createIdentifier = (): string => {
return mnemonic; 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;
};
Loading…
Cancel
Save