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

Validate decodePrivateKeyWif version and length #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions src/lib/key/wallet-import-format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
encodePrivateKeyWif,
hexToBin,
sha256 as internalSha256,
WalletImportFormatError,
} from '../lib.js';

test('decodePrivateKeyWif: pass through errors', (t) => {
Expand All @@ -17,6 +18,30 @@ test('decodePrivateKeyWif: pass through errors', (t) => {
);
});

test('decodePrivateKeyWif: invalid version (base58/legacy address)', (t) => {
t.deepEqual(
// cspell: disable-next-line
decodePrivateKeyWif('1CQbfkN8cADaJWk29ARtaa55UNdBa1kLaA'),
WalletImportFormatError.invalidVersion,
);
});

test('decodePrivateKeyWif: incorrect length (mainnet wif version, 20 bytes)', (t) => {
t.deepEqual(
// cspell: disable-next-line
decodePrivateKeyWif('tWGD2u9st6K9gUr68hdo53qhZZyk3JoQAF'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These WIFs have just been generated with the following code:

import { Base58AddressFormatVersion, encodeBase58AddressFormat, hexToBin,  } from '@bitauth/libauth';

const mainnetWifInvalidLength = encodeBase58AddressFormat(Base58AddressFormatVersion.wif, new Uint8Array(20));
const testnetWifInvalidLength = encodeBase58AddressFormat(Base58AddressFormatVersion.wifTestnet, new Uint8Array(20));

console.log('mainnetWifInvalidLength', mainnetWifInvalidLength);
console.log('testnetWifInvalidLength', testnetWifInvalidLength);

WalletImportFormatError.incorrectLength,
);
});

test('decodePrivateKeyWif: incorrect length (testnet wif version, 20 bytes)', (t) => {
t.deepEqual(
// cspell: disable-next-line
decodePrivateKeyWif('2fAnAKxErh7kQTbKhrGcAty42RZaSyLdLhp'),
WalletImportFormatError.incorrectLength,
);
});

const wifVectors = test.macro<[WalletImportFormatType, string, string]>({
// eslint-disable-next-line @typescript-eslint/max-params
exec: (t, type, wif, key) => {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/key/wallet-import-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Sha256 } from '../lib.js';

export enum WalletImportFormatError {
incorrectLength = 'The WIF private key payload is not the correct length.',
invalidVersion = 'The WIF private key does not have a valid version',
}

/**
Expand Down Expand Up @@ -89,9 +90,20 @@ export const decodePrivateKeyWif = (
wifKey: string,
sha256: { hash: Sha256['hash'] } = internalSha256,
) => {
const uncompressedPayloadLength = 32;
const compressedPayloadLength = 33;
const decoded = decodeBase58AddressFormat(wifKey, sha256);
if (typeof decoded === 'string') return decoded;
if (
decoded.version !== Base58AddressFormatVersion.wif &&
decoded.version !== Base58AddressFormatVersion.wifTestnet
)
return WalletImportFormatError.invalidVersion;
if (
decoded.payload.length !== uncompressedPayloadLength &&
decoded.payload.length !== compressedPayloadLength
)
return WalletImportFormatError.incorrectLength;
const mainnet = decoded.version === Base58AddressFormatVersion.wif;
const compressed = decoded.payload.length === compressedPayloadLength;
const privateKey = compressed
Expand Down
Loading