Skip to content

Commit

Permalink
add hash try for legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Aug 30, 2024
1 parent 46df4b7 commit 8f794db
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions tests_zemu/try_legacy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { ed25519 } from '@noble/curves/ed25519'
import pkg from 'blakejs'
const { blake2bFinal, blake2bInit, blake2bUpdate } = pkg

async function verifySignature(signature, message, publicKey) {
const context = blake2bInit(32)
blake2bUpdate(context, message)
const hash = Buffer.from(blake2bFinal(context))

const valid = ed25519.verify(signature, hash, publicKey)
if (valid) {
console.log('Valid signature')
} else {
console.log('Invalid signature')
}
}

async function main() {
const transport = await TransportNodeHid.default.open()
const app = new Kda.default(transport);;
Expand All @@ -13,27 +26,30 @@ async function main() {
const { publicKey } = await app.getPublicKey(PATH);
console.log("Pubkey: ", publicKey.toString('hex'))


// Sign a transaction
console.log("Signing a transaction")
const blob =
'{"networkId":"mainnet01","payload":{"exec":{"data":{"ks":{"pred":"keys-all","keys":["368820f80c324bbc7c2b0610688a7da43e39f91d118732671cd9c7500ff43cca"]}},"code":"(coin.transfer-create \\"alice\\" \\"bob\\" (read-keyset \\"ks\\") 100.1)\\n(coin.transfer \\"bob\\" \\"alice\\" 0.1)"}},"signers":[{"pubKey":"6be2f485a7af75fedb4b7f153a903f7e6000ca4aa501179c91a2450b777bd2a7","clist":[{"args":["alice","bob",100.1],"name":"coin.TRANSFER"},{"args":[],"name":"coin.GAS"}]},{"pubKey":"368820f80c324bbc7c2b0610688a7da43e39f91d118732671cd9c7500ff43cca","clist":[{"args":["bob","alice",0.1],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1580316382,"ttl":7200,"gasLimit":1200,"chainId":"0","gasPrice":1.0e-5,"sender":"alice"},"nonce":"2020-01-29 16:46:22.916695 UTC"}'
const messageToSign = Buffer.from(blob)
let messageToSign = Buffer.from(blob)
console.log(messageToSign.toString())
const signatureRequest = app.signTransaction(PATH, messageToSign);
let signatureRequest = app.signTransaction(PATH, messageToSign);

const signatureResponse = await signatureRequest
let signatureResponse = await signatureRequest
console.log(signatureResponse.signature.toString('hex'))

const context = blake2bInit(32)
blake2bUpdate(context, messageToSign)
const hash = Buffer.from(blake2bFinal(context))
await verifySignature(signatureResponse.signature, messageToSign, publicKey)

// Now verify the signature
const valid = ed25519.verify(signatureResponse.signature, hash, publicKey)
if (valid) {
console.log('Valid signature')
} else {
console.log('Invalid signature')
}
// Sign a hash
console.log("Signing a hash")
messageToSign = 'ffd8cd79deb956fa3c7d9be0f836f20ac84b140168a087a842be4760e40e2b1c'
console.log(messageToSign.toString())
signatureRequest = app.signHash(PATH, messageToSign);

signatureResponse = await signatureRequest
console.log(signatureResponse.signature.toString('hex'))

await verifySignature(signatureResponse.signature, messageToSign, publicKey)

}

;(async () => {
Expand Down

1 comment on commit 8f794db

@github-actions
Copy link

Choose a reason for hiding this comment

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

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 9 file(s) not formatted
  • app/src/items_format.h
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/parser_impl.h
  • app/src/common/tx.h
  • app/src/parser_impl.c
  • app/src/items.c
  • app/src/apdu_handler_legacy.c
  • app/src/parser.c

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.