Files

82 lines
2.6 KiB
TypeScript

/** ******************************************************************************
* (c) 2020 Zondax GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */
import Zemu from '@zondax/zemu'
import { ROOT_PATH, defaultOptions, models } from './common'
import LuxApp from '@zondax/ledger-lux-app'
import { C_IMPORT_FROM_X, C_EXPORT_TO_X } from './c_chain_vectors'
// @ts-ignore
import secp256k1 from 'secp256k1/elliptic'
// @ts-ignore
import crypto from 'crypto'
const SIGN_TEST_DATA = [
{
name: 'c_import_from_x',
op: C_IMPORT_FROM_X,
},
{
name: 'c_export_to_x',
op: C_EXPORT_TO_X,
},
]
describe.each(models)('C_Sign[%s]; sign', function (m) {
test.each(SIGN_TEST_DATA)('sign c-chain $name transaction', async function ({ name, op }) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new LuxApp(sim.getTransport())
const msg = op
const testcase = `${m.prefix.toLowerCase()}-sign-${name}`
const signers = ["0/1", "5/8"];
const respReq = app.sign(ROOT_PATH, signers, msg);
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', testcase)
const resp = await respReq
console.log(resp, m.name, name)
expect(resp.returnCode).toEqual(0x9000)
expect(resp.errorMessage).toEqual('No errors')
expect(resp).toHaveProperty('signatures')
expect(resp.signatures?.size).toEqual(signers.length)
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(msg).digest())
for (const signer of signers) {
const path = `${ROOT_PATH}/${signer}`
const resp_addr = await app.getAddressAndPubKey(path, false)
const pk = Uint8Array.from(resp_addr.publicKey)
const signatureRS = Uint8Array.from(resp.signatures?.get(signer)!).slice(0, -1)
const signatureOk = secp256k1.ecdsaVerify(signatureRS, msgHash, pk)
expect(signatureOk).toEqual(true)
}
} finally {
await sim.close()
}
})
})