Skip to content

Commit

Permalink
Merge pull request #64 from Zondax/dev-textual-mode
Browse files Browse the repository at this point in the history
support textual mode
  • Loading branch information
chcmedeiros authored Dec 7, 2022
2 parents 150ccbe + 4d1bb3d commit c067e17
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README-npm.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ We recommend using the npmjs package in order to receive updates/fixes.
| showAddressAndPubKey | signed message | path |
| appInfo | name, version, flags, etc | --------------- |
| deviceInfo | fw and mcu version, id, etc | Only available in dashboard |
| sign | signed message | path + message |

| sign | signed message | path + transaction type + 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 user confirmation.

sign command requires that you specify the transaction type, with one of the available types: 0 for json transaction or 1 for textual transaction.

# Testing with real devices

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ We recommend using the npmjs package in order to receive updates/fixes.
| showAddressAndPubKey | signed message | path |
| appInfo | name, version, flags, etc | --------------- |
| deviceInfo | fw and mcu version, id, etc | Only available in dashboard |
| sign | signed message | path + message |
| sign | signed message | path + transaction type + 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 user confirmation.

sign command requires that you specify the transaction type, with one of the available types: 0 for json transaction or 1 for textual transaction.

# Testing with real devices

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zondax/ledger-cosmos-js",
"author": "ZondaX AG",
"license": "Apache-2.0",
"version": "3.0.1",
"version": "3.0.2",
"description": "Node API for Cosmos App (Ledger Nano S/S+/X)",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
Expand Down Expand Up @@ -44,6 +44,7 @@
"@swc/core": "^1.2.237",
"@types/jest": "^26.0.24",
"@types/ledgerhq__hw-transport": "4.21.4",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"bip32": "2.0.6",
Expand Down
6 changes: 3 additions & 3 deletions src/commandsV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */
import { CLA, ERROR_CODE, errorCodeToString, INS, processErrorResponse } from './common'
import { CLA, ERROR_CODE, errorCodeToString, INS, processErrorResponse, P2_VALUES } from './common'

export function serializePathv1(path: number[]) {
if (path == null || path.length < 3) {
Expand All @@ -35,9 +35,9 @@ export function serializePathv1(path: number[]) {
return buf
}

export async function signSendChunkv1(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer) {
export async function signSendChunkv1(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
return app.transport
.send(CLA, INS.SIGN_SECP256K1, chunkIdx, chunkNum, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])
.send(CLA, INS.SIGN_SECP256K1, chunkIdx, txType, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])
.then((response: any) => {
const errorCodeData = response.slice(-2)
const returnCode = errorCodeData[0] * 256 + errorCodeData[1]
Expand Down
6 changes: 3 additions & 3 deletions src/commandsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
******************************************************************************* */
import { signSendChunkv1 } from './commandsV1'
import { CLA, ERROR_CODE, errorCodeToString, INS, PAYLOAD_TYPE, processErrorResponse } from './common'
import { CLA, ERROR_CODE, errorCodeToString, INS, PAYLOAD_TYPE, processErrorResponse, P2_VALUES } from './common'

export function serializePathv2(path: number[]) {
if (!path || path.length !== 5) {
Expand All @@ -31,7 +31,7 @@ export function serializePathv2(path: number[]) {
return buf
}

export async function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer) {
export async function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
let payloadType = PAYLOAD_TYPE.ADD
if (chunkIdx === 1) {
payloadType = PAYLOAD_TYPE.INIT
Expand All @@ -40,7 +40,7 @@ export async function signSendChunkv2(app: any, chunkIdx: number, chunkNum: numb
payloadType = PAYLOAD_TYPE.LAST
}

return signSendChunkv1(app, payloadType, 0, chunk)
return signSendChunkv1(app, payloadType, 0, chunk, txType)
}

export async function publicKeyv2(app: any, data: Buffer) {
Expand Down
5 changes: 5 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export const P1_VALUES = {
SHOW_ADDRESS_IN_DEVICE: 0x01,
}

export const P2_VALUES = {
JSON: 0x0,
TEXTUAL: 0x1,
}

export const ERROR_CODE = {
NoError: 0x9000,
}
Expand Down
19 changes: 9 additions & 10 deletions src/cosmosApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */
import { CHUNK_SIZE, CLA, INS, errorCodeToString, getVersion, processErrorResponse, ERROR_CODE, P1_VALUES } from './common'
import { CHUNK_SIZE, CLA, INS, errorCodeToString, getVersion, processErrorResponse, ERROR_CODE, P1_VALUES, P2_VALUES } from './common'

import Transport from '@ledgerhq/hw-transport'
import { publicKeyv1, serializePathv1, signSendChunkv1 } from './commandsV1'
Expand Down Expand Up @@ -71,12 +71,11 @@ export class CosmosApp {
}
}

async signGetChunks(path: number[], message: string) {
async signGetChunks(path: number[], buffer: Buffer) {
const serializedPath = await this.serializePath(path)

const chunks = []
chunks.push(serializedPath)
const buffer = Buffer.from(message)

for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
let end = i + CHUNK_SIZE
Expand Down Expand Up @@ -244,12 +243,12 @@ export class CosmosApp {
return this.getAddressAndPubKey(path, hrp, true)
}

async signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer) {
async signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
switch (this.versionResponse.major) {
case 1:
return signSendChunkv1(this, chunkIdx, chunkNum, chunk)
return signSendChunkv1(this, chunkIdx, chunkNum, chunk, txType)
case 2:
return signSendChunkv2(this, chunkIdx, chunkNum, chunk)
return signSendChunkv2(this, chunkIdx, chunkNum, chunk, txType)
default:
return {
return_code: 0x6400,
Expand All @@ -258,9 +257,9 @@ export class CosmosApp {
}
}

async sign(path: number[], message: string) {
return this.signGetChunks(path, message).then(chunks => {
return this.signSendChunk(1, chunks.length, chunks[0]).then(async response => {
async sign(path: number[], buffer: Buffer, txType = P2_VALUES.JSON) {
return this.signGetChunks(path, buffer).then(chunks => {
return this.signSendChunk(1, chunks.length, chunks[0], txType).then(async response => {
let result = {
return_code: response.return_code,
error_message: response.error_message,
Expand All @@ -269,7 +268,7 @@ export class CosmosApp {

for (let i = 1; i < chunks.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
result = await this.signSendChunk(1 + i, chunks.length, chunks[i])
result = await this.signSendChunk(1 + i, chunks.length, chunks[i], txType)
if (result.return_code !== ERROR_CODE.NoError) {
break
}
Expand Down

0 comments on commit c067e17

Please sign in to comment.