Skip to content
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
6 changes: 5 additions & 1 deletion modules/express/src/clientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,18 @@ function handleV1Sign(req: express.Request) {
}

export async function handleV2Sign(req: express.Request) {
const walletId = req.body.txPrebuild.walletId;
const path = req.config.signerFileSystemPath;
assert(typeof path === 'string');
const privKeyFile = await fs.readFile(path, { encoding: 'utf8' });
const privKey = JSON.parse(privKeyFile);
if (privKey[walletId] === undefined) {
throw new Error(`Could not find a field for walletId: ${walletId} in ${req.config.signerFileSystemPath}`);
}
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
try {
return await coin.signTransaction({ ...req.body, ...privKey });
return await coin.signTransaction({ ...req.body, ...{ prv: privKey[walletId] } });
} catch (error) {
console.log('error while signing wallet transaction ', error);
throw error;
Expand Down
5 changes: 1 addition & 4 deletions modules/express/src/expressApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ export function createBaseUri(config: Config): string {
function checkSignerPrvPath(path: string) {
try {
const privKeyFile = fs.readFileSync(path, { encoding: 'utf8' });
const privKey = JSON.parse(privKeyFile);
if (privKey.prv === undefined) {
throw new Error(`required field "prv" is missing`);
}
JSON.parse(privKeyFile);
} catch (e) {
throw new Error(`Failed to parse ${path} - ${e.message}`);
}
Expand Down
9 changes: 4 additions & 5 deletions modules/express/test/unit/bitgoExpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Bitgo Express', function () {

describe('server initialization', function () {
const validPrvJSON =
'{"prv":"xprv9s21ZrQH143K3EuPWCBuqnWxydaQV6et9htQige4EswvcHKEzNmkVmwTwKoadyHzJYppuADB7Us7AbaNLToNvoFoSxuWqndQRYtnNy5DUY2"}';
'{"61f039aad587c2000745c687373e0fa9":"xprv9s21ZrQH143K3EuPWCBuqnWxydaQV6et9htQige4EswvcHKEzNmkVmwTwKoadyHzJYppuADB7Us7AbaNLToNvoFoSxuWqndQRYtnNy5DUY2"}';

it('should require NODE_ENV to be production when running against prod env', function () {
const envStub = sinon.stub(process, 'env').value({ NODE_ENV: 'production' });
Expand Down Expand Up @@ -447,18 +447,17 @@ describe('Bitgo Express', function () {
(() => expressApp(args)).should.not.throw();
});

it('should require that an signerFileSystemPath contains a json with a prv field', function () {
it('should require that an signerFileSystemPath contains a parsable json', function () {
const args: any = {
env: 'test',
signerMode: 'signerMode',
signerFileSystemPath: 'invalidSignerFileSystemPath',
};
(() => expressApp(args)).should.throw();

const invalidPrv =
'{"invalidField":"invalidPrivKey"}';
const invalidPrv = '{"invalid json"}';
const readInvalidStub = sinon.stub(fs, 'readFileSync').returns(invalidPrv);
(() => expressApp(args)).should.throw(`Failed to parse ${args.signerFileSystemPath} - required field "prv" is missing`);
(() => expressApp(args)).should.throw();
readInvalidStub.restore();

const readValidStub = sinon.stub(fs, 'readFileSync').returns(validPrvJSON);
Expand Down
7 changes: 6 additions & 1 deletion modules/express/test/unit/clientRoutes/externalSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import { BitGo } from 'bitgo';
describe('External signer', () => {
it('should read prv from signerFileSystemPath and pass it to coin.signTransaction', async () => {
const validPrv =
'{"prv":"xprv9s21ZrQH143K3EuPWCBuqnWxydaQV6et9htQige4EswvcHKEzNmkVmwTwKoadyHzJYppuADB7Us7AbaNLToNvoFoSxuWqndQRYtnNy5DUY2"}';
'{"61f039aad587c2000745c687373e0fa9":"xprv9s21ZrQH143K3EuPWCBuqnWxydaQV6et9htQige4EswvcHKEzNmkVmwTwKoadyHzJYppuADB7Us7AbaNLToNvoFoSxuWqndQRYtnNy5DUY2"}';
const readFileStub = sinon.stub(fs.promises, 'readFile').resolves(validPrv);
const signTransactionStub = sinon.stub(Btc.prototype, 'signTransaction').resolves('signedTx');

const req = {
bitgo: new BitGo({ env: 'test' }),
body: {
txPrebuild: {
walletId: '61f039aad587c2000745c687373e0fa9',
},
},
params: {
coin: 'tbtc',
},
Expand Down