|
|
@ -5,44 +5,8 @@ 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 { Signer } from "did-jwt"; |
|
|
|
import * as u8a from "uint8arrays"; |
|
|
|
|
|
|
|
export function hexToBytes(s: string): Uint8Array { |
|
|
|
const input = s.startsWith("0x") ? s.substring(2) : s; |
|
|
|
return u8a.fromString(input.toLowerCase(), "base16"); |
|
|
|
} |
|
|
|
|
|
|
|
export function fromJose(signature: string): { |
|
|
|
r: string; |
|
|
|
s: string; |
|
|
|
recoveryParam?: number; |
|
|
|
} { |
|
|
|
const signatureBytes: Uint8Array = base64ToBytes(signature); |
|
|
|
if (signatureBytes.length < 64 || signatureBytes.length > 65) { |
|
|
|
throw new TypeError( |
|
|
|
`Wrong size for signature. Expected 64 or 65 bytes, but got ${signatureBytes.length}` |
|
|
|
); |
|
|
|
} |
|
|
|
const r = bytesToHex(signatureBytes.slice(0, 32)); |
|
|
|
const s = bytesToHex(signatureBytes.slice(32, 64)); |
|
|
|
const recoveryParam = |
|
|
|
signatureBytes.length === 65 ? signatureBytes[64] : undefined; |
|
|
|
return { r, s, recoveryParam }; |
|
|
|
} |
|
|
|
|
|
|
|
export function bytesToHex(b: Uint8Array): string { |
|
|
|
return u8a.toString(b, "base16"); |
|
|
|
} |
|
|
|
|
|
|
|
export function base64ToBytes(s: string): Uint8Array { |
|
|
|
const inputBase64Url = s |
|
|
|
.replace(/\+/g, "-") |
|
|
|
.replace(/\//g, "_") |
|
|
|
.replace(/=/g, ""); |
|
|
|
return u8a.fromString(inputBase64Url, "base64url"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
@ -117,12 +81,8 @@ export const createIdentifier = (): string => { |
|
|
|
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 signer = didJwt.SimpleSigner(privateKeyHex); |
|
|
|
const signer = SimpleSigner(privateKeyHex); |
|
|
|
|
|
|
|
const nowEpoch = Math.floor(Date.now() / 1000); |
|
|
|
const endEpoch = nowEpoch + 60; // add one minute
|
|
|
@ -138,17 +98,14 @@ export const accessToken = async (identifier: IIdentifier) => { |
|
|
|
}; |
|
|
|
|
|
|
|
export const sign = async (privateKeyHex: string) => { |
|
|
|
//const input = privateKeyHex.startsWith("0x")
|
|
|
|
// ? privateKeyHex.substring(2)
|
|
|
|
// : privateKeyHex;
|
|
|
|
// const privateKeyBytes = u8a.fromString(input.toLowerCase(), "base16");
|
|
|
|
|
|
|
|
const signer = didJwt.SimpleSigner(privateKeyHex); |
|
|
|
const signer = SimpleSigner(privateKeyHex); |
|
|
|
|
|
|
|
return signer; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Copied out of did-jwt since it's deprecated in that library. |
|
|
|
* |
|
|
|
* The SimpleSigner returns a configured function for signing data. |
|
|
|
* |
|
|
|
* @example |
|
|
@ -160,10 +117,34 @@ export const sign = async (privateKeyHex: string) => { |
|
|
|
* @param {String} hexPrivateKey a hex encoded private key |
|
|
|
* @return {Function} a configured signer function |
|
|
|
*/ |
|
|
|
export const SimpleSigner = async (hexPrivateKey: string): Promise<Signer> => { |
|
|
|
const signer = didJwt.ES256KSigner(hexToBytes(hexPrivateKey), true); |
|
|
|
export function SimpleSigner(hexPrivateKey: string): didJwt.Signer { |
|
|
|
const signer = didJwt.ES256KSigner(didJwt.hexToBytes(hexPrivateKey), true); |
|
|
|
return async (data) => { |
|
|
|
const signature = (await signer(data)) as string; |
|
|
|
return fromJose(signature); |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// from did-jwt/util; see SimpleSigner above
|
|
|
|
export function fromJose(signature: string): { |
|
|
|
r: string; |
|
|
|
s: string; |
|
|
|
recoveryParam?: number; |
|
|
|
} { |
|
|
|
const signatureBytes: Uint8Array = didJwt.base64ToBytes(signature); |
|
|
|
if (signatureBytes.length < 64 || signatureBytes.length > 65) { |
|
|
|
throw new TypeError( |
|
|
|
`Wrong size for signature. Expected 64 or 65 bytes, but got ${signatureBytes.length}` |
|
|
|
); |
|
|
|
} |
|
|
|
const r = bytesToHex(signatureBytes.slice(0, 32)); |
|
|
|
const s = bytesToHex(signatureBytes.slice(32, 64)); |
|
|
|
const recoveryParam = |
|
|
|
signatureBytes.length === 65 ? signatureBytes[64] : undefined; |
|
|
|
return { r, s, recoveryParam }; |
|
|
|
} |
|
|
|
|
|
|
|
// from did-jwt/util; see SimpleSigner above
|
|
|
|
export function bytesToHex(b: Uint8Array): string { |
|
|
|
return u8a.toString(b, "base16"); |
|
|
|
} |
|
|
|