├── .github └── workflows │ ├── main.yaml │ └── publish.yaml ├── .gitignore ├── .npmignore ├── .prettierrc.json ├── LICENSE ├── README-npm.md ├── README.md ├── certs ├── .gitkeep └── openssl.cnf ├── docs ├── example.png ├── zondax_dark.png └── zondax_light.png ├── eslint.config.js ├── jest.config.js ├── package.json ├── public ├── favicon.ico └── index.html ├── src ├── consts.ts ├── index.ts └── types.ts ├── tests ├── helper.ts └── test_integration.test.ts └── tsconfig.json /.github/workflows/main.yaml: -------------------------------------------------------------------------------- 1 | name: "Main" 2 | on: 3 | - push 4 | 5 | jobs: 6 | configure: 7 | runs-on: ubuntu-latest 8 | outputs: 9 | uid_gid: ${{ steps.get-user.outputs.uid_gid }} 10 | datetime: ${{ steps.get-datetime.outputs.datetime }} 11 | steps: 12 | - id: get-user 13 | run: echo "::set-output name=uid_gid::$(id -u):$(id -g)" 14 | - id: get-datetime 15 | run: echo "::set-output name=datetime::$(date +'%Y%m%d%H%M%S')" 16 | 17 | build: 18 | needs: [configure] 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v2 23 | with: 24 | submodules: true 25 | - name: Install node 26 | uses: actions/setup-node@v2 27 | - name: Install dependencies 28 | run: yarn install 29 | - name: Run linter 30 | run: yarn lint 31 | - name: Run formatter 32 | run: yarn format 33 | - name: Build 34 | run: yarn build 35 | - name: Unit tests 36 | run: yarn test 37 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: 'Publish packages' 2 | 3 | on: 4 | release: 5 | types: 6 | - published 7 | - edited 8 | tags: 9 | - "v[0-9]+(\\.[0-9]+)*" 10 | 11 | jobs: 12 | configure: 13 | runs-on: ubuntu-latest 14 | outputs: 15 | datetime: ${{ steps.get-datetime.outputs.datetime }} 16 | steps: 17 | - id: get-datetime 18 | run: echo "::set-output name=datetime::$(date +'%Y%m%d%H%M%S')" 19 | 20 | publish_npm_package: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@v2 25 | - name: Install node 26 | uses: actions/setup-node@v2 27 | with: 28 | registry-url: 'https://registry.npmjs.org' 29 | scope: '@zondax' 30 | - run: mv README-npm.md README.md 31 | - name: Install yarn 32 | run: npm install -g yarn 33 | - run: yarn install 34 | - run: yarn build 35 | - name: Get latest release version number 36 | id: get_version 37 | uses: battila7/get-version-action@v2 38 | - name: Update tag 39 | run: | 40 | echo Publishing as ${{ steps.get_version.outputs.version }} 41 | npm --allow-same-version --no-git-tag-version version ${{ steps.get_version.outputs.version }} 42 | - name: Publish package 43 | env: 44 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_PUBLISH_AUTO }} 45 | run: | 46 | npm publish 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | \.idea/ 64 | 65 | .vscode 66 | 67 | TODO\.md 68 | 69 | \dist 70 | /certs/cert.pem 71 | /certs/server.cert 72 | /certs/server.key 73 | 74 | /.idea 75 | /node_modules/ 76 | /output/ 77 | /dist/ 78 | 79 | /yarn.lock 80 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 110, 3 | "trailingComma": "all" 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2018 - 2023 Zondax AG 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README-npm.md: -------------------------------------------------------------------------------- 1 | # @zondax/ledger-cosmos-js 2 | 3 | ![zondax](docs/zondax_light.png) 4 | 5 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 6 | [![Main](https://github.com/cosmos/ledger-cosmos-js/workflows/Main/badge.svg)](https://github.com/cosmos/ledger-cosmos-js/actions?query=workflow%3AMain) 7 | [![npm version](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js.svg)](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js) 8 | 9 | This package provides a basic client library to communicate with a Tendermint/Cosmos App running in a Ledger Nano S/S+/X devices 10 | 11 | We recommend using the npmjs package in order to receive updates/fixes. 12 | 13 | # Available commands 14 | 15 | | Operation | Response | Command | 16 | | -------------------- | --------------------------- | --------------------------- | 17 | | getVersion | app version | --------------- | 18 | | publicKey | pubkey | path (legacy command) | 19 | | getAddressAndPubKey | pubkey + address | path + ( showInDevice ) | 20 | | showAddressAndPubKey | signed message | path | 21 | | appInfo | name, version, flags, etc | --------------- | 22 | | deviceInfo | fw and mcu version, id, etc | Only available in dashboard | 23 | | sign | signed message | path + message + (HRP) | 24 | 25 | 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 user confirmation. 26 | 27 | # Testing with real devices 28 | 29 | It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps: 30 | 31 | - Install the application in the Ledger device 32 | - Install the dependencies from this project 33 | - Run tests 34 | 35 | ```shell script 36 | yarn install 37 | yarn test:integration 38 | ``` 39 | 40 | # Who we are? 41 | 42 | 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) 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @zondax/ledger-cosmos-js 2 | 3 | ![zondax_light](docs/zondax_light.png#gh-light-mode-only) 4 | ![zondax_dark](docs/zondax_dark.png#gh-dark-mode-only) 5 | 6 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 7 | [![Main](https://github.com/cosmos/ledger-cosmos-js/workflows/Main/badge.svg)](https://github.com/cosmos/ledger-cosmos-js/actions?query=workflow%3AMain) 8 | [![npm version](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js.svg)](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js) 9 | 10 | This package provides a basic client library to communicate with a Tendermint/Cosmos App running in a Ledger Nano S/S+/X devices 11 | 12 | We recommend using the npmjs package in order to receive updates/fixes. 13 | 14 | # Available commands 15 | 16 | | Operation | Response | Command | 17 | | -------------------- | --------------------------- | --------------------------- | 18 | | getVersion | app version | --------------- | 19 | | publicKey | pubkey | path (legacy command) | 20 | | getAddressAndPubKey | pubkey + address | path + ( showInDevice ) | 21 | | showAddressAndPubKey | signed message | path | 22 | | appInfo | name, version, flags, etc | --------------- | 23 | | deviceInfo | fw and mcu version, id, etc | Only available in dashboard | 24 | | sign | signed message | path + message + (HRP) | 25 | 26 | 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 user confirmation. 27 | 28 | # Testing with real devices 29 | 30 | It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps: 31 | 32 | - Install the application in the Ledger device 33 | - Install the dependencies from this project 34 | - Run tests 35 | 36 | ```shell script 37 | yarn install 38 | yarn test:integration 39 | ``` 40 | 41 | # Who we are? 42 | 43 | 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) 44 | -------------------------------------------------------------------------------- /certs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmos/ledger-cosmos-js/656a0f45de2f2ce2bdcb57e9f6a19dcd1c96131e/certs/.gitkeep -------------------------------------------------------------------------------- /certs/openssl.cnf: -------------------------------------------------------------------------------- 1 | [ req ] 2 | prompt = no 3 | distinguished_name = req_distinguished_name 4 | 5 | [ req_distinguished_name ] 6 | C = CH 7 | ST = TestState 8 | L = TestLocality 9 | O = Zondax 10 | OU = Development 11 | CN = Zondax 12 | emailAddress = info@zondax.ch 13 | -------------------------------------------------------------------------------- /docs/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmos/ledger-cosmos-js/656a0f45de2f2ce2bdcb57e9f6a19dcd1c96131e/docs/example.png -------------------------------------------------------------------------------- /docs/zondax_dark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmos/ledger-cosmos-js/656a0f45de2f2ce2bdcb57e9f6a19dcd1c96131e/docs/zondax_dark.png -------------------------------------------------------------------------------- /docs/zondax_light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmos/ledger-cosmos-js/656a0f45de2f2ce2bdcb57e9f6a19dcd1c96131e/docs/zondax_light.png -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | languageOptions: { 3 | ecmaVersion: "latest", 4 | sourceType: "module", 5 | globals: { 6 | window: "readonly", 7 | document: "readonly", 8 | process: "readonly", 9 | require: "readonly", 10 | }, 11 | parserOptions: { 12 | project: "tsconfig.json", 13 | }, 14 | }, 15 | ignores: ["dist/*", "node_modules/*"], 16 | plugins: { 17 | "unused-imports": require("eslint-plugin-unused-imports"), 18 | "@typescript-eslint": require("@typescript-eslint/eslint-plugin"), 19 | "eslint-plugin-tsdoc": require("eslint-plugin-tsdoc"), 20 | }, 21 | rules: { 22 | curly: "warn", 23 | "prefer-const": "warn", 24 | "no-else-return": "warn", 25 | complexity: ["warn", 1000], 26 | "no-unneeded-ternary": "warn", 27 | "no-alert": "warn", 28 | "no-empty": "warn", 29 | "no-useless-catch": "error", 30 | "require-await": "warn", 31 | "no-continue": "warn", 32 | "no-console": "warn", 33 | "unused-imports/no-unused-imports": "warn", 34 | "no-magic-numbers": "off", 35 | }, 36 | }; 37 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: "ts-jest", 3 | testEnvironment: "node", 4 | }; 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@zondax/ledger-cosmos-js", 3 | "author": "Zondax AG", 4 | "license": "Apache-2.0", 5 | "version": "4.0.0", 6 | "description": "Node API for Cosmos App (Ledger Nano S/S+/X)", 7 | "main": "./dist/index.js", 8 | "typings": "./dist/index.d.ts", 9 | "types": "./dist/index.d.ts", 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/cosmos/ledger-cosmos-js.git" 13 | }, 14 | "keywords": [ 15 | "Ledger", 16 | "Typescript", 17 | "Tendermint", 18 | "Cosmos" 19 | ], 20 | "bugs": { 21 | "url": "https://github.com/cosmos/ledger-cosmos-js/issues" 22 | }, 23 | "homepage": "https://github.com/cosmos/ledger-cosmos-js", 24 | "resolutions": { 25 | "@ledgerhq/hw-transport": "6.27.1", 26 | "@ledgerhq/devices": "6.27.1", 27 | "@ledgerhq/hw-transport-node-hid": "6.27.1", 28 | "@ledgerhq/hw-transport-node-hid-noevents": "6.27.1" 29 | }, 30 | "dependencies": { 31 | "@ledgerhq/hw-transport": "6.30.6", 32 | "@zondax/ledger-js": "^0.10.0", 33 | "bech32": "^2.0.0", 34 | "buffer": "^6.0.3", 35 | "crypto-browserify": "^3.12.0", 36 | "ripemd160": "^2.0.2", 37 | "stream-browserify": "^3.0.0" 38 | }, 39 | "devDependencies": { 40 | "@ledgerhq/hw-transport-node-hid": "6.28.6", 41 | "@ledgerhq/hw-transport-webusb": "6.28.6", 42 | "@ledgerhq/hw-transport-mocker": "^6.28.6", 43 | "@swc/core": "^1.6.1", 44 | "@types/jest": "^29.5.12", 45 | "@types/ledgerhq__hw-transport": "4.21.8", 46 | "@types/node": "^20.14.2", 47 | "@typescript-eslint/eslint-plugin": "^7.13.0", 48 | "@typescript-eslint/parser": "^7.13.0", 49 | "bip32": "4.0.0", 50 | "bip39": "3.1.0", 51 | "copyfiles": "^2.4.1", 52 | "core-js": "^3.37.1", 53 | "crypto-js": "4.2.0", 54 | "eslint": "^9.5.0", 55 | "eslint-config-prettier": "^9.1.0", 56 | "eslint-plugin-import": "^2.29.1", 57 | "eslint-plugin-jest": "^28.6.0", 58 | "eslint-plugin-prettier": "^5.1.3", 59 | "eslint-plugin-unused-imports": "^3.2.0", 60 | "eslint-plugin-tsdoc": "^0.2.17", 61 | "jest": "^29.7.0", 62 | "jest-serial-runner": "^1.2.0", 63 | "prettier": "^3.3.2", 64 | "secp256k1": "^5.0.0", 65 | "ts-jest": "29.1.5", 66 | "ts-node": "^10.9.2", 67 | "typescript": "^5.4.5", 68 | "webpack": "^5.92.0" 69 | }, 70 | "moduleDirectories": [ 71 | "node_modules", 72 | "dist" 73 | ], 74 | "browserslist": [ 75 | "> 1%", 76 | "last 2 versions" 77 | ], 78 | "files": [ 79 | "dist/*", 80 | "LICENSE", 81 | "yarn.lock" 82 | ], 83 | "publishConfig": { 84 | "access": "public" 85 | }, 86 | "peerDependencies": { 87 | "@babel/core": "^7.17.5", 88 | "@types/node": "^17.0.21" 89 | }, 90 | "scripts": { 91 | "build": "tsc", 92 | "lint": "eslint .", 93 | "lint:fix": "eslint --fix .", 94 | "format": "prettier --write .", 95 | "test": "yarn build && jest" 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cosmos/ledger-cosmos-js/656a0f45de2f2ce2bdcb57e9f6a19dcd1c96131e/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | App Integration Example 9 | 10 | 11 | 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/consts.ts: -------------------------------------------------------------------------------- 1 | /** ****************************************************************************** 2 | * (c) 2018 - 2023 Zondax AG 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | ******************************************************************************* */ 16 | export const CLA = 0x55; 17 | 18 | export const enum P1_VALUES { 19 | ONLY_RETRIEVE = 0x00, 20 | SHOW_ADDRESS_IN_DEVICE = 0x01, 21 | } 22 | 23 | export const enum P2_VALUES { 24 | JSON = 0x0, 25 | TEXTUAL = 0x1, 26 | } 27 | 28 | export const PKLEN = 33; 29 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /** ****************************************************************************** 2 | * (c) 2018 - 2023 Zondax AG 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | ******************************************************************************* */ 16 | import { ResponseSign, type ResponseAddress, type ResponsePubkey } from "./types"; 17 | import { CLA, P1_VALUES, P2_VALUES, PKLEN } from "./consts"; 18 | import type Transport from "@ledgerhq/hw-transport"; 19 | import BaseApp, { 20 | HARDENED, 21 | INSGeneric, 22 | LedgerError, 23 | PAYLOAD_TYPE, 24 | ResponseError, 25 | ResponsePayload, 26 | processErrorResponse, 27 | processResponse, 28 | } from "@zondax/ledger-js"; 29 | import { ByteStream } from "@zondax/ledger-js/dist/byteStream"; 30 | 31 | const crypto = require("crypto"); 32 | const bech32 = require("bech32"); 33 | const Ripemd160 = require("ripemd160"); 34 | 35 | export default class CosmosApp extends BaseApp { 36 | static _INS = { 37 | GET_VERSION: 0x00 as number, 38 | SIGN_SECP256K1: 0x02 as number, 39 | GET_ADDR_SECP256K1: 0x04 as number, 40 | }; 41 | 42 | static _params = { 43 | cla: CLA, 44 | ins: { ...CosmosApp._INS } as INSGeneric, 45 | p1Values: { ONLY_RETRIEVE: 0x00 as 0, SHOW_ADDRESS_IN_DEVICE: 0x01 as 1 }, 46 | chunkSize: 250, 47 | requiredPathLengths: [5], 48 | }; 49 | 50 | /** 51 | * Constructs a new CosmosApp instance. 52 | * @param transport - The transport instance. 53 | * @throws {Error} - If the transport is not defined. 54 | */ 55 | constructor(transport: Transport) { 56 | super(transport, CosmosApp._params); 57 | 58 | if (!this.transport) { 59 | throw new Error("Transport has not been defined"); 60 | } 61 | } 62 | 63 | private serializeHRP(hrp: string) { 64 | if (hrp == null || hrp.length < 3 || hrp.length > 83) { 65 | throw new Error("Invalid HRP"); 66 | } 67 | 68 | const buf = new ByteStream(); 69 | buf.appendUint8(hrp.length); 70 | buf.appendBytes(Buffer.from(hrp)); 71 | 72 | return buf.getCompleteBuffer(); 73 | } 74 | 75 | private serializeCosmosPath(path: string) { 76 | if (typeof path !== "string") { 77 | // NOTE: this is probably unnecessary 78 | throw new ResponseError( 79 | LedgerError.GenericError, 80 | "Path should be a string (e.g \"m/44'/461'/5'/0/3\")", 81 | ); 82 | } 83 | 84 | if (!path.startsWith("m/")) { 85 | throw new ResponseError( 86 | LedgerError.GenericError, 87 | 'Path should start with "m/" (e.g "m/44\'/461\'/5\'/0/3")', 88 | ); 89 | } 90 | 91 | const pathArray = path.split("/"); 92 | pathArray.shift(); // remove "m" 93 | 94 | if ( 95 | this.REQUIRED_PATH_LENGTHS && 96 | this.REQUIRED_PATH_LENGTHS.length > 0 && 97 | !this.REQUIRED_PATH_LENGTHS.includes(pathArray.length) 98 | ) { 99 | throw new ResponseError(LedgerError.GenericError, "Invalid path length. (e.g \"m/44'/5757'/5'/0/3\")"); 100 | } 101 | 102 | const buf = new ByteStream(); 103 | pathArray.forEach((child: string, i: number) => { 104 | let value = 0; 105 | 106 | if (child.endsWith("'")) { 107 | value += HARDENED; 108 | child = child.slice(0, -1); 109 | } 110 | 111 | const numChild = Number(child); 112 | 113 | if (Number.isNaN(numChild)) { 114 | throw new ResponseError( 115 | LedgerError.GenericError, 116 | `Invalid path : ${child} is not a number. (e.g "m/44'/461'/5'/0/3")`, 117 | ); 118 | } 119 | value += numChild; 120 | buf.appendUint32(value); 121 | }); 122 | 123 | return buf.getCompleteBuffer(); 124 | } 125 | 126 | async publicKey(path: string): Promise { 127 | const serializedPath = await this.serializeCosmosPath(path); 128 | const data = Buffer.concat([this.serializeHRP("cosmos"), serializedPath]); 129 | 130 | try { 131 | const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, 0, 0, data); 132 | const response = processResponse(responseBuffer); 133 | const compressed_pk = Buffer.from(response.readBytes(PKLEN)); 134 | 135 | return { 136 | compressed_pk, 137 | } as ResponsePubkey; 138 | } catch (e) { 139 | throw processErrorResponse(e); 140 | } 141 | } 142 | 143 | async getAddressAndPubKey(path: string, hrp: string): Promise { 144 | const serializedPath = await this.serializeCosmosPath(path); 145 | const data = Buffer.concat([this.serializeHRP(hrp), serializedPath]); 146 | 147 | try { 148 | const responseBuffer = await this.transport.send( 149 | this.CLA, 150 | this.INS.GET_ADDR_SECP256K1, 151 | P1_VALUES.ONLY_RETRIEVE, 152 | 0, 153 | data, 154 | ); 155 | 156 | const response = processResponse(responseBuffer); 157 | const compressed_pk = response.readBytes(PKLEN); 158 | const bech32_address = response.readBytes(response.length()).toString(); 159 | 160 | return { 161 | compressed_pk, 162 | bech32_address, 163 | } as ResponseAddress; 164 | } catch (e) { 165 | throw processErrorResponse(e); 166 | } 167 | } 168 | 169 | async showAddressAndPubKey(path: string, hrp: string): Promise { 170 | const serializedPath = await this.serializeCosmosPath(path); 171 | const data = Buffer.concat([this.serializeHRP(hrp), serializedPath]); 172 | 173 | try { 174 | const responseBuffer = await this.transport.send( 175 | this.CLA, 176 | this.INS.GET_ADDR_SECP256K1, 177 | P1_VALUES.SHOW_ADDRESS_IN_DEVICE, 178 | 0, 179 | data, 180 | ); 181 | 182 | const response = processResponse(responseBuffer); 183 | const compressed_pk = response.readBytes(PKLEN); 184 | const bech32_address = response.readBytes(response.length()).toString(); 185 | 186 | return { 187 | compressed_pk, 188 | bech32_address, 189 | } as ResponseAddress; 190 | } catch (e) { 191 | throw processErrorResponse(e); 192 | } 193 | } 194 | 195 | static getBech32FromPK(hrp: string, pk: Buffer) { 196 | if (pk.length !== 33) { 197 | throw new Error("expected compressed public key [31 bytes]"); 198 | } 199 | const hashSha256 = crypto.createHash("sha256").update(pk).digest(); 200 | const hashRip = new Ripemd160().update(hashSha256).digest(); 201 | return bech32.encode(hrp, bech32.toWords(hashRip)); 202 | } 203 | 204 | async prepareChunks_hrp(path: string, buffer: Buffer, hrp: string | undefined) { 205 | const serializedPath = await this.serializeCosmosPath(path); 206 | const firstChunk = 207 | hrp === undefined ? serializedPath : Buffer.concat([serializedPath, this.serializeHRP(hrp)]); 208 | 209 | const chunks = []; 210 | chunks.push(firstChunk); 211 | 212 | for (let i = 0; i < buffer.length; i += this.CHUNK_SIZE) { 213 | let end = i + this.CHUNK_SIZE; 214 | if (i > buffer.length) { 215 | end = buffer.length; 216 | } 217 | chunks.push(buffer.subarray(i, end)); 218 | } 219 | 220 | return chunks; 221 | } 222 | 223 | async signSendCosmosChunk( 224 | chunkIdx: number, 225 | chunkNum: number, 226 | chunk: Buffer, 227 | txtype = P2_VALUES.JSON, 228 | ): Promise { 229 | let payloadType = PAYLOAD_TYPE.ADD; 230 | if (chunkIdx === 1) { 231 | payloadType = PAYLOAD_TYPE.INIT; 232 | } 233 | if (chunkIdx === chunkNum) { 234 | payloadType = PAYLOAD_TYPE.LAST; 235 | } 236 | 237 | const statusList = [LedgerError.NoErrors, LedgerError.DataIsInvalid, LedgerError.BadKeyHandle]; 238 | 239 | const responseBuffer = await this.transport.send( 240 | this.CLA, 241 | this.INS.SIGN_SECP256K1, 242 | payloadType, 243 | txtype, 244 | chunk, 245 | statusList, 246 | ); 247 | const response = processResponse(responseBuffer); 248 | 249 | return response; 250 | } 251 | 252 | private async signImpl( 253 | path: string, 254 | buffer: Buffer, 255 | hrp: string | undefined, 256 | txtype: number, 257 | ): Promise { 258 | const chunks = await this.prepareChunks_hrp(path, buffer, hrp); 259 | 260 | try { 261 | let result = await this.signSendCosmosChunk(1, chunks.length, chunks[0], txtype); 262 | 263 | for (let i = 1; i < chunks.length; i += 1) { 264 | result = await this.signSendCosmosChunk(1 + i, chunks.length, chunks[i], txtype); 265 | } 266 | 267 | return { 268 | signature: result.readBytes(result.length()), 269 | }; 270 | } catch (e) { 271 | throw processErrorResponse(e); 272 | } 273 | } 274 | 275 | async sign( 276 | path: string, 277 | buffer: Buffer, 278 | hrp: string | undefined, 279 | txtype = P2_VALUES.JSON, 280 | ): Promise { 281 | return await this.signImpl(path, buffer, hrp, txtype); 282 | } 283 | } 284 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export interface ResponseAddress { 2 | bech32_address: string; 3 | compressed_pk: Buffer; 4 | } 5 | 6 | export interface ResponsePubkey { 7 | bech32_address: string; 8 | compressed_pk: Buffer; 9 | } 10 | 11 | export interface ResponseSign { 12 | signature: Buffer; 13 | } 14 | -------------------------------------------------------------------------------- /tests/helper.ts: -------------------------------------------------------------------------------- 1 | export const COSMOS_HRP = "cosmos"; 2 | export const INJ_HRP = "inj"; 3 | export const ETH_PATH = "m/44'/60'/0'/0/0"; 4 | export const COSMOS_PATH = "m/44'/118'/0'/0/0"; 5 | 6 | export const GET_ADDRESS_PUBKEY_RESPONSE = 7 | "035c986b9ae5fbfb8e1e9c12c817f5ef8fdb821cdecaa407f1420ec4f8f1d766bf636f736d6f7331776b643974666d357071766868617871373777763974766a6373617a7561796b77736c6436359000"; 8 | 9 | export const INVALID_HRP_RESPONSE = "6986"; 10 | 11 | export const GET_ETH_ADDRESS_RESPONSE = 12 | "022374f2dacd71042b5a888e3839e4ba54752ad6a51d35b54f6abb899c4329d4bf696e6a31356e3268306c7a76666763387834666d366664796138396e37387836656532663368377a33669000"; 13 | 14 | export const INVALID_ADDR_RESPONSE = "0x6984"; 15 | 16 | export const SIGN_BASIC_AMINO_RESPONSE = 17 | "304402206687b768c2971c973a990f7d64d3b97e2fbd8b7ccbeed3b323182a1b1350c17b022048d671283a3fa33148b8f0c4dfcc7051c1141e038763cecfec6b2f2f3006de8f9000"; 18 | 19 | export const example_tx_str_basic = { 20 | account_number: "108", 21 | chain_id: "cosmoshub-4", 22 | fee: { 23 | amount: [ 24 | { 25 | amount: "600", 26 | denom: "uatom", 27 | }, 28 | ], 29 | gas: "200000", 30 | }, 31 | memo: "", 32 | msgs: [ 33 | { 34 | type: "cosmos-sdk/MsgWithdrawDelegationReward", 35 | value: { 36 | delegator_address: "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", 37 | validator_address: "cosmosvaloper1kn3wugetjuy4zetlq6wadchfhvu3x740ae6z6x", 38 | }, 39 | }, 40 | { 41 | type: "cosmos-sdk/MsgWithdrawDelegationReward", 42 | value: { 43 | delegator_address: "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", 44 | validator_address: "cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0", 45 | }, 46 | }, 47 | ], 48 | sequence: "106", 49 | }; 50 | 51 | export const SIGN_MLTISEND_AMINO_RESPONSE = 52 | "3045022100f3f3b094717136dc7a9d85af87c75fd1c3b0b68a5025123cac645bb5a7f76c530220184cc26659cb19b814d91359276e80db93c30daae76a285e6a4487047e3d14df9000"; 53 | 54 | export const example_tx_str_msgMultiSend = { 55 | account_number: "10", 56 | chain_id: "chain-WiONzW", 57 | fee: { 58 | amount: [], 59 | gas: "200000", 60 | }, 61 | memo: "", 62 | msgs: [ 63 | { 64 | type: "cosmos-sdk/MsgMultiSend", 65 | value: { 66 | inputs: [ 67 | { 68 | address: "cosmos1w4efqfklkezgyt6lncjdwxncrzyzpr2efzcqal", 69 | coins: [ 70 | { 71 | amount: "30", 72 | denom: "stake", 73 | }, 74 | ], 75 | }, 76 | ], 77 | outputs: [ 78 | { 79 | address: "cosmos184hgxlzat3qhm7p28563w4jyw4aa3wcgnj6gtv", 80 | coins: [ 81 | { 82 | amount: "10", 83 | denom: "stake", 84 | }, 85 | ], 86 | }, 87 | { 88 | address: "cosmos1pfyz36qx8z8dm8ktd75mwx5j5vsmkzfn7wrgp9", 89 | coins: [ 90 | { 91 | amount: "10", 92 | denom: "stake", 93 | }, 94 | ], 95 | }, 96 | { 97 | address: "cosmos1xu388ml6krya3ysmlrup2ylxjtzhl4hlaem3ng", 98 | coins: [ 99 | { 100 | amount: "10", 101 | denom: "stake", 102 | }, 103 | ], 104 | }, 105 | ], 106 | }, 107 | }, 108 | ], 109 | sequence: "16", 110 | }; 111 | 112 | export const SIGN_BASIC_TEXTUAL_RESPONSE = 113 | "3045022100e5e33eeaa83e5a5ecc5960e27689f7508b5541ef5c42cc7aeaa229793a2d1c7c022049d93b1835c2bed9857bbbf00ccccbf5152bfef05b9ab6c0cc05fc76bf96cc7a9000"; 114 | export const example_tx_sign_textual = 115 | "a10192a20168436861696e20696402686d792d636861696ea2016e4163636f756e74206e756d626572026131a2016853657175656e6365026132a301674164647265737302782d636f736d6f7331756c6176336873656e7570737771666b77327933737570356b677471776e767161386579687304f5a3016a5075626c6963206b657902781f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657904f5a3026d5075624b6579206f626a656374030104f5a401634b657902785230324542204444374620453446442045423736204443384120323035452046363544203739304320443330452038413337203541354320323532382045423341203932334120463146422034443739203444030204f5a102781e54686973207472616e73616374696f6e206861732031204d657373616765a3016d4d6573736167652028312f312902781c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e640301a2026e4d736753656e64206f626a6563740302a3016c46726f6d206164647265737302782d636f736d6f7331756c6176336873656e7570737771666b77327933737570356b677471776e76716138657968730303a3016a546f206164647265737302782d636f736d6f7331656a726634637572327779366b667572673966326a707070326833616665356836706b6835740303a30166416d6f756e74026731302041544f4d0303a1026e456e64206f66204d657373616765a201644d656d6f0278193e20e29a9befb88f5c7532363942e29a9befb88f2020202020a2016446656573026a302e3030322041544f4da30169476173206c696d697402673130302730303004f5a3017148617368206f66207261772062797465730278403963303433323930313039633237306232666661396633633066613535613039306330313235656265663838316637646135333937386462663933663733383504f5"; 116 | -------------------------------------------------------------------------------- /tests/test_integration.test.ts: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * (c) 2018 - 2024 Zondax AG 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | *****************************************************************************/ 16 | import { MockTransport } from "@ledgerhq/hw-transport-mocker"; 17 | import CosmosApp from "../src"; 18 | import { 19 | COSMOS_HRP, 20 | COSMOS_PATH, 21 | ETH_PATH, 22 | GET_ADDRESS_PUBKEY_RESPONSE, 23 | GET_ETH_ADDRESS_RESPONSE, 24 | INJ_HRP, 25 | INVALID_ADDR_RESPONSE, 26 | INVALID_HRP_RESPONSE, 27 | SIGN_BASIC_AMINO_RESPONSE, 28 | SIGN_BASIC_TEXTUAL_RESPONSE, 29 | SIGN_MLTISEND_AMINO_RESPONSE, 30 | example_tx_sign_textual, 31 | example_tx_str_basic, 32 | example_tx_str_msgMultiSend, 33 | } from "./helper"; 34 | import { P2_VALUES } from "../src/consts"; 35 | 36 | describe("CosmosApp Address Generation", () => { 37 | it("Retreive Address and Pubkey", async () => { 38 | const responseBuffer = Buffer.from(GET_ADDRESS_PUBKEY_RESPONSE, "hex"); 39 | 40 | const transport = new MockTransport(responseBuffer); 41 | const app = new CosmosApp(transport); 42 | const resp = await app.getAddressAndPubKey("m/44'/118'/5'/0/3", COSMOS_HRP); 43 | 44 | expect(resp.bech32_address.toString()).toEqual("cosmos1wkd9tfm5pqvhhaxq77wv9tvjcsazuaykwsld65"); 45 | expect(resp.compressed_pk.length).toEqual(33); 46 | expect(resp.compressed_pk.toString("hex")).toEqual( 47 | "035c986b9ae5fbfb8e1e9c12c817f5ef8fdb821cdecaa407f1420ec4f8f1d766bf", 48 | ); 49 | }); 50 | 51 | it("Show Address and Pubkey", async () => { 52 | const responseBuffer = Buffer.from(GET_ADDRESS_PUBKEY_RESPONSE, "hex"); 53 | 54 | const transport = new MockTransport(responseBuffer); 55 | const app = new CosmosApp(transport); 56 | const resp = await app.showAddressAndPubKey("m/44'/118'/5'/0/3", COSMOS_HRP); 57 | 58 | expect(resp.bech32_address.toString()).toEqual("cosmos1wkd9tfm5pqvhhaxq77wv9tvjcsazuaykwsld65"); 59 | expect(resp.compressed_pk.length).toEqual(33); 60 | expect(resp.compressed_pk.toString("hex")).toEqual( 61 | "035c986b9ae5fbfb8e1e9c12c817f5ef8fdb821cdecaa407f1420ec4f8f1d766bf", 62 | ); 63 | }); 64 | 65 | it("Invalid HRP", async () => { 66 | const responseBuffer = Buffer.from(INVALID_HRP_RESPONSE, "hex"); 67 | 68 | const transport = new MockTransport(responseBuffer); 69 | const app = new CosmosApp(transport); 70 | try { 71 | const resp = await app.getAddressAndPubKey(ETH_PATH, COSMOS_HRP); 72 | } catch (e: any) { 73 | expect(e.message).toEqual("Transaction rejected"); 74 | } 75 | }); 76 | 77 | it("Get Eth Address", async () => { 78 | const responseBuffer = Buffer.from(GET_ETH_ADDRESS_RESPONSE, "hex"); 79 | 80 | const transport = new MockTransport(responseBuffer); 81 | const app = new CosmosApp(transport); 82 | const resp = await app.getAddressAndPubKey(ETH_PATH, INJ_HRP); 83 | 84 | expect(resp.bech32_address).toEqual("inj15n2h0lzvfgc8x4fm6fdya89n78x6ee2f3h7z3f"); 85 | }); 86 | 87 | it("Invalid Address Huge", async () => { 88 | const responseBuffer = Buffer.from(INVALID_ADDR_RESPONSE, "hex"); 89 | 90 | const transport = new MockTransport(responseBuffer); 91 | const app = new CosmosApp(transport); 92 | try { 93 | const resp = await app.getAddressAndPubKey("m/44'/2147483647'/0'/0/4294967295", "cosmos"); 94 | } catch (e: any) { 95 | expect(e.returnCode).toEqual(0xffff); 96 | } 97 | }); 98 | }); 99 | 100 | describe("CosmosApp Signing", () => { 101 | it("Sign Basic Amino TX", async () => { 102 | const responseBuffer = Buffer.from(SIGN_BASIC_AMINO_RESPONSE, "hex"); 103 | const tx = Buffer.from(JSON.stringify(example_tx_str_basic), "utf-8"); 104 | const transport = new MockTransport(responseBuffer); 105 | const app = new CosmosApp(transport); 106 | const resp = await app.sign(COSMOS_PATH, tx, COSMOS_HRP, P2_VALUES.JSON); 107 | 108 | expect(resp.signature.toString("hex")).toEqual( 109 | "304402206687b768c2971c973a990f7d64d3b97e2fbd8b7ccbeed3b323182a1b1350c17b022048d671283a3fa33148b8f0c4dfcc7051c1141e038763cecfec6b2f2f3006de8f", 110 | ); 111 | }); 112 | 113 | it("Sign MsgMultisend TX", async () => { 114 | const responseBuffer = Buffer.from(SIGN_MLTISEND_AMINO_RESPONSE, "hex"); 115 | const tx = Buffer.from(JSON.stringify(example_tx_str_msgMultiSend), "utf-8"); 116 | const transport = new MockTransport(responseBuffer); 117 | const app = new CosmosApp(transport); 118 | const resp = await app.sign(COSMOS_PATH, tx, COSMOS_HRP, P2_VALUES.JSON); 119 | 120 | expect(resp.signature.toString("hex")).toEqual( 121 | "3045022100f3f3b094717136dc7a9d85af87c75fd1c3b0b68a5025123cac645bb5a7f76c530220184cc26659cb19b814d91359276e80db93c30daae76a285e6a4487047e3d14df", 122 | ); 123 | }); 124 | 125 | it("Sign Basic Textual TX", async () => { 126 | const responseBuffer = Buffer.from(SIGN_BASIC_TEXTUAL_RESPONSE, "hex"); 127 | const tx = Buffer.from(JSON.stringify(example_tx_sign_textual), "utf-8"); 128 | const transport = new MockTransport(responseBuffer); 129 | const app = new CosmosApp(transport); 130 | const resp = await app.sign(COSMOS_PATH, tx, COSMOS_HRP, P2_VALUES.TEXTUAL); 131 | 132 | expect(resp.signature.toString("hex")).toEqual( 133 | "3045022100e5e33eeaa83e5a5ecc5960e27689f7508b5541ef5c42cc7aeaa229793a2d1c7c022049d93b1835c2bed9857bbbf00ccccbf5152bfef05b9ab6c0cc05fc76bf96cc7a", 134 | ); 135 | }); 136 | }); 137 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "moduleResolution": "node", 5 | "target": "es2020", 6 | "strict": true, 7 | "esModuleInterop": true, 8 | "declaration": true, 9 | "skipLibCheck": true, 10 | "outDir": "./dist", 11 | "sourceMap": true, 12 | "inlineSources": true 13 | }, 14 | "ts-node": { 15 | "swc": true 16 | }, 17 | "exclude": ["./dist/**"], 18 | "include": ["src"] 19 | } 20 | --------------------------------------------------------------------------------