Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support new API #2

Merged
merged 5 commits into from
Mar 26, 2019
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ledger-cosmos-js",
"version": "0.0.1",
"description": "Node API for Ledger Nano S - Tendermint/Cosmos App",
"version": "1.0.0",
"description": "Node API for Ledger Nano S Cosmos App",
"main": "./src/index.js",
"repository": {
"type": "git",
Expand Down
74 changes: 74 additions & 0 deletions src/ledger-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const INS_GET_VERSION = 0x00;
const INS_PUBLIC_KEY_SECP256K1 = 0x01;
jleni marked this conversation as resolved.
Show resolved Hide resolved
const INS_SIGN_SECP256K1 = 0x02;
const INS_SHOW_ADDR_SECP256K1 = 0x03;
const INS_GET_ADDR_SECP256K1 = 0x04;

const CHUNK_SIZE = 250;

Expand Down Expand Up @@ -138,6 +139,7 @@ LedgerApp.prototype.get_version = function () {
result["major"] = apduResponse[1];
result["minor"] = apduResponse[2];
result["patch"] = apduResponse[3];
result["device_locked"] = apduResponse[4] === 1;

result["return_code"] = error_code_data[0] * 256 + error_code_data[1];
result["error_message"] = errorMessage(result["return_code"]);
Expand Down Expand Up @@ -307,4 +309,76 @@ LedgerApp.prototype.showAddress = function (hrp, path) {
process_error_response);
};

LedgerApp.prototype.getAddressAndPubKey = function (hrp, path) {
let data = Buffer.concat([serialize_hrp(hrp), serialize_path(path)]);
let buffer = serialize(CLA, INS_GET_ADDR_SECP256K1, 0, 0, data);

return this.comm.exchange(buffer.toString('hex'), [0x9000, 0x6A80]).then(
function (apduResponse) {
let result = {};
apduResponse = Buffer.from(apduResponse, 'hex');
let error_code_data = apduResponse.slice(-2);

result["return_code"] = error_code_data[0] * 256 + error_code_data[1];
result["error_message"] = errorMessage(result["return_code"]);

if (result.return_code !== 0x9000) {
return
}

result["compressed_pk"] = Buffer.from(apduResponse.slice(0, 33));
result["bech32_address"] = Buffer.from(apduResponse.slice(33, -2)).toString();

return result;
},
process_error_response);
};

LedgerApp.prototype.appInfo = function () {
let buffer = serialize(0xB0, 0x01, 0, 0);

return this.comm.exchange(buffer.toString('hex'), [0x9000, 0x6A80]).then(
function (apduResponse) {
let result = {};
apduResponse = Buffer.from(apduResponse, 'hex');
let error_code_data = apduResponse.slice(-2);

result["return_code"] = error_code_data[0] * 256 + error_code_data[1];
result["error_message"] = errorMessage(result["return_code"]);

if (result.return_code !== 0x9000) {
return
}

if (apduResponse[0] !== 1) {
// Ledger responds with format ID 1. There is no spec for any format != 1
result["error_message"] = "response format ID not recognized";
return result
}

const appNameLen = apduResponse[1];
result["appName"] = apduResponse.slice(2, 2+appNameLen).toString('ascii');

var idx = 2+appNameLen;
const appVersionLen=apduResponse[idx];

idx++;
result["appVersion"] = apduResponse.slice(idx, idx+appVersionLen).toString('ascii');
idx+=appVersionLen;

const appFlagsLen=apduResponse[idx];
idx++;
result["flagsLen"] = appFlagsLen;
result["flagsValue"] = apduResponse[idx];

result["flag_recovery"] = (result["flagsValue"] & 1) !== 0;
result["flag_signed_mcu_code"] = (result["flagsValue"] & 2) !== 0;
result["flag_onboarded"] = (result["flagsValue"] & 4) !== 0;
result["flag_pin_validated"] = (result["flagsValue"] & 128) !== 0;

return result;
},
process_error_response);
};

module.exports = LedgerApp;
102 changes: 78 additions & 24 deletions tests/tests-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ ledger = require('../src');
comm = ledger.comm_node;
browser = false;

const TIMEOUT = 1000;
const LONG_TIMEOUT = 45000;
const EXPECTED_MAJOR = 1;
const EXPECTED_MINOR = 1;
const EXPECTED_PATCH = 0;
const Timeout = 1000;
const TimeoutLong = 45000;
const ExpectedVersionMajor = 1;
const ExpectedVersionMinor = 3;
const ExpectedVersionPatch = 1;

describe('get_version', function () {
jleni marked this conversation as resolved.
Show resolved Hide resolved
let response;
// call API
before(function () {
return comm.create_async(TIMEOUT, true).then(
return comm.create_async(Timeout, true).then(
function (comm) {
let app = new ledger.App(comm);
return app.get_version().then(function (result) {
Expand All @@ -46,6 +46,7 @@ describe('get_version', function () {
});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
it('has property test_mode', function () {
Expand All @@ -64,18 +65,18 @@ describe('get_version', function () {
expect(response.test_mode).to.be.false;
});
it('app has matching version', function () {
expect(response.major).to.equal(EXPECTED_MAJOR);
expect(response.minor).to.equal(EXPECTED_MINOR);
expect(response.patch).to.equal(EXPECTED_PATCH);
expect(response.major).to.equal(ExpectedVersionMajor);
expect(response.minor).to.equal(ExpectedVersionMinor);
expect(response.patch).to.equal(ExpectedVersionPatch);
});
});

describe('get_pk', function () {
let response;
// call API
before(function () {
this.timeout(LONG_TIMEOUT);
return comm.create_async(LONG_TIMEOUT, true).then(
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
function (comm) {
let app = new ledger.App(comm);

Expand Down Expand Up @@ -164,7 +165,7 @@ describe('sign_get_chunks', function () {

// call API
before(function () {
return comm.create_async(TIMEOUT, true).then(
return comm.create_async(Timeout, true).then(
function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand All @@ -189,7 +190,7 @@ describe('sign_get_chunks_big', function () {

// call API
before(function () {
return comm.create_async(TIMEOUT, true).then(
return comm.create_async(Timeout, true).then(
function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand Down Expand Up @@ -226,7 +227,7 @@ describe('sign_send_chunk', function () {

// call API
before(function () {
return comm.create_async(TIMEOUT, true).then(
return comm.create_async(Timeout, true).then(
function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand All @@ -240,6 +241,7 @@ describe('sign_send_chunk', function () {
});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
});
Expand All @@ -250,8 +252,8 @@ describe('sign', function () {

// call API
before(function () {
this.timeout(LONG_TIMEOUT);
return comm.create_async(LONG_TIMEOUT, true).then(
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
async function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand All @@ -261,6 +263,7 @@ describe('sign', function () {
});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
it('has no errors', function () {
Expand All @@ -278,8 +281,8 @@ describe('sign_and_verify', function () {

// call API
before(function () {
this.timeout(LONG_TIMEOUT);
return comm.create_async(LONG_TIMEOUT, true).then(
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
async function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand Down Expand Up @@ -319,8 +322,8 @@ describe('sign_2', function () {

// call API
before(function () {
this.timeout(LONG_TIMEOUT);
return comm.create_async(LONG_TIMEOUT, true).then(
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
async function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand All @@ -331,6 +334,7 @@ describe('sign_2', function () {
});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
it('has no errors', function () {
Expand All @@ -346,8 +350,8 @@ describe('sign_parsing_error_message', function () {

// call API
before(function () {
this.timeout(LONG_TIMEOUT);
return comm.create_async(LONG_TIMEOUT, true).then(
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
async function (comm) {
let app = new ledger.App(comm);
let path = [44, 118, 0, 0, 0]; // Derivation path. First 3 items are automatically hardened!
Expand All @@ -358,6 +362,7 @@ describe('sign_parsing_error_message', function () {
});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x6A80);
});
it('has no errors', function () {
Expand All @@ -384,8 +389,8 @@ describe('showAddress', function () {
let response;
// call API
before(function () {
this.timeout(LONG_TIMEOUT);
return comm.create_async(LONG_TIMEOUT, true).then(
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
function (comm) {
let app = new ledger.App(comm);

Expand All @@ -398,6 +403,55 @@ describe('showAddress', function () {
});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
});

describe('getAddressAndPubKey', function () {
let response;
// call API
before(function () {
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
function (comm) {
let app = new ledger.App(comm);

let path = [44, 118, 5, 0, 3];
return app.getAddressAndPubKey("cosmos", path).then(function (result) {
response = result;
console.log(response);
});

});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
});

describe('appInfo', function () {
let response;
// call API
before(function () {
this.timeout(TimeoutLong);
return comm.create_async(TimeoutLong, true).then(
function (comm) {
let app = new ledger.App(comm);
return app.appInfo().then(function (result) {
response = result;
console.log(response);
});

});
});
it('return_code is 0x9000', function () {
console.log("Error code 0x%s: %s ", response.return_code.toString(16), response.error_message);
expect(response.return_code).to.equal(0x9000);
});
});

expect(response.return_code).to.equal(0x9000);
});
});