Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap out Buffer with browser-compatible Uint8Array #83

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ module.exports = {
allow: ['^UNSAFE_', 'coin_type', 'address_index'],
},
],

'no-restricted-globals': [
'error',
{
name: 'Buffer',
message: "Use 'Uint8Array' instead.",
},
],
},

overrides: [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
]
},
"dependencies": {
"@metamask/utils": "^3.2.0",
"@noble/ed25519": "^1.6.0",
"@noble/hashes": "^1.0.0",
"@noble/secp256k1": "^1.5.5",
Expand Down
31 changes: 16 additions & 15 deletions src/BIP44CoinTypeNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bytesToHex } from '@metamask/utils';
import fixtures from '../test/fixtures';
import { encodeExtendedKey, PRIVATE_KEY_VERSION } from './extended-keys';
import {
Expand Down Expand Up @@ -82,26 +83,26 @@ describe('BIP44CoinTypeNode', () => {
const inputs = [
{
privateKey: '0xf00',
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
...options,
},
{
privateKey: Buffer.allocUnsafe(64).fill(1).toString('hex'),
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
privateKey: bytesToHex(new Uint8Array(64).fill(1)),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
...options,
},
{
privateKey: Buffer.allocUnsafe(63).fill(1).toString('hex'),
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
privateKey: bytesToHex(new Uint8Array(63).fill(1)),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
...options,
},
{
privateKey: Buffer.alloc(64).toString('hex'),
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
privateKey: bytesToHex(new Uint8Array(64)),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
...options,
},
];
Expand All @@ -116,13 +117,13 @@ describe('BIP44CoinTypeNode', () => {
BIP44CoinTypeNode.fromJSON(
{
privateKey: 1,
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
depth: 2,
} as any,
arbitraryCoinType,
),
).rejects.toThrow('Invalid hex string: "1".');
).rejects.toThrow('Value must be a hexadecimal string.');
});

it('throws if coin type is invalid', async () => {
Expand Down Expand Up @@ -430,7 +431,7 @@ describe('BIP44CoinTypeNode', () => {

const extendedKey = encodeExtendedKey({
version: PRIVATE_KEY_VERSION,
privateKey: node.privateKeyBuffer as Buffer,
privateKey: node.privateKeyBuffer as Uint8Array,
chainCode: node.chainCodeBuffer,
depth: node.depth,
parentFingerprint: node.parentFingerprint,
Expand Down
8 changes: 4 additions & 4 deletions src/BIP44CoinTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ export class BIP44CoinTypeNode implements BIP44CoinTypeNodeInterface {
return this.#node.depth;
}

public get privateKeyBuffer(): Buffer | undefined {
public get privateKeyBuffer(): Uint8Array | undefined {
return this.#node.privateKeyBuffer;
}

public get publicKeyBuffer(): Buffer {
public get publicKeyBuffer(): Uint8Array {
return this.#node.publicKeyBuffer;
}

public get chainCodeBuffer(): Buffer {
public get chainCodeBuffer(): Uint8Array {
return this.#node.chainCodeBuffer;
}

Expand All @@ -191,7 +191,7 @@ export class BIP44CoinTypeNode implements BIP44CoinTypeNodeInterface {
return this.#node.compressedPublicKey;
}

public get compressedPublicKeyBuffer(): Buffer {
public get compressedPublicKeyBuffer(): Uint8Array {
return this.#node.compressedPublicKeyBuffer;
}

Expand Down
7 changes: 4 additions & 3 deletions src/BIP44Node.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bytesToHex } from '@metamask/utils';
import fixtures from '../test/fixtures';
import { createBip39KeyFromSeed, deriveChildKey } from './derivers/bip39';
import { hexStringToBuffer } from './utils';
Expand Down Expand Up @@ -131,7 +132,7 @@ describe('BIP44Node', () => {

// getter
['depth', 'privateKey', 'publicKey', 'address'].forEach((property) => {
expect(() => (node[property] = Buffer.allocUnsafe(64).fill(1))).toThrow(
expect(() => (node[property] = new Uint8Array(64).fill(1))).toThrow(
expect.objectContaining({
name: 'TypeError',
message: expect.stringMatching(
Expand Down Expand Up @@ -443,7 +444,7 @@ describe('BIP44Node', () => {
});

expect(node.compressedPublicKey).toStrictEqual(
compressPublicKey(node.publicKeyBuffer).toString('hex'),
bytesToHex(compressPublicKey(node.publicKeyBuffer)),
);
});
});
Expand Down Expand Up @@ -478,7 +479,7 @@ describe('BIP44Node', () => {

const extendedKey = encodeExtendedKey({
version: PRIVATE_KEY_VERSION,
privateKey: node.privateKeyBuffer as Buffer,
privateKey: node.privateKeyBuffer as Uint8Array,
chainCode: node.chainCodeBuffer,
depth: node.depth,
parentFingerprint: node.parentFingerprint,
Expand Down
14 changes: 7 additions & 7 deletions src/BIP44Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type BIP44ExtendedKeyOptions = {
readonly depth: number;
readonly parentFingerprint: number;
readonly index: number;
readonly chainCode: Buffer | string;
readonly privateKey?: Buffer | string;
readonly publicKey?: Buffer | string;
readonly chainCode: Uint8Array | string;
readonly privateKey?: Uint8Array | string;
readonly publicKey?: Uint8Array | string;
};

type BIP44DerivationPathOptions = {
Expand Down Expand Up @@ -221,15 +221,15 @@ export class BIP44Node implements BIP44NodeInterface {
return this.#node.depth as BIP44Depth;
}

public get privateKeyBuffer(): Buffer | undefined {
public get privateKeyBuffer(): Uint8Array | undefined {
return this.#node.privateKeyBuffer;
}

public get publicKeyBuffer(): Buffer {
public get publicKeyBuffer(): Uint8Array {
return this.#node.publicKeyBuffer;
}

public get chainCodeBuffer(): Buffer {
public get chainCodeBuffer(): Uint8Array {
return this.#node.chainCodeBuffer;
}

Expand All @@ -245,7 +245,7 @@ export class BIP44Node implements BIP44NodeInterface {
return this.#node.compressedPublicKey;
}

public get compressedPublicKeyBuffer(): Buffer {
public get compressedPublicKeyBuffer(): Uint8Array {
return this.#node.compressedPublicKeyBuffer;
}

Expand Down
27 changes: 13 additions & 14 deletions src/SLIP10Node.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bytesToHex, hexToBytes } from '@metamask/utils';
import fixtures from '../test/fixtures';
import { ed25519, secp256k1 } from './curves';
import { SLIP10Node } from './SLIP10Node';
Expand Down Expand Up @@ -157,7 +158,7 @@ describe('SLIP10Node', () => {
it('throws if no public or private key is specified', async () => {
await expect(
SLIP10Node.fromExtendedKey({
chainCode: Buffer.alloc(32, 1),
chainCode: new Uint8Array(32).fill(1),
depth: 0,
parentFingerprint: 0,
index: 0,
Expand Down Expand Up @@ -188,8 +189,8 @@ describe('SLIP10Node', () => {
depth: input as any,
parentFingerprint: 0,
index: 0,
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
curve: 'secp256k1',
}),
).rejects.toThrow(
Expand Down Expand Up @@ -218,8 +219,8 @@ describe('SLIP10Node', () => {
depth: 0,
parentFingerprint: input as any,
index: 0,
publicKey: Buffer.alloc(65, 1),
chainCode: Buffer.alloc(32, 1),
publicKey: new Uint8Array(65).fill(1),
chainCode: new Uint8Array(32).fill(1),
curve: 'secp256k1',
}),
).rejects.toThrow(
Expand All @@ -232,30 +233,28 @@ describe('SLIP10Node', () => {
await expect(
SLIP10Node.fromExtendedKey({
privateKey: 'foo',
chainCode: Buffer.alloc(32, 1),
chainCode: new Uint8Array(32).fill(1),
depth: 0,
parentFingerprint: 0,
index: 0,
curve: 'secp256k1',
}),
).rejects.toThrow(
'Invalid value: Must be a valid hex string of length: 64.',
);
).rejects.toThrow('Value must be a hexadecimal string.');
});

it('throws if the private key is not a Buffer or hexadecimal string', async () => {
await expect(
SLIP10Node.fromExtendedKey({
// @ts-expect-error Invalid private key type.
privateKey: 123,
chainCode: Buffer.alloc(32, 1),
chainCode: new Uint8Array(32).fill(1),
depth: 0,
parentFingerprint: 0,
index: 0,
curve: 'secp256k1',
}),
).rejects.toThrow(
'Invalid value: Expected a Buffer or hexadecimal string.',
'Invalid value: Expected an instance of Uint8Array or hexadecimal string.',
);
});
});
Expand Down Expand Up @@ -338,7 +337,7 @@ describe('SLIP10Node', () => {
'publicKeyBuffer',
'chainCodeBuffer',
].forEach((property) => {
expect(() => (node[property] = Buffer.allocUnsafe(64).fill(1))).toThrow(
expect(() => (node[property] = new Uint8Array(64).fill(1))).toThrow(
expect.objectContaining({
name: 'TypeError',
message: expect.stringMatching(
Expand Down Expand Up @@ -532,7 +531,7 @@ describe('SLIP10Node', () => {
});

expect(node.compressedPublicKey).toStrictEqual(
compressPublicKey(node.publicKeyBuffer).toString('hex'),
bytesToHex(compressPublicKey(node.publicKeyBuffer)),
);
});
});
Expand Down Expand Up @@ -563,7 +562,7 @@ describe('SLIP10Node', () => {
'returns the address for an secp256k1 node',
async ({ index, address }) => {
const { privateKey, chainCode } = await createBip39KeyFromSeed(
hexStringToBuffer(hexSeed),
hexToBytes(hexSeed),
secp256k1,
);

Expand Down
Loading