Skip to content

Commit

Permalink
Updated proto and libs
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneethGunas committed May 19, 2022
1 parent 063fca0 commit b1389bc
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 47 deletions.
13 changes: 0 additions & 13 deletions cbor.js

This file was deleted.

3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// pre-requsit for react-native
require('cbor-rn-prereqs');

import { CKTapCard } from './protocol';

export { CKTapCard };
1 change: 0 additions & 1 deletion nfc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function send(cmd, args = {}) {
const bytes = cborEncode(args);
const r = await NfcManager.isoDepHandler.transceive(bytes);
const { response, status } = decodeAndSplitResponse(r);
// await NfcManager.cancelTechnologyRequest();
return { response, status };
} catch (ex) {
console.log(ex);
Expand Down
5 changes: 2 additions & 3 deletions nfc/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const CBOR = require('cbor');
var Buffer = require('@craftzdog/react-native-buffer').Buffer;
const CBOR = require('@ellipticoin/cbor');

// transceive data format: CLA, INS, P1, P2, Data Len, Data (bytes array)
export const cborEncode = (obj) => {
Expand All @@ -10,7 +9,7 @@ export const cborEncode = (obj) => {

export const decodeAndSplitResponse = (r) => {
return {
response: CBOR.decode(Buffer.from(r.slice(0, r.length - 2))),
response: CBOR.decode(Buffer.from(r)),
status: bytesToHex(Buffer.from(r.slice(r.length - 2))),
};
};
Expand Down
29 changes: 16 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"author": "Praneeth G",
"license": "MIT",
"dependencies": {
"@ellipticoin/cbor": "^1.0.4",
"bech32": "^2.0.0",
"bip32": "^2.0.6",
"bs58": "^5.0.0",
"buf-b32": "^2.2.0",
"buffer-xor": "^2.0.2",
"cbor": "^8.1.0",
"ecpair": "^2.0.1",
"js-sha256": "^0.9.0",
"ripemd160": "^2.0.2",
Expand Down
20 changes: 6 additions & 14 deletions protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import base58 from 'bs58';

const { randomBytes } = require('crypto');

//TODO: will update after nfc integration
async function _send(cmd, args = {}) {
const { status: stat_word, response: resp } = await transceive(cmd, args);
return { stat_word, resp };
}

export class CKTapCard {
constructor() {
this.card_nonce = null;
Expand All @@ -38,6 +36,7 @@ export class CKTapCard {
this.auth_delay = null;
this.is_tapsigner = null;
this.path = null;
this.num_backups = null;
}

async initialise() {
Expand Down Expand Up @@ -82,7 +81,6 @@ export class CKTapCard {
const code = resp['code'] || 500;
throw new Error(`${code} on ${cmd}: ${msg}`);
}

return resp;
}

Expand Down Expand Up @@ -111,6 +109,7 @@ export class CKTapCard {
this.auth_delay = resp['auth_delay'] || 0;
this.is_tapsigner = resp['tapsigner'] || false;
this.path = resp['path'] ? path2str(resp['path']) : null;
this.num_backups = resp['num_backups'] || 0;
const { active_slot, num_slots } = resp['slots'] || {
active_slot: 0,
num_slots: 1,
Expand Down Expand Up @@ -144,10 +143,7 @@ export class CKTapCard {
if (cmd === 'sign') {
args.digest = xor_bytes(args.digest, session_key);
} else if (cmd === 'change') {
args.data = xor_bytes(
Buffer.from(args.data),
session_key.slice(0, args.data.length)
);
args.data = xor_bytes(args.data, session_key.slice(0, args.data.length));
}
const resp = await this.send(cmd, args);
return { session_key, resp };
Expand Down Expand Up @@ -278,7 +274,7 @@ export class CKTapCard {
master: true,
});
const xpub = resp['xpub'];
// python: return hash160(xpub[-33:])[0:4]
// TODO: check hash160
return hash160(xpub.slice(-33)).slice(0, 4);
}

Expand Down Expand Up @@ -314,7 +310,7 @@ export class CKTapCard {
console.warn('CVC must be 6..32 characters long');
return;
}
await this.send_auth('change', old_cvc, { data: force_bytes(new_cvc) });
return this.send_auth('change', old_cvc, { data: force_bytes(new_cvc) });
}

async certificate_check() {
Expand Down Expand Up @@ -556,11 +552,7 @@ export class CKTapCard {
}

async wait() {
for (var i = 0; i < 15; i++) {
try {
await this.send_auth('wait');
} catch (e) {}
}
return this.send('wait');
}

async read(cvc) {
Expand Down
Empty file added test.js
Empty file.
7 changes: 5 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function xor_bytes(a, b) {
console.warn('Length mismatch: Expected same lengths at xor_bytes');
return;
}
return xor(a, b);
return Buffer.from(xor(a, b));
}

function pick_nonce() {
Expand Down Expand Up @@ -197,7 +197,10 @@ function recover_pubkey(status_resp, read_resp, my_nonce, ses_key) {

// have to decrypt pubkey
let pubkey = read_resp['pubkey'];
pubkey = pubkey.sloce(0, 1) + xor_bytes(pubkey.sloce(1), ses_key);
pubkey = Buffer.concat([
pubkey.slice(0, 1),
xor_bytes(pubkey.sloce(1), ses_key),
]);

// Critical: proves card knows key
// TODO: implement sha256 everywhere
Expand Down

0 comments on commit b1389bc

Please sign in to comment.