Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
feat trezor connect v8 upgrade (#670)
Browse files Browse the repository at this point in the history
* feat: trezor-connect upgrade to v8, WIP

* fix: voting trezor integration version upgrade & refactor

* fix: trezorAccount serialize account parameter  hdKeypath fix

* fix: voting transactions fee flooring issue fixed

* fix: Trezor T network -104 value should be 152, fixed

* fix: remove nem-trezor dependency since not needed anymore

* fix: trezor result error field fixed

* fix: removed unused parameter receiverPublicKey and the encryption logic

* fix: importance transfer tx trezor compatibility issue fixed

* fix: failing unit tests fixed, trezor unit tests added, pr feedbacks

* fix: trezor account network param passed, trezor spec refactored

Co-authored-by: Baha <[email protected]>
  • Loading branch information
yilmazbahadir and Baha authored Mar 17, 2022
1 parent ea49adf commit 5327de4
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 1,097 deletions.
85 changes: 59 additions & 26 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"author": "https://github.com/QuantumMechanics <[email protected]>",
"license": "MIT",
"dependencies": {
"@ledgerhq/hw-transport-node-hid-noevents": "^5.43.0"
"@ledgerhq/hw-transport-node-hid-noevents": "^5.43.0",
"trezor-connect": "^8.2.7"
},
"devDependencies": {
"@babel/runtime": "^7.4.2",
Expand All @@ -28,7 +29,6 @@
"browserify": "^13.0.0",
"browserify-ngannotate": "^2.0.0",
"bufferutil": "^4.0.1",
"symbol-post-launch-optin-module": "1.0.1",
"chart.js": "2.1.6",
"crypto-js": "^3.1.9-1",
"electron": "11.1.0",
Expand All @@ -52,7 +52,6 @@
"merge-stream": "^1.0.0",
"nem-library": "1.0.5",
"nem-sdk": "^1.6.8",
"nem-trezor": "1.1.0",
"nem-voting": "^2.2.9",
"ng-toast": "2.0.0",
"ngstorage": "0.3.10",
Expand All @@ -62,6 +61,7 @@
"rxjs": "5.5.12",
"symbol-hd-wallets": "0.12.0",
"symbol-paper-wallets": "1.0.2-optin",
"symbol-post-launch-optin-module": "1.0.1",
"symbol-qr-library": "^0.9.0",
"symbol-sdk": "1.0.0",
"url-parse": "1.1.9",
Expand Down
94 changes: 69 additions & 25 deletions src/app/modules/trezor/trezor.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TransactionTypes } from 'nem-library';
import nem from 'nem-sdk';
import TrezorConnect from 'trezor-connect';

/** Service storing Trezor utility functions. */
class Trezor {
Expand All @@ -18,6 +20,7 @@ class Trezor {
// Service properties region //

// End properties region //
TrezorConnect.manifest({ email: '[email protected]', appUrl: 'https://www.nem.io' });
}

// Service methods region //
Expand All @@ -37,27 +40,34 @@ class Trezor {
return `m/44'/${coinType}'/${index}'/0'/0'`;
}

adjustNetwork(network) {
return network < 0 ? 256 + network : network;
}

createAccount(network, index, label) {
return new Promise((resolve, reject) => {
const hdKeypath = this.bip44(network, index);

TrezorConnect.nemGetAddress(hdKeypath, network, (result) => {
TrezorConnect.nemGetAddress({
path: hdKeypath,
network: this.adjustNetwork(network),
showOnTrezor: true
}).then(function(result) {
if (result.success) {
resolve({
"brain": false,
"algo": "trezor",
"encrypted": "",
"iv": "",
"address": result.address,
"address": result.payload.address,
"label": label,
"network": network,
"child": "",
"hdKeypath": hdKeypath
});
} else {
reject(result.error);
reject(result.payload.error);
}
});
})
});
}

Expand All @@ -66,9 +76,16 @@ class Trezor {
const value = "0000000000000000000000000000000000000000000000000000000000000000";

return new Promise((resolve, reject) => {
TrezorConnect.cipherKeyValue(account.hdKeypath, key, value, true, true, true, (result) => {
TrezorConnect.cipherKeyValue({
path: account.hdKeypath,
key: key,
value: value,
encrypt: true,
askOnEncrypt: true,
askOnDecrypt: true
}).then(function(result) {
if (result.success) {
const privateKey = nem.utils.helpers.fixPrivateKey(result.value);
const privateKey = nem.utils.helpers.fixPrivateKey(result.payload.value);
const keyPair = nem.crypto.keyPair.create(privateKey);
const publicKey = keyPair.publicKey.toString();
const address = nem.model.address.toAddress(publicKey, network);
Expand All @@ -79,38 +96,65 @@ class Trezor {
publicKey
});
} else {
reject(result.error);
reject(result.payload.error);
}
});
})
});
}

/**
* Adjusts importance transfer transaction to be compatible with Trezor
*
* @param {Object} transaction - Transaction to adjust
* @returns the adjusted transaction or the same transaction (if not an importance transfer)
*/
adjustImportanceTransferTransaction(transaction) {
if (transaction.type === TransactionTypes.IMPORTANCE_TRANSFER) {
Object.assign(transaction, {
importanceTransfer: {
mode: transaction.mode,
publicKey: transaction.remoteAccount,
},
});
}
return transaction;
}

serialize(transaction, account) {
const tx = this.adjustImportanceTransferTransaction(transaction);

return new Promise((resolve, reject) => {
TrezorConnect.nemSignTx(account.hdKeypath, transaction, (result) => {
if (result.success) {
resolve(result.message);
} else {
reject({
"code": 0,
"data": {
"message": result.error
}
});
}
});
TrezorConnect.nemSignTransaction({
path: account.hdKeypath,
transaction: tx,
}).then(function (result) {
if (result.success) {
resolve(result.payload);
} else {
reject({
code: 0,
data: {
message: result.payload.error,
},
});
}
});
});
}

showAccount(account) {
return new Promise((resolve, reject) => {
TrezorConnect.nemGetAddress(account.hdKeypath, account.network, (result) => {
TrezorConnect.nemGetAddress({
path: account.hdKeypath,
network: this.adjustNetwork(account.network),
showOnTrezor: true
}).then(function(result) {
if (result.success) {
resolve(result.address);
resolve(result.payload.address);
} else {
reject(result.error);
reject(result.payload.error);
}
});
})
});
}

Expand Down
41 changes: 41 additions & 0 deletions src/app/modules/trezor/trezorAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Observable } from "rxjs";

/**
* TrezorAccount model
*/
export class TrezorAccount {

/**
* Constructor
* @param address
* @param hdKeyPath
* @param network
* @param Trezor trezor service
*/
constructor(address, hdKeyPath, network, Trezor) {
this.address = address;
this.hdKeyPath = hdKeyPath;
this.network = network;
this._Trezor = Trezor;
}

/**
* Sign a transaction
* @param transaction
* @returns {Observable<{data: any, signature: string}>}
*/
signTransaction(transaction) {
transaction.setNetworkType(this.network);
return Observable.fromPromise(this._Trezor.serialize(transaction.toDTO(), { hdKeypath: this.hdKeyPath }));
}

/**
* Sign multiple transactions
*
* @param transactions
* @returns {Observable<any>} Observable of signed transactions
*/
signTransactions(transactions) {
return Observable.merge(...(transactions.map(t => this.signTransaction(t)))).toArray();
}
}
Loading

0 comments on commit 5327de4

Please sign in to comment.