Skip to content

Commit

Permalink
fix: make signature response depending on curve
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon authored and dawidsowardx committed May 1, 2023
1 parent 5a54d14 commit aef48bd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
47 changes: 29 additions & 18 deletions src/ledger/wrapper/ledger-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const createLedgerWrapperWithMockedTransport = (
})
}

const getExpectedTransactionSigningExchanges = (instructionCode: string) => [
const getExpectedTransactionSigningExchanges = (
instructionCode: string,
finalOutput: string
) => [
{
input: 'aa120000',
output: '305495ba9000',
Expand Down Expand Up @@ -62,19 +65,7 @@ const getExpectedTransactionSigningExchanges = (instructionCode: string) => [
},
{
input: `ac${instructionCode}00002e651702800289ba4758731898e0850fbde5d412c080e4f8b7ea03174cb180d90c0a6669656c645f6e616d65202000`,
output:
'5ad8cd006761aa698ea77f271c421a7ea9e34da45b4e827d2fce1b5205933b77e852261381cfaa0a8ecfba52622d5c1560462db70df08fc905111e2a9a5fa601cffce054df51fb4072e7faf627e0f64f168fd8811f749d34720ac8da264bac069000',
},
]

const getExpectedTransacionSignature = (curve: string) => [
{
curve,
derivationPath: `m/44'/1022'/10'/525'/0'/1238'`,
publicKey:
'cffce054df51fb4072e7faf627e0f64f168fd8811f749d34720ac8da264bac06',
signature:
'5ad8cd006761aa698ea77f271c421a7ea9e34da45b4e827d2fce1b5205933b77e852261381cfaa0a8ecfba52622d5c1560462db70df08fc905111e2a9a5fa601',
output: finalOutput,
},
]

Expand Down Expand Up @@ -317,7 +308,8 @@ describe('Ledger Babylon Wrapper', () => {
it('should sign verbose TX using curve25519', async () => {
const ledger = createLedgerWrapperWithMockedTransport(
getExpectedTransactionSigningExchanges(
LedgerInstructionCode.SignTxEd255519
LedgerInstructionCode.SignTxEd255519,
'5ad8cd006761aa698ea77f271c421a7ea9e34da45b4e827d2fce1b5205933b77e852261381cfaa0a8ecfba52622d5c1560462db70df08fc905111e2a9a5fa601cffce054df51fb4072e7faf627e0f64f168fd8811f749d34720ac8da264bac069000'
)
)

Expand All @@ -330,13 +322,23 @@ describe('Ledger Babylon Wrapper', () => {

if (result.isErr()) throw result.error

expect(result.value).toEqual(getExpectedTransacionSignature('curve25519'))
expect(result.value).toEqual([
{
curve: 'curve25519',
derivationPath: `m/44'/1022'/10'/525'/0'/1238'`,
publicKey:
'cffce054df51fb4072e7faf627e0f64f168fd8811f749d34720ac8da264bac06',
signature:
'5ad8cd006761aa698ea77f271c421a7ea9e34da45b4e827d2fce1b5205933b77e852261381cfaa0a8ecfba52622d5c1560462db70df08fc905111e2a9a5fa601',
},
])
})

it('should sign summary TX using secp256k1', async () => {
const ledger = createLedgerWrapperWithMockedTransport(
getExpectedTransactionSigningExchanges(
LedgerInstructionCode.SignTxSecp256k1Smart
LedgerInstructionCode.SignTxSecp256k1Smart,
'5adadad8cd006761aa698ea77f271c421a7ea9e34da45b4e827d2fce1b5205933b77e852261381cfaa0a8ecfba52622d5c1560462db70df08fc905111e2a9a5fa601cffce054df51fb4072e7faf627e0f64f168fd8811f749d34720ac8da264bac069000'
)
)

Expand All @@ -354,7 +356,16 @@ describe('Ledger Babylon Wrapper', () => {

if (result.isErr()) throw result.error

expect(result.value).toEqual(getExpectedTransacionSignature('secp256k1'))
expect(result.value).toEqual([
{
curve: 'secp256k1',
derivationPath: `m/44'/1022'/10'/525'/0'/1238'`,
publicKey:
'01cffce054df51fb4072e7faf627e0f64f168fd8811f749d34720ac8da264bac06',
signature:
'5adadad8cd006761aa698ea77f271c421a7ea9e34da45b4e827d2fce1b5205933b77e852261381cfaa0a8ecfba52622d5c1560462db70df08fc905111e2a9a5fa6',
},
])
})
})
})
37 changes: 33 additions & 4 deletions src/ledger/wrapper/ledger-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,39 @@ export const LedgerWrapper = ({
okAsync('')
)
)
.map((result) => ({
publicKey: result.slice(128),
signature: result.slice(0, 128),
}))
.andThen((result) => {
const isCurve25519 = signer.curve === 'curve25519'
const signatureByteCount = isCurve25519 ? 64 : 65
const publicKeyByteCount = isCurve25519 ? 32 : 33
if (
result.length !==
(signatureByteCount + publicKeyByteCount) * 2
) {
return err(
'Result containing signature and PublicKey has incorrect length.'
)
}

const signature = result.slice(0, signatureByteCount * 2)
const sigOffset = signatureByteCount * 2
const publicKey = result.slice(
sigOffset,
sigOffset + 2 * publicKeyByteCount + 1
)

if (signature.length !== signatureByteCount * 2) {
return err('Signature has incorrect length.')
}

if (publicKey.length !== publicKeyByteCount * 2) {
return err('PublicKey has incorrect length.')
}

return ok({
signature,
publicKey,
})
})
.map((result) => [
...previousValue,
{
Expand Down

0 comments on commit aef48bd

Please sign in to comment.