Skip to content

Commit

Permalink
fix: ledger sig conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Mar 4, 2024
1 parent eeced6c commit 6cbdc04
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions crypto/ledger_secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crypto

import (
"bufio"
"encoding/hex"
"fmt"
"math/big"
"os"
Expand Down Expand Up @@ -154,18 +155,38 @@ func (pkl PrivKeyLedgerSecp256k1) Sign(msg []byte) ([]byte, error) {
if err != nil {
return nil, err
}
fmt.Println("DEBUG!!! ledger sig", hex.EncodeToString(sig))

return convertDERtoBER(sig)
convertedSig, err := convertDERtoBER(sig)
if err != nil {
return nil, err
}
fmt.Println("DEBUG!!! converted sig", hex.EncodeToString(convertedSig))
return convertedSig, nil
}

func convertDERtoBER(signatureDER []byte) ([]byte, error) {
sigDER, err := ecdsa.ParseDERSignature(signatureDER[:])
if err != nil {
return nil, err
}
sig := sigDER.Serialize() // 0x30 <total length> 0x02 <length of R> <R> 0x02 <length of S> <S>
r := new(big.Int).SetBytes(sig[4:36])
s := new(big.Int).SetBytes(sig[38:70])

// Total length of returned signature is 1 byte for each magic and length
// (6 total), plus lengths of R and S.
// totalLen := 6 + len(canonR) + len(canonS)
// b := make([]byte, 0, totalLen)
// b = append(b, asn1SequenceID)
// b = append(b, byte(totalLen-2))
// b = append(b, asn1IntegerID)
// b = append(b, byte(len(canonR)))
// b = append(b, canonR...)
// b = append(b, asn1IntegerID)
// b = append(b, byte(len(canonS)))
// b = append(b, canonS...)
sig := sigDER.Serialize()
lenOfR := int(sig[3])
r := new(big.Int).SetBytes(sig[4 : 4+lenOfR])
s := new(big.Int).SetBytes(sig[4+lenOfR+2:])
sigBER := tmbtcec.Signature{R: r, S: s}
return sigBER.Serialize(), nil
}
Expand Down

0 comments on commit 6cbdc04

Please sign in to comment.