Skip to content

Commit

Permalink
fix about signature recovery identifier range
Browse files Browse the repository at this point in the history
  • Loading branch information
m0t0k1ch1 committed Apr 27, 2018
1 parent dc93cf3 commit f2527fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion domain/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package domain

const (
SignatureLength = 65 // bytes
SignatureLength = 65 // bytes
SignatureRecoveryIdentifierRangeBase = 27

UserNameLengthMin = 1
UserNameLengthMax = 16
Expand Down
6 changes: 6 additions & 0 deletions domain/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func (sig Signature) Bytes() []byte {
return sig[:]
}

func (sig *Signature) LowerRecoveryIdentifierRange() {
if sig[SignatureLength-1] >= SignatureRecoveryIdentifierRangeBase {
sig[SignatureLength-1] -= SignatureRecoveryIdentifierRangeBase
}
}

func NewSignatureFromBytes(sigBytes []byte) Signature {
sig := Signature{}
copy(sig[:], sigBytes[:])
Expand Down
10 changes: 3 additions & 7 deletions infrastructure/auth/metamask/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ func (s *service) SetUpChallenge(u *domain.User) error {
}

func (s *service) VerifyResponse(u *domain.User, responseBytes []byte) error {
switch responseBytes[domain.SignatureLength-1] {
case 27:
responseBytes[domain.SignatureLength-1] = 0
case 28:
responseBytes[domain.SignatureLength-1] = 1
}
sig := domain.NewSignatureFromBytes(responseBytes)
sig.LowerRecoveryIdentifierRange()

pubkey, err := crypto.SigToPub(
challenge(u.Challenge).signatureHashBytes(),
responseBytes,
sig.Bytes(),
)
if err != nil {
return err
Expand Down

0 comments on commit f2527fb

Please sign in to comment.