Skip to content

Commit

Permalink
add new GenericApp support (#100)
Browse files Browse the repository at this point in the history
* switch to yarn v3

* remove unused deps and run prettier

* support metadata and remove scheme

* create new class for GenericApp

* create new class for GenericApp

* fix: tests

* feat: add chain ticker, and fix txMetadata fetch process

* feat: add tests for generic app

---------

Co-authored-by: Carlo Sala <[email protected]>
  • Loading branch information
emmanuelm41 and carlosala authored Apr 18, 2024
1 parent 6b1479e commit 38b931f
Show file tree
Hide file tree
Showing 17 changed files with 8,797 additions and 4,324 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
/output/
/dist/

.gitignore
node_modules
.yarn/*
!.yarn/releases
!.yarn/plugins
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ package.json
package-lock.json
dist
node_modules
.yarn
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"printWidth": 120
"printWidth": 120,
"proseWrap": "always"
}
541 changes: 541 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

874 changes: 874 additions & 0 deletions .yarn/releases/yarn-3.6.3.cjs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
logFilters:
- code: YN0013
level: discard

nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-3.6.3.cjs
25 changes: 16 additions & 9 deletions README-npm.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ using the official Substrate Ledger apps in recovery mode.
| getAddress | pubkey + address | path + ( showInDevice ) |
| sign | signed message | path + message |

getAddress command requires that you set the derivation path (account, change, index) and has an option parameter to display the address on the device. By default, it will retrieve the information without confirmation from the user.
getAddress command requires that you set the derivation path (account, change, index) and has an option parameter to
display the address on the device. By default, it will retrieve the information without confirmation from the user.

# Add new chain

If you want to add support for your chain, you just need to create a PR in this repository adding the parameters that belong to the chain.
Go to [supported APPs](./src/supported_apps.ts) and add a new entry at the end of the file.
If you want to add support for your chain, you just need to create a PR in this repository adding the parameters that
belong to the chain. Go to [supported APPs](./src/supported_apps.ts) and add a new entry at the end of the file.

```
{
Expand All @@ -35,15 +36,18 @@ Go to [supported APPs](./src/supported_apps.ts) and add a new entry at the end o
},
```

Take the last used CLA and pick the following number. This is just an ID for the app that is used in APDU protocol. This is probably the easiest way to get a free CLA.
Take the last used CLA and pick the following number. This is just an ID for the app that is used in APDU protocol. This
is probably the easiest way to get a free CLA.

For Slip0044 parameter, you might want to [register here](https://github.com/satoshilabs/slips/blob/master/slip-0044.md) as well.
For Slip0044 parameter, you might want to [register here](https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
as well.

SS58 prefix have no limitation whatsoever, you just have to set an uint16 number that is used in your chain.

# Testing with real devices

It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps:
It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these
steps:

- Install the application in the Ledger device
- Install the dependencies from this project
Expand All @@ -56,9 +60,11 @@ yarn test

## Example:

Visit and download the [latest release](https://github.com/Zondax/ledger-kusama/releases/latest) from repository (in this case Kusama).
Visit and download the [latest release](https://github.com/Zondax/ledger-kusama/releases/latest) from repository (in
this case Kusama).

Download the installer script for your device but bear in mind that NanoX does not allow side loading applications. Give execution permission and run the script.
Download the installer script for your device but bear in mind that NanoX does not allow side loading applications. Give
execution permission and run the script.

```shell script
chmod +x installer_nano_device.sh
Expand All @@ -78,4 +84,5 @@ Run tests and you will see how this module communicates with your device.

# Who we are?

We are Zondax, a company pioneering blockchain services. If you want to know more about us, please visit us at [zondax.ch](https://zondax.ch)
We are Zondax, a company pioneering blockchain services. If you want to know more about us, please visit us at
[zondax.ch](https://zondax.ch)
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# ledger-substrate (JS Integration)

![zondax_light](docs/zondax_light.png#gh-light-mode-only)
![zondax_dark](docs/zondax_dark.png#gh-dark-mode-only)
![zondax_light](docs/zondax_light.png#gh-light-mode-only) ![zondax_dark](docs/zondax_dark.png#gh-dark-mode-only)

[![Main](https://github.com/Zondax/ledger-substrate-js/workflows/Main/badge.svg)](https://github.com/Zondax/ledger-substrate-js/actions?query=workflow%3AMain)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand All @@ -20,12 +19,13 @@ using the official Substrate Ledger apps in recovery mode.
| getAddress | pubkey + address | path + ( showInDevice ) |
| sign | signed message | path + message |

getAddress command requires that you set the derivation path (account, change, index) and has an option parameter to display the address on the device. By default, it will retrieve the information without confirmation from the user.
getAddress command requires that you set the derivation path (account, change, index) and has an option parameter to
display the address on the device. By default, it will retrieve the information without confirmation from the user.

# Add new chain

If you want to add support for your chain, you just need to create a PR in this repository adding the parameters that belong to the chain.
Go to [supported APPs](./src/supported_apps.ts) and add a new entry at the end of the file.
If you want to add support for your chain, you just need to create a PR in this repository adding the parameters that
belong to the chain. Go to [supported APPs](./src/supported_apps.ts) and add a new entry at the end of the file.

```
{
Expand All @@ -36,15 +36,18 @@ Go to [supported APPs](./src/supported_apps.ts) and add a new entry at the end o
},
```

Take the last used CLA and pick the following number. This is just an ID for the app that is used in APDU protocol. This is probably the easiest way to get a free CLA.
Take the last used CLA and pick the following number. This is just an ID for the app that is used in APDU protocol. This
is probably the easiest way to get a free CLA.

For Slip0044 parameter, you might want to [register here](https://github.com/satoshilabs/slips/blob/master/slip-0044.md) as well.
For Slip0044 parameter, you might want to [register here](https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
as well.

SS58 prefix have no limitation whatsoever, you just have to set an uint16 number that is used in your chain.

# Testing with real devices

It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps:
It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these
steps:

- Install the application in the Ledger device
- Install the dependencies from this project
Expand All @@ -57,9 +60,11 @@ yarn test

## Example:

Visit and download the [latest release](https://github.com/Zondax/ledger-kusama/releases/latest) from repository (in this case Kusama).
Visit and download the [latest release](https://github.com/Zondax/ledger-kusama/releases/latest) from repository (in
this case Kusama).

Download the installer script for your device but bear in mind that NanoX does not allow side loading applications. Give execution permission and run the script.
Download the installer script for your device but bear in mind that NanoX does not allow side loading applications. Give
execution permission and run the script.

```shell script
chmod +x installer_nano_device.sh
Expand All @@ -79,4 +84,5 @@ Run tests and you will see how this module communicates with your device.

# Who we are?

We are Zondax, a company pioneering blockchain services. If you want to know more about us, please visit us at [zondax.ch](https://zondax.ch)
We are Zondax, a company pioneering blockchain services. If you want to know more about us, please visit us at
[zondax.ch](https://zondax.ch)
68 changes: 32 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"license": "Apache-2.0",
"version": "0.35.2",
"description": "TS / Node API for Substrate based apps running on Ledger Nano S/S+/X",
"bin": "./dist/cmd/cli.js",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"types": "./dist/index.d.ts",
Expand All @@ -23,37 +22,40 @@
"url": "https://github.com/Zondax/ledger-substrate-js/issues"
},
"homepage": "https://github.com/Zondax/ledger-substrate-js",
"dependencies": {
"@ledgerhq/hw-transport": "^6.27.1",
"scripts": {
"build": "tsc",
"test:integration": "yarn build && jest -t 'Integration'",
"test:key-derivation": "yarn build && jest -t 'KeyDerivation'",
"linter": "eslint --max-warnings 0 src/**/*",
"linter:fix": "yarn run linter --fix",
"format": "prettier --write ."
},
"devDependencies": {
"@ledgerhq/hw-transport": "^6.28.8",
"@ledgerhq/hw-transport-node-hid": "^6.27.21",
"@swc/core": "^1.3.85",
"@types/jest": "^29.5.4",
"@types/node": "^20.6.1",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"bip32": "^4.0.0",
"bip32-ed25519": "https://github.com/Zondax/bip32-ed25519",
"bip39": "^3.0.4",
"bip39": "^3.1.0",
"blakejs": "^1.2.1",
"bs58": "^5.0.0",
"hash.js": "^1.1.7"
},
"devDependencies": {
"@ledgerhq/hw-transport-node-hid": "^6.27.13",
"@swc/core": "^1.2.237",
"@types/jest": "^29.5.2",
"@types/node": "^18.7.6",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"copyfiles": "^2.4.1",
"ed25519-supercop": "^2.0.1",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard-with-typescript": "^34.0.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.6.0",
"eslint": "^8.49.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard-with-typescript": "^39.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^16.1.0",
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.5.0",
"jest-runner": "^29.5.0",
"jest-serial-runner": "^1.2.0",
"prettier": "^2.7.1",
"ts-jest": "29.1.0",
"ts-node": "^10.8.1",
"typescript": "5.0.4"
"jest": "^29.7.0",
"jest-runner": "^29.7.0",
"jest-serial-runner": "^1.2.1",
"prettier": "^3.0.3",
"ts-jest": "29.1.1",
"ts-node": "^10.9.1",
"typescript": "5.2.2"
},
"moduleDirectories": [
"node_modules",
Expand All @@ -67,14 +69,8 @@
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc && yarn copy-files",
"copy-files": "copyfiles -u 0 src/**/*.proto dist/",
"test:integration": "yarn build && jest -t 'Integration'",
"test:key-derivation": "yarn build && jest -t 'KeyDerivation'",
"supported": "ts-node src/cmd/cli.ts supported",
"linter": "eslint . --max-warnings 0",
"linter:fix": "yarn linter --fix",
"format": "prettier --write ."
"packageManager": "[email protected]",
"dependencies": {
"axios": "^1.6.8"
}
}
68 changes: 59 additions & 9 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ export const enum P1_VALUES {
SHOW_ADDRESS_IN_DEVICE = 0x01,
}

export const enum SCHEME {
ED25519 = 0x00,
SR25519 = 0x01,
}

export const enum ERROR_CODE {
NoError = 0x9000,
InvalidData = 0x6984,
}

export const ERROR_DESCRIPTION: Record<number, string> = {
Expand Down Expand Up @@ -154,15 +150,16 @@ export function processErrorResponse(response: any) {
}

export async function getVersion(transport: Transport, cla: number) {
return await transport.send(cla, INS.GET_VERSION, 0, 0).then((response) => {
try {
const response = await transport.send(cla, INS.GET_VERSION, 0, 0);
const errorCodeData = response.subarray(-2);
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];

// 12 bytes + 2 error code
if (response.length !== 14) {
return {
return_code: 0x6984,
error_message: errorCodeToString(0x6984),
return_code: ERROR_CODE.InvalidData,
error_message: errorCodeToString(ERROR_CODE.InvalidData),
};
}

Expand All @@ -184,5 +181,58 @@ export async function getVersion(transport: Transport, cla: number) {
deviceLocked,
target_id: targetId.toString(16),
};
}, processErrorResponse);
} catch (e) {
return processErrorResponse(e);
}
}

export function serializePath(slip0044: number, account: number, change: number, addressIndex: number) {
if (!Number.isInteger(account)) throw new Error("Input must be an integer");
if (!Number.isInteger(change)) throw new Error("Input must be an integer");
if (!Number.isInteger(addressIndex)) throw new Error("Input must be an integer");

const buf = Buffer.alloc(20);
buf.writeUInt32LE(0x8000002c, 0);
buf.writeUInt32LE(slip0044, 4);
buf.writeUInt32LE(account, 8);
buf.writeUInt32LE(change, 12);
buf.writeUInt32LE(addressIndex, 16);
return buf;
}

export function splitBufferToChunks(message: Buffer, chunkSize: number) {
const chunks = [];
const buffer = Buffer.from(message);

for (let i = 0; i < buffer.length; i += chunkSize) {
let end = i + chunkSize;
if (i > buffer.length) {
end = buffer.length;
}
chunks.push(buffer.subarray(i, end));
}

return chunks;
}

export function getSignReqChunks(
slip0044: number,
account: number,
change: number,
addressIndex: number,
blob: Buffer,
metadata?: Buffer,
) {
const chunks: Buffer[] = [];
const bip44Path = serializePath(slip0044, account, change, addressIndex);

const blobLen = Buffer.alloc(2);
blobLen.writeUInt16LE(blob.length);

chunks.push(Buffer.concat([bip44Path, blobLen]));

if (metadata == null) chunks.push(...splitBufferToChunks(blob, CHUNK_SIZE));
else chunks.push(...splitBufferToChunks(Buffer.concat([blob, metadata]), CHUNK_SIZE));

return chunks;
}
Loading

0 comments on commit 38b931f

Please sign in to comment.