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

perf(crypto): native bip38 #1941

Merged
merged 12 commits into from
Jan 4, 2019
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
2 changes: 0 additions & 2 deletions packages/core-json-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@arkecosystem/core-http-utils": "^2.1.0",
"@arkecosystem/crypto": "^2.1.0",
"@keyv/sqlite": "^2.0.0",
"@types/bip38": "^2.0.0",
"@types/bip39": "^2.4.1",
"@types/boom": "^7.2.1",
"@types/joi": "^14.0.0",
Expand All @@ -42,7 +41,6 @@
"@types/uuid": "^3.4.4",
"@types/wif": "^2.0.1",
"axios": "^0.18.0",
"bip38": "^2.0.2",
"bip39": "^2.5.0",
"boom": "^7.3.0",
"is-reachable": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { crypto, HashAlgorithms } from "@arkecosystem/crypto";
import bip38 from "bip38";
import { bip38, crypto, HashAlgorithms } from "@arkecosystem/crypto";
import bip39 from "bip39";
import Joi from "joi";
import { database } from "../../../services/database";
Expand Down
3 changes: 1 addition & 2 deletions packages/core-json-rpc/src/server/utils/decrypt-wif.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { configManager, crypto } from "@arkecosystem/crypto";
import bip38 from "bip38";
import { bip38, configManager, crypto } from "@arkecosystem/crypto";
import wif from "wif";

export const decryptWIF = (encryptedWif, userId, bip38password) => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"@types/bip38": "^2.0.0",
"@types/commander": "^2.12.2",
"@types/wif": "^2.0.1",
"bip38": "^2.0.2",
"commander": "^2.19.0",
"wif": "^2.0.6"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

import { configManager, crypto } from "@arkecosystem/crypto";
import bip38 from "bip38";
import { bip38, configManager, crypto } from "@arkecosystem/crypto";
import app from "commander";
import fs from "fs";
import wif from "wif";
Expand Down
56 changes: 56 additions & 0 deletions packages/crypto/__tests__/crypto/bip38.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "jest-extended";

import bs58check from "bs58check";
import wif from "wif";
import { bip38 } from "../../src/crypto";

import fixtures from "./fixtures/bip38.json";

describe("BIP38", () => {
describe("decrypt", () => {
fixtures.valid.forEach(fixture => {
it(`should decrypt '${fixture.description}'`, () => {
const result = bip38.decrypt(fixture.bip38, fixture.passphrase);
expect(wif.encode(0x80, result.privateKey, result.compressed)).toEqual(fixture.wif);
});
});

fixtures.invalid.verify.forEach(fixture => {
it(`should not decrypt '${fixture.description}'`, () => {
try {
bip38.decrypt(fixture.base58, "foobar");
} catch (error) {
expect(error.message).toEqual(fixture.exception);
}
});
});
});

describe("encrypt", () => {
fixtures.valid.forEach(fixture => {
if (fixture.decryptOnly) {
return;
}

it(`should encrypt '${fixture.description}'`, () => {
const buffer = bs58check.decode(fixture.wif);
const actual = bip38.encrypt(buffer.slice(1, 33), !!buffer[33], fixture.passphrase);
expect(actual).toEqual(fixture.bip38);
});
});
});

describe("verify", () => {
fixtures.valid.forEach(fixture => {
it(`should verify '${fixture.bip38}'`, () => {
expect(bip38.verify(fixture.bip38)).toBeTrue();
});
});

fixtures.invalid.verify.forEach(fixture => {
it(`should not verify '${fixture.description}'`, () => {
expect(bip38.verify(fixture.base58)).toBeFalse();
});
});
});
});
121 changes: 121 additions & 0 deletions packages/crypto/__tests__/crypto/fixtures/bip38.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"valid": [
{
"passphrase": "TestingOneTwoThree",
"bip38": "6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg",
"wif": "5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR",
"address": "1Jq6MksXQVWzrznvZzxkV6oY57oWXD9TXB",
"description": "no EC multiply / no compression #1"
},
{
"passphrase": "Satoshi",
"bip38": "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq",
"wif": "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5",
"address": "1AvKt49sui9zfzGeo8EyL8ypvAhtR2KwbL",
"description": "no EC multiply / no compression #2"
},
{
"passphrase": "TestingOneTwoThree",
"bip38": "6PYNKZ1EAgYgmQfmNVamxyXVWHzK5s6DGhwP4J5o44cvXdoY7sRzhtpUeo",
"wif": "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP",
"address": "164MQi977u9GUteHr4EPH27VkkdxmfCvGW",
"description": "no EC multiply / compression #1"
},
{
"passphrase": "Satoshi",
"bip38": "6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7",
"wif": "KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7",
"address": "1HmPbwsvG5qJ3KJfxzsZRZWhbm1xBMuS8B",
"description": "no EC multiply / compression #2"
},
{
"passphrase": "TestingOneTwoThree",
"bip38": "6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX",
"wif": "5K4caxezwjGCGfnoPTZ8tMcJBLB7Jvyjv4xxeacadhq8nLisLR2",
"address": "1PE6TQi6HTVNz5DLwB1LcpMBALubfuN2z2",
"description": "EC multiply / no compression, no lot sequence #1",
"decryptOnly": true,
"code": "passphrasepxFy57B9v8HtUsszJYKReoNDV6VHjUSGt8EVJmux9n1J3Ltf1gRxyDGXqnf9qm"
},
{
"passphrase": "Satoshi",
"bip38": "6PfLGnQs6VZnrNpmVKfjotbnQuaJK4KZoPFrAjx1JMJUa1Ft8gnf5WxfKd",
"wif": "5KJ51SgxWaAYR13zd9ReMhJpwrcX47xTJh2D3fGPG9CM8vkv5sH",
"address": "1CqzrtZC6mXSAhoxtFwVjz8LtwLJjDYU3V",
"description": "EC multiply / no compression, no lot sequence #2",
"decryptOnly": true,
"code": "passphraseoRDGAXTWzbp72eVbtUDdn1rwpgPUGjNZEc6CGBo8i5EC1FPW8wcnLdq4ThKzAS"
},
{
"passphrase": "MOLON LABE",
"bip38": "6PgNBNNzDkKdhkT6uJntUXwwzQV8Rr2tZcbkDcuC9DZRsS6AtHts4Ypo1j",
"wif": "5JLdxTtcTHcfYcmJsNVy1v2PMDx432JPoYcBTVVRHpPaxUrdtf8",
"address": "1Jscj8ALrYu2y9TD8NrpvDBugPedmbj4Yh",
"description": "EC multiply / no compression, lot sequence #1",
"decryptOnly": true,
"confirm": "cfrm38V8aXBn7JWA1ESmFMUn6erxeBGZGAxJPY4e36S9QWkzZKtaVqLNMgnifETYw7BPwWC9aPD",
"code": "passphraseaB8feaLQDENqCgr4gKZpmf4VoaT6qdjJNJiv7fsKvjqavcJxvuR1hy25aTu5sX",
"lot": 263183,
"seq": 1
},
{
"passphrase": "ΜΟΛΩΝ ΛΑΒΕ",
"bip38": "6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5mar2ngH",
"wif": "5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D",
"address": "1Lurmih3KruL4xDB5FmHof38yawNtP9oGf",
"description": "EC multiply / no compression, lot sequence #1",
"decryptOnly": true,
"confirm": "cfrm38V8G4qq2ywYEFfWLD5Cc6msj9UwsG2Mj4Z6QdGJAFQpdatZLavkgRd1i4iBMdRngDqDs51",
"code": "passphrased3z9rQJHSyBkNBwTRPkUGNVEVrUAcfAXDyRU1V28ie6hNFbqDwbFBvsTK7yWVK",
"lot": 806938,
"sequence": 1
}
],
"invalid": {
"decrypt": [],
"encrypt": [],
"verify": [
{
"description": "Invalid base58",
"exception": "Invalid checksum",
"base58": "6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5marXXXX"
},
{
"description": "Length > 39",
"exception": "Invalid BIP38 data length",
"hex": "0142c000000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "QmxDezFMDL7ExfYmsETsQXAtBbw5YE1CDyA8pm1AGpMpVVUpsVy1yXv4VTL"
},
{
"description": "Length < 39",
"exception": "Invalid BIP38 data length",
"hex": "0142c00000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "2DnNxWcx4Prn8wmjbkvtYGDALsq8BMWxQ33KnXkeH8vrxE41psDLXRmK3"
},
{
"description": "prefix !== 0x01",
"exception": "Invalid BIP38 prefix",
"hex": "0242c0000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "AfE1YY4Wr2FLAENaH9PVaLRdyk714V4rhwiJMSGyQCGFB3rhGDCs2R7c4s"
},
{
"description": "flag !== 0xc0 && flag !== 0xe0",
"exception": "Invalid BIP38 type",
"hex": "0101ff000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "5JjnYkbFBmUnhGeDMVhR7aSitLToe1odEfXDBeg4RMK6JmAm9g7rkm7qY3"
},
{
"description": "EC Mult: ~(flag & 0x24)",
"exception": "Invalid BIP38 type",
"hex": "0101db000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "5JbtdQFKSemRTqMuWrJgSfzE8AX2jdz1KiZuMmuUcv9iXha1s6UarQTciW"
},
{
"description": "EC Mult: ~(flag & 0x24)",
"exception": "Invalid BIP38 type",
"hex": "010135000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "5HyV7HSYdHUgLf7w36mxMHDPH9muTgUYHEj6cEogKMuV7ae8VRM3VEg56w"
}
]
}
}
2 changes: 0 additions & 2 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"dependencies": {
"@types/bip32": "^1.0.0",
"@types/bip38": "^2.0.0",
"@types/bip39": "^2.4.1",
"@types/bytebuffer": "^5.0.37",
"@types/create-hash": "^1.2.0",
Expand All @@ -53,7 +52,6 @@
"@types/wif": "^2.0.1",
"bignumber.js": "^8.0.1",
"bip32": "^1.0.2",
"bip38": "^2.0.2",
"bip39": "^2.5.0",
"bs58check": "^2.1.2",
"bytebuffer": "^5.0.1",
Expand Down
Loading