Skip to content

Commit

Permalink
Remove reference to eosjs-ecc and add tests for new logic
Browse files Browse the repository at this point in the history
some change

Refactor

New version

Fix linting
  • Loading branch information
Jon La Marr committed Dec 11, 2019
1 parent 43c02aa commit 8a4d0fd
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 215 deletions.
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",
"@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

0 comments on commit 8a4d0fd

Please sign in to comment.