Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 241ad33

Browse files
Merge pull request #6 from appliedblockchain/encryption
Encryption
2 parents c1eeb0a + ec20526 commit 241ad33

File tree

6 files changed

+24
-37
lines changed

6 files changed

+24
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "b-privacy",
4-
"version": "0.2.1",
4+
"version": "0.3.0",
55
"main": "src/b-privacy.js",
66
"dependencies": {
77
"bitcore-lib": "^0.15.0",

src/asymmetric-encryption.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ function kdf(keyMaterial, keyLength) {
2121

2222
// Encrypts `input` JSON message using `privateKey` and `remoteKey` public key.
2323
function encrypt(input, _privateKey, _remoteKey) {
24-
2524
const data = Buffer.from(JSON.stringify(input), 'utf8');
2625

2726
// We'll work on buffer for private key.
28-
const privateKey = toBuffer(_privateKey);
27+
const privateKey = _privateKey;
2928

3029
// We'll work on buffer for remote public key.
31-
const remoteKey = toBuffer(_remoteKey);
30+
const remoteKey = _remoteKey;
3231

3332
// Derive secret.
3433
const secret = ec.keyFromPrivate(privateKey)
35-
.derive(ec.keyFromPublic(remoteKey).getPublic())
34+
.derive(ec.keyFromPublic({x: remoteKey.slice(0,32), y: remoteKey.slice(32,remoteKey.length)}).getPublic())
3635
.toArrayLike(Buffer);
3736

3837
const key = kdf(secret, 32);

src/b-privacy.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ class BPrivacy {
3535
}
3636
}
3737

38-
// Returns public key as `Buffer`.
39-
get publicKey() {
40-
return toBuffer(this.pubKey.toString());
41-
}
42-
43-
// Returns private key as `Buffer`.
44-
get privateKey() {
45-
return toBuffer(this.pvtKey.toString());
46-
}
47-
4838
static generateMnemonicPhrase() {
4939
return new Mnemonic().phrase;
5040
}
@@ -71,25 +61,28 @@ class BPrivacy {
7161
const pathLevel = `44'/${coinType}'/${account}'/${change}` // *note2
7262
const derivedChild = this.hdKey.derive(`m/${pathLevel}/${index}`)
7363
const pvtKey = derivedChild.privateKey
74-
this.pvtKey = pvtKey
75-
this.pubKey = pvtKey.publicKey
64+
this.pvtKeyBtc = pvtKey;
65+
this.pvtKey = toBuffer(pvtKey.toString());
66+
this.pubKey = this.deriveEthereumPublicKey();
7667
this.keyIdx = index
77-
this.deriveEthereumAddress()
78-
return pvtKey
68+
this.address = this.deriveEthereumAddress()
69+
}
70+
71+
deriveEthereumPublicKey() {
72+
return util.privateToPublic(this.pvtKey);
7973
}
8074

8175
deriveEthereumAddress() {
8276
const eBip44 = EthereumBip44.fromPrivateSeed(this.hdKey.toString())
8377
const address = eBip44.getAddress(this.keyIdx)
84-
this.address = address
85-
return
78+
return address;
8679
}
8780

8881
// returns a promise with one parameter, the message signature
8982
sign(message) {
9083
const msgHash = util.sha3(message)
9184

92-
const signature = util.ecsign(msgHash, new Buffer(this.pvtKey.toString(), 'hex'))
85+
const signature = util.ecsign(msgHash, this.pvtKey)
9386
return signature
9487
}
9588

@@ -120,12 +113,12 @@ class BPrivacy {
120113
// Encrypts `input` using `BPrivacy`'s private key and provided reader's
121114
// remote `publicKey`.
122115
encrypt(input, remoteKey) {
123-
return BPrivacy.encrypt(input, this.privateKey, remoteKey);
116+
return BPrivacy.encrypt(input, this.pvtKey, remoteKey);
124117
}
125118

126119
// Decrypts `input` message using `BPrivacy`'s private key.
127120
decrypt(input) {
128-
return BPrivacy.decrypt(input, this.privateKey);
121+
return BPrivacy.decrypt(input, this.pvtKey);
129122
}
130123

131124
}

src/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module.exports = {
105105
isHex,
106106
isHex0x,
107107
toBuffer,
108-
// toHex,
108+
//toHex,
109109
toHex0x,
110110
areBuffersEqual,
111111
kindOf,

test/asymmetric-encryption.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('AsymmetricEncryption', function () {
1111
const a = new B({ mnemonic: B.generateMnemonicPhrase() })
1212
const b = new B({ mnemonic: B.generateMnemonicPhrase() })
1313
const m = { foo: "bar" };
14-
const a2b = a.encrypt(m, b.publicKey);
14+
const a2b = a.encrypt(m, b.pubKey);
1515
const m2 = b.decrypt(a2b);
1616
t(m2, m);
1717
});

test/b-privacy.test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const c = console
22
const BPrivacy = require('../src/b-privacy.js')
33
const localStorageMock = {}
44
let mnemonicTmp
5+
const { toHex0x, toBuffer } = require('../src/core');
56

67
const examplePhrase = "aspect else project orient seed doll admit remind library turkey dutch inhale"
78

@@ -46,13 +47,13 @@ test('generates a 12 work mnemoic phrase', () => {
4647
test('derives the first private key, public key and address', () => {
4748
const mnemonicPhrase = BPrivacy.generateMnemonicPhrase();
4849
const bp = new BPrivacy({mnemonic: mnemonicPhrase})
49-
bp.deriveKey()
5050
const key = bp.pvtKey
5151
expect(key).toBeDefined()
52-
expect(key.toString()).toHaveLength(64)
52+
// We use to 0x prefix for all
53+
expect(toHex0x(key)).toHaveLength(66)
5354
const pubKey = bp.pubKey
5455
expect(pubKey).toBeDefined()
55-
expect(pubKey.toString()).toHaveLength(66)
56+
expect(toHex0x(pubKey)).toHaveLength(130)
5657
const address = bp.address
5758
expect(address).toBeDefined()
5859
expect(address.toString()).toHaveLength(42)
@@ -104,14 +105,8 @@ test('signs a message via web3Sign', () => {
104105
// optput: 0x3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1cb 0x1c1931b8a3c91af0afae485d8ce5b597c5f2424f2bb3055b56e0204790047dd85379bce18b7279a04a1674d4bbfc302a5a68fb497da4a90bd924991bbeb7db1b1b 0xfc86f571353e44568aa9103db4edd7f53a410c73
105106
})
106107

107-
test('converts a stringified public_key (without 0x prefix) to an address', () => {
108-
const bp = new BPrivacy({mnemonic: examplePhrase})
109-
let staticAddress = BPrivacy.publicKeyToAddress(bp.pubKey.toString());
110-
expect(staticAddress).toEqual(bp.address);
111-
});
112-
113-
test('converts a stringified public_key (with 0x prefix) to an address', () => {
108+
test('converts a public_key to an address', () => {
114109
const bp = new BPrivacy({mnemonic: examplePhrase})
115-
let staticAddress = BPrivacy.publicKeyToAddress(`0x${bp.pubKey.toString()}`);
110+
let staticAddress = BPrivacy.publicKeyToAddress(bp.pubKey);
116111
expect(staticAddress).toEqual(bp.address);
117112
});

0 commit comments

Comments
 (0)