Skip to content

Commit

Permalink
refactor: split utils
Browse files Browse the repository at this point in the history
  • Loading branch information
javadkh2 committed Nov 10, 2023
1 parent 0761496 commit 0346834
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/libs/hd-wallet/src/bip44/utils/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IUnsignedCommand } from '@kadena/client';
import { sign } from '@kadena/cryptography-utils';

import { HDKey } from 'ed25519-keygen/hdkey';
import { uint8ArrayToHex } from '../../utils/utils';
import { uint8ArrayToHex } from '../../utils/buffer-helpers';

/**
* Derive a key pair using a seed and an index.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HARDENED_OFFSET, harden } from '../../utils/utils';
import { HARDENED_OFFSET, harden } from '../../utils/crypto';
import { kadenaGenKeypair as kadenaGenKeypairOriginal } from '../vendor/kadena-crypto';

function kadenaGenOneKeypair(
Expand Down
29 changes: 29 additions & 0 deletions packages/libs/hd-wallet/src/utils/buffer-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Convert a Buffer to a Base64 encoded string.
* @param {Buffer} buffer - Buffer to convert.
* @returns {string} - Returns the Base64 encoded string.
*/
export function bufferToBase64(buffer: Buffer): string {
return buffer.toString('base64');
}

/**
* Convert a Base64 encoded string to a Buffer.
* @param {string} base64 - Base64 encoded string to convert.
* @returns {Buffer} - Returns the resulting Buffer.
*/
export function base64ToBuffer(base64: string): Buffer {
return Buffer.from(base64, 'base64');
}

/**
* Convert a Uint8Array to a hexadecimal string.
* @param {Uint8Array} uint8Array - The array to convert.
* @returns {string} - Returns the hexadecimal representation of the input.
*/
export const uint8ArrayToHex = (uint8Array: Uint8Array): string => {
if (uint8Array.length === 33 && uint8Array.at(0) === 0) {
uint8Array = uint8Array.slice(1);
}
return [...uint8Array].map((x) => x.toString(16).padStart(2, '0')).join('');
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,5 @@ export function decrypt(
}
}

/**
* Convert a Buffer to a Base64 encoded string.
* @param {Buffer} buffer - Buffer to convert.
* @returns {string} - Returns the Base64 encoded string.
*/
export function bufferToBase64(buffer: Buffer): string {
return buffer.toString('base64');
}

/**
* Convert a Base64 encoded string to a Buffer.
* @param {string} base64 - Base64 encoded string to convert.
* @returns {Buffer} - Returns the resulting Buffer.
*/
export function base64ToBuffer(base64: string): Buffer {
return Buffer.from(base64, 'base64');
}

export const HARDENED_OFFSET = 0x80000000;
export const harden = (n: number) => HARDENED_OFFSET + n;

/**
* Convert a Uint8Array to a hexadecimal string.
* @param {Uint8Array} uint8Array - The array to convert.
* @returns {string} - Returns the hexadecimal representation of the input.
*/
export const uint8ArrayToHex = (uint8Array: Uint8Array): string => {
if (uint8Array.length === 33 && uint8Array.at(0) === 0) {
uint8Array = uint8Array.slice(1);
}
return [...uint8Array].map((x) => x.toString(16).padStart(2, '0')).join('');
};
2 changes: 1 addition & 1 deletion packages/libs/hd-wallet/src/utils/kadenaEncryption.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomBytes } from 'crypto';
import { decrypt, encrypt } from './utils';
import { decrypt, encrypt } from './crypto';

/**
* Encrypts the message with a password .
Expand Down

0 comments on commit 0346834

Please sign in to comment.