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

Commit

Permalink
Merge pull request #6 from appliedblockchain/encryption
Browse files Browse the repository at this point in the history
Encryption
  • Loading branch information
campbell-codes authored Nov 15, 2017
2 parents c1eeb0a + ec20526 commit 241ad33
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "b-privacy",
"version": "0.2.1",
"version": "0.3.0",
"main": "src/b-privacy.js",
"dependencies": {
"bitcore-lib": "^0.15.0",
Expand Down
7 changes: 3 additions & 4 deletions src/asymmetric-encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ function kdf(keyMaterial, keyLength) {

// Encrypts `input` JSON message using `privateKey` and `remoteKey` public key.
function encrypt(input, _privateKey, _remoteKey) {

const data = Buffer.from(JSON.stringify(input), 'utf8');

// We'll work on buffer for private key.
const privateKey = toBuffer(_privateKey);
const privateKey = _privateKey;

// We'll work on buffer for remote public key.
const remoteKey = toBuffer(_remoteKey);
const remoteKey = _remoteKey;

// Derive secret.
const secret = ec.keyFromPrivate(privateKey)
.derive(ec.keyFromPublic(remoteKey).getPublic())
.derive(ec.keyFromPublic({x: remoteKey.slice(0,32), y: remoteKey.slice(32,remoteKey.length)}).getPublic())
.toArrayLike(Buffer);

const key = kdf(secret, 32);
Expand Down
31 changes: 12 additions & 19 deletions src/b-privacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ class BPrivacy {
}
}

// Returns public key as `Buffer`.
get publicKey() {
return toBuffer(this.pubKey.toString());
}

// Returns private key as `Buffer`.
get privateKey() {
return toBuffer(this.pvtKey.toString());
}

static generateMnemonicPhrase() {
return new Mnemonic().phrase;
}
Expand All @@ -71,25 +61,28 @@ class BPrivacy {
const pathLevel = `44'/${coinType}'/${account}'/${change}` // *note2
const derivedChild = this.hdKey.derive(`m/${pathLevel}/${index}`)
const pvtKey = derivedChild.privateKey
this.pvtKey = pvtKey
this.pubKey = pvtKey.publicKey
this.pvtKeyBtc = pvtKey;
this.pvtKey = toBuffer(pvtKey.toString());
this.pubKey = this.deriveEthereumPublicKey();
this.keyIdx = index
this.deriveEthereumAddress()
return pvtKey
this.address = this.deriveEthereumAddress()
}

deriveEthereumPublicKey() {
return util.privateToPublic(this.pvtKey);
}

deriveEthereumAddress() {
const eBip44 = EthereumBip44.fromPrivateSeed(this.hdKey.toString())
const address = eBip44.getAddress(this.keyIdx)
this.address = address
return
return address;
}

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

const signature = util.ecsign(msgHash, new Buffer(this.pvtKey.toString(), 'hex'))
const signature = util.ecsign(msgHash, this.pvtKey)
return signature
}

Expand Down Expand Up @@ -120,12 +113,12 @@ class BPrivacy {
// Encrypts `input` using `BPrivacy`'s private key and provided reader's
// remote `publicKey`.
encrypt(input, remoteKey) {
return BPrivacy.encrypt(input, this.privateKey, remoteKey);
return BPrivacy.encrypt(input, this.pvtKey, remoteKey);
}

// Decrypts `input` message using `BPrivacy`'s private key.
decrypt(input) {
return BPrivacy.decrypt(input, this.privateKey);
return BPrivacy.decrypt(input, this.pvtKey);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module.exports = {
isHex,
isHex0x,
toBuffer,
// toHex,
//toHex,
toHex0x,
areBuffersEqual,
kindOf,
Expand Down
2 changes: 1 addition & 1 deletion test/asymmetric-encryption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('AsymmetricEncryption', function () {
const a = new B({ mnemonic: B.generateMnemonicPhrase() })
const b = new B({ mnemonic: B.generateMnemonicPhrase() })
const m = { foo: "bar" };
const a2b = a.encrypt(m, b.publicKey);
const a2b = a.encrypt(m, b.pubKey);
const m2 = b.decrypt(a2b);
t(m2, m);
});
Expand Down
17 changes: 6 additions & 11 deletions test/b-privacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const c = console
const BPrivacy = require('../src/b-privacy.js')
const localStorageMock = {}
let mnemonicTmp
const { toHex0x, toBuffer } = require('../src/core');

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

Expand Down Expand Up @@ -46,13 +47,13 @@ test('generates a 12 work mnemoic phrase', () => {
test('derives the first private key, public key and address', () => {
const mnemonicPhrase = BPrivacy.generateMnemonicPhrase();
const bp = new BPrivacy({mnemonic: mnemonicPhrase})
bp.deriveKey()
const key = bp.pvtKey
expect(key).toBeDefined()
expect(key.toString()).toHaveLength(64)
// We use to 0x prefix for all
expect(toHex0x(key)).toHaveLength(66)
const pubKey = bp.pubKey
expect(pubKey).toBeDefined()
expect(pubKey.toString()).toHaveLength(66)
expect(toHex0x(pubKey)).toHaveLength(130)
const address = bp.address
expect(address).toBeDefined()
expect(address.toString()).toHaveLength(42)
Expand Down Expand Up @@ -104,14 +105,8 @@ test('signs a message via web3Sign', () => {
// optput: 0x3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1cb 0x1c1931b8a3c91af0afae485d8ce5b597c5f2424f2bb3055b56e0204790047dd85379bce18b7279a04a1674d4bbfc302a5a68fb497da4a90bd924991bbeb7db1b1b 0xfc86f571353e44568aa9103db4edd7f53a410c73
})

test('converts a stringified public_key (without 0x prefix) to an address', () => {
const bp = new BPrivacy({mnemonic: examplePhrase})
let staticAddress = BPrivacy.publicKeyToAddress(bp.pubKey.toString());
expect(staticAddress).toEqual(bp.address);
});

test('converts a stringified public_key (with 0x prefix) to an address', () => {
test('converts a public_key to an address', () => {
const bp = new BPrivacy({mnemonic: examplePhrase})
let staticAddress = BPrivacy.publicKeyToAddress(`0x${bp.pubKey.toString()}`);
let staticAddress = BPrivacy.publicKeyToAddress(bp.pubKey);
expect(staticAddress).toEqual(bp.address);
});

0 comments on commit 241ad33

Please sign in to comment.