diff --git a/src/backend/accounts/crypto.ts b/src/backend/accounts/crypto.ts index 9fe51380..f3fba9b6 100644 --- a/src/backend/accounts/crypto.ts +++ b/src/backend/accounts/crypto.ts @@ -10,6 +10,14 @@ type Keys = { seed: Uint8Array; }; +function uint8ArrayToBuffer(array: Uint8Array): forge.util.ByteStringBuffer { + return forge.util.createBuffer( + Array.from(array) + .map((byte) => String.fromCharCode(byte)) + .join(""), + ); +} + async function seedFromMnemonic(mnemonic: string) { return pbkdf2Async(sha256, mnemonic, "mnemonic", { c: 2048, @@ -29,8 +37,8 @@ export async function keysFromMnemonic(mnemonic: string): Promise { }); return { - privateKey, - publicKey, + privateKey: new Uint8Array(privateKey), + publicKey: new Uint8Array(publicKey), seed, }; } @@ -43,11 +51,12 @@ export async function signCode( code: string, privateKey: Uint8Array, ): Promise { - return forge.pki.ed25519.sign({ + const signature = forge.pki.ed25519.sign({ encoding: "utf8", message: code, - privateKey, + privateKey: uint8ArrayToBuffer(privateKey), }); + return new Uint8Array(signature); } export function bytesToBase64(bytes: Uint8Array) { @@ -71,7 +80,9 @@ export function base64ToBuffer(data: string) { } export function base64ToStringBuffer(data: string) { - return forge.util.createBuffer(base64ToBuffer(data)); + const decoded = base64ToBuffer(data); + + return uint8ArrayToBuffer(decoded); } export function stringBufferToBase64(buffer: forge.util.ByteStringBuffer) { @@ -91,7 +102,7 @@ export async function encryptData(data: string, secret: Uint8Array) { const cipher = forge.cipher.createCipher( "AES-GCM", - forge.util.createBuffer(secret), + uint8ArrayToBuffer(secret), ); cipher.start({ iv, @@ -115,7 +126,7 @@ export function decryptData(data: string, secret: Uint8Array) { const decipher = forge.cipher.createDecipher( "AES-GCM", - forge.util.createBuffer(secret), + uint8ArrayToBuffer(secret), ); decipher.start({ iv: base64ToStringBuffer(iv),