-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(encryption): Added encryption library
- Loading branch information
1 parent
e324827
commit 9bab145
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
This library contains useful functions that deal with encryption. | ||
*/ | ||
|
||
const axios = require("axios") | ||
|
||
let _this | ||
|
||
class Encryption { | ||
constructor(config) { | ||
this.restURL = config.restURL | ||
this.apiToken = config.apiToken | ||
this.axios = axios | ||
// console.log(`Blockbook apiToken: ${this.apiToken}`) | ||
|
||
// Add JWT token to the authorization header. | ||
this.axiosOptions = { | ||
headers: { | ||
authorization: `Token ${this.apiToken}` | ||
} | ||
} | ||
|
||
_this = this | ||
} | ||
|
||
// Search the blockchain for a public key associated with a BCH address. | ||
async getPubKey(addr) { | ||
try { | ||
if (!addr || typeof addr !== "string") | ||
throw new Error(`Input must be a valid Bitcoin Cash address.`) | ||
|
||
const response = await _this.axios.get( | ||
`${this.restURL}encryption/publickey/${addr}`, | ||
_this.axiosOptions | ||
) | ||
return response.data | ||
} catch (error) { | ||
if (error.response && error.response.data) throw error.response.data | ||
else throw error | ||
} | ||
} | ||
} | ||
|
||
module.exports = Encryption |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const assert = require("chai").assert | ||
|
||
const BCHJS = require("../../src/bch-js") | ||
const bchjs = new BCHJS() | ||
|
||
describe("#Encryption", () => { | ||
describe("#getPubKey", () => { | ||
it("should get a public key", async () => { | ||
const addr = "bitcoincash:qpf8jv9hmqcda0502gjp7nm3g24y5h5s4unutghsxq" | ||
|
||
const result = await bchjs.encryption.getPubKey(addr) | ||
// console.log(`result: ${JSON.stringify(result, null, 2)}`) | ||
|
||
assert.property(result, "success") | ||
assert.equal(result.success, true) | ||
assert.property(result, "publicKey") | ||
}) | ||
|
||
it("should report when public key can not be found", async () => { | ||
const addr = "bitcoincash:qpxqr2pmcverj4vukgjqssvk2zju8tp9xsgz2nqagx" | ||
|
||
const result = await bchjs.encryption.getPubKey(addr) | ||
// console.log(`result: ${JSON.stringify(result, null, 2)}`) | ||
|
||
assert.property(result, "success") | ||
assert.equal(result.success, false) | ||
assert.property(result, "publicKey") | ||
assert.equal(result.publicKey, "not found") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const assert = require("chai").assert | ||
const sinon = require("sinon") | ||
|
||
const BCHJS = require("../../src/bch-js") | ||
// const bchjs = new BCHJS() | ||
let bchjs | ||
|
||
const mockData = require("./fixtures/encryption-mock") | ||
|
||
describe("#Encryption", () => { | ||
let sandbox | ||
|
||
beforeEach(() => { | ||
bchjs = new BCHJS() | ||
sandbox = sinon.createSandbox() | ||
}) | ||
|
||
afterEach(() => sandbox.restore()) | ||
|
||
describe("#getPubKey", () => { | ||
// Failure address: | ||
// bitcoincash:qpxqr2pmcverj4vukgjqssvk2zju8tp9xsgz2nqagx | ||
|
||
it("should throw error if BCH address is not provided.", async () => { | ||
try { | ||
await bchjs.encryption.getPubKey() | ||
|
||
assert.equal(true, false, "Unexpected result!") | ||
} catch (err) { | ||
// console.log(`err: `, err) | ||
assert.include( | ||
err.message, | ||
"Input must be a valid Bitcoin Cash address" | ||
) | ||
} | ||
}) | ||
|
||
it("should report when public key can not be found", async () => { | ||
// Stub the network call. | ||
sandbox | ||
.stub(bchjs.encryption.axios, "get") | ||
.resolves({ data: mockData.failureMock }) | ||
|
||
const addr = "bitcoincash:qpxqr2pmcverj4vukgjqssvk2zju8tp9xsgz2nqagx" | ||
|
||
const result = await bchjs.encryption.getPubKey(addr) | ||
// console.log(`result: ${JSON.stringify(result, null, 2)}`) | ||
|
||
assert.property(result, "success") | ||
assert.equal(result.success, false) | ||
assert.property(result, "publicKey") | ||
assert.equal(result.publicKey, "not found") | ||
}) | ||
|
||
it("should get a public key", async () => { | ||
// Stub the network call. | ||
sandbox | ||
.stub(bchjs.encryption.axios, "get") | ||
.resolves({ data: mockData.successMock }) | ||
|
||
const addr = "bitcoincash:qpf8jv9hmqcda0502gjp7nm3g24y5h5s4unutghsxq" | ||
|
||
const result = await bchjs.encryption.getPubKey(addr) | ||
// console.log(`result: ${JSON.stringify(result, null, 2)}`) | ||
|
||
assert.property(result, "success") | ||
assert.equal(result.success, true) | ||
assert.property(result, "publicKey") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
Mock data used for unit tests against the Encryption library. | ||
*/ | ||
|
||
const successMock = { | ||
success: true, | ||
publicKey: | ||
"03fcc37586d93af1e146238217989924e0ab1f011c34e1a23aec529354d5b28eb4" | ||
} | ||
|
||
const failureMock = { | ||
success: false, | ||
publicKey: "not found" | ||
} | ||
|
||
module.exports = { | ||
successMock, | ||
failureMock | ||
} |