Skip to content

Commit 0da49d3

Browse files
authored
fix: remove node.js Buffer for better browser compatibility (#656)
1 parent fa97383 commit 0da49d3

File tree

6 files changed

+22
-43
lines changed

6 files changed

+22
-43
lines changed

.changeset/wet-phones-search.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@fuel-ts/hasher": patch
3+
"@fuel-ts/keystore": patch
4+
---
5+
6+
Remove node.js Buffer for browser compatibility

packages/hasher/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@fuel-ts/math": "workspace:*",
3131
"@fuel-ts/providers": "workspace:*",
3232
"@fuel-ts/transactions": "workspace:*",
33+
"@fuel-ts/keystore": "workspace:*",
3334
"lodash.clonedeep": "^4.5.0"
3435
},
3536
"devDependencies": {

packages/hasher/src/hasher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { BytesLike } from '@ethersproject/bytes';
22
import { sha256 } from '@ethersproject/sha2';
33
import { ZeroBytes32 } from '@fuel-ts/constants';
4+
import { bufferFromString } from '@fuel-ts/keystore';
45
import { bn } from '@fuel-ts/math';
56
import type { TransactionRequestLike } from '@fuel-ts/providers';
67
import { transactionRequestify, TransactionType } from '@fuel-ts/providers';
@@ -15,7 +16,7 @@ import cloneDeep from 'lodash.clonedeep';
1516
* @returns A sha256 hash of the message
1617
*/
1718
export function hashMessage(msg: string) {
18-
return sha256(Buffer.from(msg));
19+
return sha256(bufferFromString(msg, 'utf-8'));
1920
}
2021

2122
/**

packages/keystore/src/aes-ctr-web.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export async function encrypt<T>(password: string, data: T): Promise<Keystore> {
1515
const iv = randomBytes(16);
1616
const salt = randomBytes(32);
1717
const secret = keyFromPassword(password, salt);
18-
const dataBuffer = Uint8Array.from(Buffer.from(JSON.stringify(data), 'utf-8'));
18+
const dataString = JSON.stringify(data);
19+
const dataBuffer = bufferFromString(dataString, 'utf-8');
1920
const alg = {
2021
name: ALGORITHM,
2122
counter: iv,

packages/keystore/src/keystore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { encrypt as encWeb, decrypt as decWeb } from './aes-ctr-web';
44
import { strategy } from './universal-crypto';
55

66
export type { Keystore } from './aes-ctr';
7-
export { keyFromPassword } from './aes-ctr';
7+
export { keyFromPassword, bufferFromString, stringFromBuffer } from './aes-ctr';
88
export { randomBytes } from './randomBytes';
99

1010
/**

pnpm-lock.yaml

Lines changed: 10 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)