mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api.git
synced 2026-09-21 16:52:00 -07:00
feat(encryption): Adding encryption REST API route
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
Unit tests for EncryptionRESTController.
|
||||
*/
|
||||
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import EncryptionRESTController from '../../../src/controllers/rest-api/encryption/controller.js'
|
||||
import { createMockRequest, createMockResponse, createMockRequestWithParams } from '../mocks/controller-mocks.js'
|
||||
|
||||
describe('#encryption-controller.js', () => {
|
||||
let sandbox
|
||||
let mockAdapters
|
||||
let mockUseCases
|
||||
let uut
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox()
|
||||
mockAdapters = {}
|
||||
mockUseCases = {
|
||||
encryption: {
|
||||
getPublicKey: sandbox.stub().resolves({
|
||||
success: true,
|
||||
publicKey: '02abc123def456789'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
uut = new EncryptionRESTController({
|
||||
adapters: mockAdapters,
|
||||
useCases: mockUseCases
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore()
|
||||
})
|
||||
|
||||
describe('#constructor()', () => {
|
||||
it('should require adapters', () => {
|
||||
assert.throws(() => {
|
||||
// eslint-disable-next-line no-new
|
||||
new EncryptionRESTController({ useCases: mockUseCases })
|
||||
}, /Adapters library required/)
|
||||
})
|
||||
|
||||
it('should require encryption use cases', () => {
|
||||
assert.throws(() => {
|
||||
// eslint-disable-next-line no-new
|
||||
new EncryptionRESTController({ adapters: mockAdapters, useCases: {} })
|
||||
}, /Encryption use cases required/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#root()', () => {
|
||||
it('should return encryption status', async () => {
|
||||
const req = createMockRequest()
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.root(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.deepEqual(res.jsonData, { status: 'encryption' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getPublicKey()', () => {
|
||||
it('should return public key on success', async () => {
|
||||
const req = createMockRequestWithParams({
|
||||
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getPublicKey(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: true,
|
||||
publicKey: '02abc123def456789'
|
||||
})
|
||||
assert.isTrue(mockUseCases.encryption.getPublicKey.calledOnce)
|
||||
assert.isTrue(mockUseCases.encryption.getPublicKey.calledWith({
|
||||
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
}))
|
||||
})
|
||||
|
||||
it('should return not found when public key is not found', async () => {
|
||||
mockUseCases.encryption.getPublicKey.resolves({
|
||||
success: false,
|
||||
publicKey: 'not found'
|
||||
})
|
||||
|
||||
const req = createMockRequestWithParams({
|
||||
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getPublicKey(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 200)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
publicKey: 'not found'
|
||||
})
|
||||
})
|
||||
|
||||
it('should reject array addresses', async () => {
|
||||
const req = createMockRequestWithParams({
|
||||
address: ['addr1', 'addr2']
|
||||
})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getPublicKey(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 400)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
error: 'address can not be an array.'
|
||||
})
|
||||
assert.isFalse(mockUseCases.encryption.getPublicKey.called)
|
||||
})
|
||||
|
||||
it('should reject missing address', async () => {
|
||||
const req = createMockRequestWithParams({})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getPublicKey(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 400)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
error: 'address is required.'
|
||||
})
|
||||
assert.isFalse(mockUseCases.encryption.getPublicKey.called)
|
||||
})
|
||||
|
||||
it('should handle errors via handleError', async () => {
|
||||
const error = new Error('No transaction history.')
|
||||
error.status = 400
|
||||
mockUseCases.encryption.getPublicKey.rejects(error)
|
||||
|
||||
const req = createMockRequestWithParams({
|
||||
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getPublicKey(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 400)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
error: 'No transaction history.'
|
||||
})
|
||||
})
|
||||
|
||||
it('should default to 500 status for errors without status', async () => {
|
||||
const error = new Error('Internal error')
|
||||
mockUseCases.encryption.getPublicKey.rejects(error)
|
||||
|
||||
const req = createMockRequestWithParams({
|
||||
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
})
|
||||
const res = createMockResponse()
|
||||
|
||||
await uut.getPublicKey(req, res)
|
||||
|
||||
assert.equal(res.statusValue, 500)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
error: 'Internal error'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('#handleError()', () => {
|
||||
it('should use error status and message when provided', () => {
|
||||
const error = new Error('Custom error')
|
||||
error.status = 422
|
||||
const res = createMockResponse()
|
||||
|
||||
uut.handleError(error, res)
|
||||
|
||||
assert.equal(res.statusValue, 422)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
error: 'Custom error'
|
||||
})
|
||||
})
|
||||
|
||||
it('should default to 500 and Internal server error', () => {
|
||||
const error = {}
|
||||
const res = createMockResponse()
|
||||
|
||||
uut.handleError(error, res)
|
||||
|
||||
assert.equal(res.statusValue, 500)
|
||||
assert.deepEqual(res.jsonData, {
|
||||
success: false,
|
||||
error: 'Internal server error'
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -9,6 +9,7 @@ import RESTControllers from '../../../src/controllers/rest-api/index.js'
|
||||
import BlockchainRouter from '../../../src/controllers/rest-api/full-node/blockchain/router.js'
|
||||
import ControlRouter from '../../../src/controllers/rest-api/full-node/control/router.js'
|
||||
import DSProofRouter from '../../../src/controllers/rest-api/full-node/dsproof/router.js'
|
||||
import EncryptionRouter from '../../../src/controllers/rest-api/encryption/router.js'
|
||||
import MiningRouter from '../../../src/controllers/rest-api/full-node/mining/router.js'
|
||||
import PriceRouter from '../../../src/controllers/rest-api/price/router.js'
|
||||
import RawTransactionsRouter from '../../../src/controllers/rest-api/full-node/rawtransactions/router.js'
|
||||
@@ -100,6 +101,9 @@ describe('#controllers/rest-api/index.js', () => {
|
||||
getMutableCid: () => {},
|
||||
decodeOpReturn: () => {},
|
||||
getCIDData: () => {}
|
||||
},
|
||||
encryption: {
|
||||
getPublicKey: () => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -129,6 +133,7 @@ describe('#controllers/rest-api/index.js', () => {
|
||||
const blockchainAttachStub = sandbox.stub(BlockchainRouter.prototype, 'attach')
|
||||
const controlAttachStub = sandbox.stub(ControlRouter.prototype, 'attach')
|
||||
const dsproofAttachStub = sandbox.stub(DSProofRouter.prototype, 'attach')
|
||||
const encryptionAttachStub = sandbox.stub(EncryptionRouter.prototype, 'attach')
|
||||
const fulcrumAttachStub = sandbox.stub(FulcrumRouter.prototype, 'attach')
|
||||
const miningAttachStub = sandbox.stub(MiningRouter.prototype, 'attach')
|
||||
const priceAttachStub = sandbox.stub(PriceRouter.prototype, 'attach')
|
||||
@@ -148,6 +153,8 @@ describe('#controllers/rest-api/index.js', () => {
|
||||
assert.equal(controlAttachStub.getCall(0).args[0], app)
|
||||
assert.isTrue(dsproofAttachStub.calledOnce)
|
||||
assert.equal(dsproofAttachStub.getCall(0).args[0], app)
|
||||
assert.isTrue(encryptionAttachStub.calledOnce)
|
||||
assert.equal(encryptionAttachStub.getCall(0).args[0], app)
|
||||
assert.isTrue(fulcrumAttachStub.calledOnce)
|
||||
assert.equal(fulcrumAttachStub.getCall(0).args[0], app)
|
||||
assert.isTrue(miningAttachStub.calledOnce)
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
Unit tests for EncryptionUseCases.
|
||||
*/
|
||||
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import EncryptionUseCases from '../../../src/use-cases/encryption-use-cases.js'
|
||||
|
||||
describe('#encryption-use-cases.js', () => {
|
||||
let sandbox
|
||||
let mockAdapters
|
||||
let mockUseCases
|
||||
let mockBchjs
|
||||
let uut
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox()
|
||||
mockAdapters = {}
|
||||
|
||||
// Mock bchjs
|
||||
mockBchjs = {
|
||||
Address: {
|
||||
toCashAddress: sandbox.stub().returns('bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf')
|
||||
},
|
||||
ECPair: {
|
||||
fromPublicKey: sandbox.stub().returns({}),
|
||||
toCashAddress: sandbox.stub().returns('bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf')
|
||||
}
|
||||
}
|
||||
|
||||
// Mock use cases
|
||||
mockUseCases = {
|
||||
fulcrum: {
|
||||
getTransactions: sandbox.stub().resolves({
|
||||
transactions: [
|
||||
{ tx_hash: 'abc123def456' }
|
||||
]
|
||||
})
|
||||
},
|
||||
rawtransactions: {
|
||||
getRawTransaction: sandbox.stub().resolves({
|
||||
vin: [
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'signature 02abc123def456789'
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
uut = new EncryptionUseCases({
|
||||
adapters: mockAdapters,
|
||||
useCases: mockUseCases,
|
||||
bchjs: mockBchjs
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore()
|
||||
})
|
||||
|
||||
describe('#constructor()', () => {
|
||||
it('should require adapters', () => {
|
||||
assert.throws(() => {
|
||||
// eslint-disable-next-line no-new
|
||||
new EncryptionUseCases({ useCases: mockUseCases })
|
||||
}, /Adapters instance required/)
|
||||
})
|
||||
|
||||
it('should require useCases', () => {
|
||||
assert.throws(() => {
|
||||
// eslint-disable-next-line no-new
|
||||
new EncryptionUseCases({ adapters: mockAdapters })
|
||||
}, /UseCases instance required/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getPublicKey()', () => {
|
||||
it('should return public key when found', async () => {
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isTrue(result.success)
|
||||
assert.equal(result.publicKey, '02abc123def456789')
|
||||
assert.isTrue(mockBchjs.Address.toCashAddress.calledOnce)
|
||||
assert.isTrue(mockUseCases.fulcrum.getTransactions.calledOnce)
|
||||
assert.isTrue(mockUseCases.rawtransactions.getRawTransaction.calledOnce)
|
||||
})
|
||||
|
||||
it('should return not found when public key does not match', async () => {
|
||||
// Make the ECPair.toCashAddress return a different address
|
||||
mockBchjs.ECPair.toCashAddress.returns('bitcoincash:qqq000000000000000000000000000000000000000')
|
||||
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isFalse(result.success)
|
||||
assert.equal(result.publicKey, 'not found')
|
||||
})
|
||||
|
||||
it('should throw error when no transaction history', async () => {
|
||||
mockUseCases.fulcrum.getTransactions.resolves({
|
||||
transactions: []
|
||||
})
|
||||
|
||||
try {
|
||||
await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
assert.fail('Should have thrown an error')
|
||||
} catch (err) {
|
||||
assert.equal(err.message, 'No transaction history.')
|
||||
}
|
||||
})
|
||||
|
||||
it('should handle transactions without scriptSig', async () => {
|
||||
mockUseCases.rawtransactions.getRawTransaction.resolves({
|
||||
vin: [
|
||||
{ txid: 'coinbase' } // No scriptSig
|
||||
]
|
||||
})
|
||||
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isFalse(result.success)
|
||||
assert.equal(result.publicKey, 'not found')
|
||||
})
|
||||
|
||||
it('should handle invalid public key hex gracefully', async () => {
|
||||
mockUseCases.rawtransactions.getRawTransaction.resolves({
|
||||
vin: [
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'signature NOT_VALID_HEX'
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isFalse(result.success)
|
||||
assert.equal(result.publicKey, 'not found')
|
||||
})
|
||||
|
||||
it('should handle ECPair.fromPublicKey throwing error', async () => {
|
||||
mockBchjs.ECPair.fromPublicKey.throws(new Error('Invalid public key'))
|
||||
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isFalse(result.success)
|
||||
assert.equal(result.publicKey, 'not found')
|
||||
})
|
||||
|
||||
it('should search through multiple transactions', async () => {
|
||||
// First transaction has no matching public key
|
||||
mockUseCases.fulcrum.getTransactions.resolves({
|
||||
transactions: [
|
||||
{ tx_hash: 'tx1' },
|
||||
{ tx_hash: 'tx2' }
|
||||
]
|
||||
})
|
||||
|
||||
// Return different data for each tx - first tx has input with non-matching key
|
||||
mockUseCases.rawtransactions.getRawTransaction
|
||||
.onFirstCall().resolves({
|
||||
vin: [
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'sig 02aaa111bbb222ccc'
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
.onSecondCall().resolves({
|
||||
vin: [
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'sig 02abc123def456789'
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// First tx doesn't match, second tx matches
|
||||
mockBchjs.ECPair.toCashAddress
|
||||
.onFirstCall().returns('bitcoincash:qqq000000000000000000000000000000000000000')
|
||||
.onSecondCall().returns('bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf')
|
||||
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isTrue(result.success)
|
||||
assert.equal(result.publicKey, '02abc123def456789')
|
||||
assert.equal(mockUseCases.rawtransactions.getRawTransaction.callCount, 2)
|
||||
})
|
||||
|
||||
it('should search through multiple inputs in a transaction', async () => {
|
||||
mockUseCases.rawtransactions.getRawTransaction.resolves({
|
||||
vin: [
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'sig 02aaa111bbb222ccc'
|
||||
}
|
||||
},
|
||||
{
|
||||
scriptSig: {
|
||||
asm: 'sig 02abc123def456789'
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// First input doesn't match, second input matches
|
||||
mockBchjs.ECPair.toCashAddress
|
||||
.onFirstCall().returns('bitcoincash:qqq000000000000000000000000000000000000000')
|
||||
.onSecondCall().returns('bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf')
|
||||
|
||||
const result = await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
|
||||
assert.isTrue(result.success)
|
||||
assert.equal(result.publicKey, '02abc123def456789')
|
||||
})
|
||||
|
||||
it('should propagate fulcrum errors', async () => {
|
||||
const error = new Error('Fulcrum API error')
|
||||
mockUseCases.fulcrum.getTransactions.rejects(error)
|
||||
|
||||
try {
|
||||
await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
assert.fail('Should have thrown an error')
|
||||
} catch (err) {
|
||||
assert.equal(err.message, 'Fulcrum API error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should propagate rawtransactions errors', async () => {
|
||||
const error = new Error('RawTransactions API error')
|
||||
mockUseCases.rawtransactions.getRawTransaction.rejects(error)
|
||||
|
||||
try {
|
||||
await uut.getPublicKey({ address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf' })
|
||||
assert.fail('Should have thrown an error')
|
||||
} catch (err) {
|
||||
assert.equal(err.message, 'RawTransactions API error')
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user