|
|
@ -1,13 +1,14 @@ |
|
|
|
|
|
|
|
import { pbkdf2Async } from "@noble/hashes/pbkdf2"; |
|
|
|
|
|
|
|
import { sha256 } from "@noble/hashes/sha256"; |
|
|
|
import { generateMnemonic } from "@scure/bip39"; |
|
|
|
import { generateMnemonic } from "@scure/bip39"; |
|
|
|
import { wordlist } from "@scure/bip39/wordlists/english"; |
|
|
|
import { wordlist } from "@scure/bip39/wordlists/english"; |
|
|
|
import forge from "node-forge"; |
|
|
|
import forge from "node-forge"; |
|
|
|
import { encode } from "universal-base64url"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function seedFromMnemonic(mnemonic: string) { |
|
|
|
async function seedFromMnemonic(mnemonic: string) { |
|
|
|
const md = forge.md.sha256.create(); |
|
|
|
return pbkdf2Async(sha256, mnemonic, "mnemonic", { |
|
|
|
md.update(mnemonic); |
|
|
|
c: 2048, |
|
|
|
// TODO this is probably not correct
|
|
|
|
dkLen: 32, |
|
|
|
return md.digest().toHex(); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function keysFromMenmonic(mnemonic: string) { |
|
|
|
export async function keysFromMenmonic(mnemonic: string) { |
|
|
@ -28,13 +29,19 @@ export function genMnemonic(): string { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function signCode( |
|
|
|
export async function signCode( |
|
|
|
_code: string, |
|
|
|
code: string, |
|
|
|
_privateKey: forge.pki.ed25519.NativeBuffer |
|
|
|
privateKey: forge.pki.ed25519.NativeBuffer |
|
|
|
): Promise<Uint8Array> { |
|
|
|
): Promise<forge.pki.ed25519.NativeBuffer> { |
|
|
|
// TODO add real signature
|
|
|
|
return forge.pki.ed25519.sign({ |
|
|
|
return new Uint8Array(); |
|
|
|
encoding: "utf8", |
|
|
|
|
|
|
|
message: code, |
|
|
|
|
|
|
|
privateKey, |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function bytesToBase64Url(bytes: Uint8Array): string { |
|
|
|
export function bytesToBase64Url(bytes: Uint8Array): string { |
|
|
|
return encode(String.fromCodePoint(...bytes)); |
|
|
|
return btoa(String.fromCodePoint(...bytes)) |
|
|
|
|
|
|
|
.replace(/\//g, "_") |
|
|
|
|
|
|
|
.replace(/\+/g, "-") |
|
|
|
|
|
|
|
.replace(/=+$/, ""); |
|
|
|
} |
|
|
|
} |
|
|
|