Skip to content

Commit 20a6009

Browse files
committed
added Bitcoin WIF & Sui Bech32 private key import/export
1 parent 251f863 commit 20a6009

22 files changed

+739
-72
lines changed

export/index.template.html

Lines changed: 376 additions & 1 deletion
Large diffs are not rendered by default.

export/index.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,67 @@ describe("TKHQ", () => {
120120
expect(encodedKey).toEqual(keySol);
121121
});
122122

123+
it("encodes bitcoin WIF private key correctly", async () => {
124+
const keyWif = "L1sF5SF3CnCN9gA7vh7MAtbiVu9igdr3C1BYPKZduw4yaezdeCTV";
125+
const keyWifBytes = TKHQ.base58Decode(keyWif).subarray(0, -4); // remove 4 byte checksum
126+
expect(keyWifBytes.length).toEqual(34); // 1 byte version + 32 byte privkey + 1 byte compressed flag
127+
const keyPrivBytes = keyWifBytes.subarray(1, 33);
128+
const encodedKey = await TKHQ.encodeKey(
129+
keyPrivBytes,
130+
"BITCOIN_MAINNET_WIF"
131+
);
132+
expect(encodedKey).toEqual(keyWif);
133+
});
134+
135+
// Bitcoin WIF export negative tests
136+
it("rejects bitcoin WIF encoding with wrong private key length", async () => {
137+
const tooShortKey = new Uint8Array(31).fill(0x42);
138+
await expect(
139+
TKHQ.encodeKey(tooShortKey, "BITCOIN_MAINNET_WIF")
140+
).rejects.toThrow("invalid private key length. Expected 32 bytes. Got 31.");
141+
142+
const tooLongKey = new Uint8Array(33).fill(0x42);
143+
await expect(
144+
TKHQ.encodeKey(tooLongKey, "BITCOIN_MAINNET_WIF")
145+
).rejects.toThrow("invalid private key length. Expected 32 bytes. Got 33.");
146+
});
147+
148+
// Encodes Testnet WIF correctly
149+
it("encodes bitcoin Testnet WIF private key correctly", async () => {
150+
const keyWif = "cTVYYVnHVzNepyxZhB7fzRuneC6RVDJETJdGAoDc4RrirgghQxyR";
151+
const keyWifBytes = TKHQ.base58Decode(keyWif).subarray(0, -4); // remove 4 byte checksum
152+
expect(keyWifBytes.length).toEqual(34); // 1 byte version + 32 byte privkey + 1 byte compressed flag
153+
const keyPrivBytes = keyWifBytes.subarray(1, 33);
154+
const encodedKey = await TKHQ.encodeKey(
155+
keyPrivBytes,
156+
"BITCOIN_TESTNET_WIF"
157+
);
158+
expect(encodedKey).toEqual(keyWif);
159+
});
160+
161+
it("encodes sui bech32 private key correctly", async () => {
162+
const keySui =
163+
"suiprivkey1qpj5xd9396rxsu7h45tzccalhuf95e4pygls3ps9txszn9ywpwsnznaeq0l";
164+
const { words } = TKHQ.decodeBech32(keySui);
165+
const keySuiBytes = TKHQ.bech32FromWords(words).subarray(1); // remove 1 byte scheme flag
166+
expect(keySuiBytes.length).toEqual(32);
167+
const encodedKey = await TKHQ.encodeKey(keySuiBytes, "SUI_BECH32");
168+
expect(encodedKey).toEqual(keySui);
169+
});
170+
171+
// SUI Bech32 export negative tests
172+
it("rejects sui bech32 encoding with wrong private key length", async () => {
173+
const tooShortKey = new Uint8Array(31).fill(0x42);
174+
await expect(TKHQ.encodeKey(tooShortKey, "SUI_BECH32")).rejects.toThrow(
175+
"invalid private key length. Expected 32 bytes. Got 31."
176+
);
177+
178+
const tooLongKey = new Uint8Array(33).fill(0x42);
179+
await expect(TKHQ.encodeKey(tooLongKey, "SUI_BECH32")).rejects.toThrow(
180+
"invalid private key length. Expected 32 bytes. Got 33."
181+
);
182+
});
183+
123184
it("encodes wallet with only mnemonic correctly", async () => {
124185
const mnemonic =
125186
"suffer surround soup duck goose patrol add unveil appear eye neglect hurry alpha project tomorrow embody hen wish twenty join notable amused burden treat";

import/dist/common.bundle.33a2ded2831a9b0e3d8a.js

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

import/dist/common.bundle.9407f0e3109ed80fba97.js.LICENSE.txt renamed to import/dist/common.bundle.33a2ded2831a9b0e3d8a.js.LICENSE.txt

File renamed without changes.

import/dist/common.bundle.33a2ded2831a9b0e3d8a.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

import/dist/common.bundle.9407f0e3109ed80fba97.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

import/dist/common.bundle.9407f0e3109ed80fba97.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

import/dist/index.bundle.fa4b8f8eab66cd51d789.js renamed to import/dist/index.bundle.5eade4e4daa25d7588e3.js

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

import/dist/index.bundle.fa4b8f8eab66cd51d789.js.LICENSE.txt renamed to import/dist/index.bundle.5eade4e4daa25d7588e3.js.LICENSE.txt

File renamed without changes.

import/dist/index.bundle.5eade4e4daa25d7588e3.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)