Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reference to eosjs-ecc and add tests for new logic #32

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@
"@babel/core": "7.6.0",
"@babel/preset-env": "7.2.3",
"@babel/preset-react": "7.0.0",
"@ledgerhq/hw-transport": "4.48.0",
"@ledgerhq/hw-transport-u2f": "4.48.0",
"@ledgerhq/hw-transport": "5.0.0-babel7.3",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be updated due to this issue that's been open for a long time: LedgerHQ/ledgerjs#332, JoinColony/purser#184

This has a fix with an open PR: LedgerHQ/ledgerjs#241

"@ledgerhq/hw-transport-u2f": "5.0.0-babel7.3",
"@types/elliptic": "^6.4.10",
"@types/jest": "^23.3.9",
"asn1-ber": "1.0.9",
"bip32-path": "0.4.2",
"buffer": "5.2.1",
"eosjs": "20.0.0",
"eosjs-ecc": "4.0.4"
"eosjs": "20.0.4-67ee21f.0",
"elliptic": "^6.5.0"
},
"devDependencies": {
"@babel/cli": "^7.6.0",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@blockone/tslint-config-blockone": "^4.0.0",
"@types/node": "^11.9.4",
"babel-polyfill": "^6.26.0",
"jest": "^24.1.0",
"ts-jest": "^23.10.4",
"tslib": "^1.9.3",
Expand Down
58 changes: 58 additions & 0 deletions src/LedgerUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Signature } from 'eosjs/dist/eosjs-jssig'
import { convertSignatures } from './LedgerUtils'

const mockedConvertedSignatureValue = 'mockedConvertedSignatureValue'

describe('JsSignatureProvider', () => {
it('should not convert signature given string with length < 130 characters', async () => {
const signature = 'someSignature'
const result = convertSignatures([signature], mockConvertSignature)

expect(result[0]).toBe(signature)
})

it('should not convert signature given string with length > 130 characters', async () => {
const signature = '4cf0909aa10f10341cd7ca013630492d09b65e50c80974e962d94bbc95df635e90afb2e4dd22eb254cb451ca26a6aeb0b88e9b031a2233b2a16d56252dba83f9632-more-than-130-characters'
const result = convertSignatures([signature], mockConvertSignature)

expect(result[0]).toBe(signature)
})

it('should convert signature given string with length === 130 characters', () => {
const signature = 'SIG_K1_HKkqi3zray76i63ZQwAHWMjoLk3wTa1ajZWPcUnrhgmSWQYEHDJsxkny6VDTWEmVdfktxpGoTA81qe6QuCrDmazeQndmxhktxpGoTA81qe6QuCrDmazeQndmxhD'
const result = convertSignatures([signature], mockConvertSignature)

expect(result[0]).toBe(mockedConvertedSignatureValue)
})

it('should convert signature given string with length === 130 characters, but not another signature with length < 130 characters', () => {
const signature1 = 'SIG_K1_HKkqi3zray76i63ZQwAHWMjoLk3wTa1ajZWPcUnrhgmSWQYEHDJsxkny6VDTWEmVdfktxpGoTA81qe6QuCrDmazeQndmxhktxpGoTA81qe6QuCrDmazeQndmxhD'
const signature2 = 'someSignature'
const result = convertSignatures([signature1, signature2], mockConvertSignature)

expect(result[0]).toBe(mockedConvertedSignatureValue)
expect(result[1]).toBe(signature2)
})

it('should convert signature given string with length === 130 characters, but not another signature with length > 130 characters', () => {
const signature1 = 'SIG_K1_HKkqi3zray76i63ZQwAHWMjoLk3wTa1ajZWPcUnrhgmSWQYEHDJsxkny6VDTWEmVdfktxpGoTA81qe6QuCrDmazeQndmxhktxpGoTA81qe6QuCrDmazeQndmxhD'
const signature2 = '4cf0909aa10f10341cd7ca013630492d09b65e50c80974e962d94bbc95df635e90afb2e4dd22eb254cb451ca26a6aeb0b88e9b031a2233b2a16d56252dba83f9632-more-than-130-characters'
const result = convertSignatures([signature1, signature2], mockConvertSignature)

expect(result[0]).toBe(mockedConvertedSignatureValue)
expect(result[1]).toBe(signature2)
})

it('should convert multiple signatures given string with length === 130 characters', () => {
const signature1 = 'SIG_K1_HKkqi3zray76i63ZQwAHWMjoLk3wTa1ajZWPcUnrhgmSWQYEHDJsxkny6VDTWEmVdfktxpGoTA81qe6QuCrDmazeQndmxhktxpGoTA81qe6QuCrDmazeQndmxhD'
const signature2 = 'SIG_K1_HKkqi3zray76i63ZQwAHWMjoLk3wTa1ajZWPcUnrhgmSWQYEHDJsxkny6VDTWEmVdfktxpGoTA81qe6QuCrDmazeQndmxhktxpGoTA81qe6QuCrDmazeQndmxhD'
const result = convertSignatures([signature1, signature2], mockConvertSignature)

expect(result[0]).toBe(mockedConvertedSignatureValue)
expect(result[1]).toBe(mockedConvertedSignatureValue)
})
})

function mockConvertSignature(signature: string) {
return mockedConvertedSignatureValue
}
13 changes: 8 additions & 5 deletions src/LedgerUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Transport from '@ledgerhq/hw-transport-u2f'
import { Api, JsonRpc, Serialize } from 'eosjs'
import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig'

import ecc from 'eosjs-ecc'
import { JsSignatureProvider, Signature } from 'eosjs/dist/eosjs-jssig'

import asn1 from 'asn1-ber'
import Buff from 'buffer/'
Expand Down Expand Up @@ -32,7 +30,8 @@ export async function getTransport() {
return cachedTransport
}

export const convertSignatures = (sigs: string[]): string[] => {
export const convertSignatures = (sigs: string[],
signatureConversion: (signature: string) => string = convertSignature): string[] => {
if (!Array.isArray(sigs)) {
sigs = [sigs]
}
Expand All @@ -42,7 +41,7 @@ export const convertSignatures = (sigs: string[]): string[] => {
for (let i = 0; i < sigs.length; i++) {
const sig = sigs[i]
if (typeof sig === 'string' && sig.length === 130) {
sigs[i] = ecc.Signature.from(sig).toString()
sigs[i] = signatureConversion(sig)
}
}

Expand Down Expand Up @@ -117,3 +116,7 @@ const createNewBuffer = (api: Api, type: string, data: any) => {
const encode = (writer: any , buffer: Uint8Array) => {
writer.writeBuffer(Buff.Buffer.from(buffer), asn1.Ber.OctetString)
}

const convertSignature = (sig: string): string => {
return Signature.fromString(sig).toString()
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"esModuleInterop": true,
"skipLibCheck": true,
"jsx": "react",
"types": [],
"lib": ["es2015", "dom"]
},
"include": [
Expand Down
Loading