Skip to content

Commit

Permalink
farcaster: allow shortened URLs
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Mar 6, 2024
1 parent 1c2b819 commit 21f05df
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions vochain/transaction/proofs/farcasterproof/farcasterproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package farcasterproof
import (
"bytes"
"crypto/ed25519"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
Expand All @@ -21,19 +22,22 @@ import (
)

const (
frameHashSize = 20
pollURLpattern = `([0-9a-fA-F]{64})`
frameHashSize = 20
pollURLpattern = `([0-9a-fA-F]{64})`
pollURLpatternShortened = `([0-9a-zA-Z+/]{8})`
)

var (
// DisableElectionIDVerification is a flag to dissable the election ID verification on the poll URL.
// This should be used only for testing purposes.
DisableElectionIDVerification = false
re *regexp.Regexp
reShortened *regexp.Regexp
)

func init() {
re = regexp.MustCompile(pollURLpattern)
reShortened = regexp.MustCompile(pollURLpatternShortened)
}

// FarcasterVerifier is a proof verifier for the Farcaster frame protocol.
Expand Down Expand Up @@ -86,7 +90,17 @@ func (*FarcasterVerifier) Verify(process *models.Process, envelope *models.VoteE
return false, nil, fmt.Errorf("process ID mismatch (got %x, expected %x)", votePID, envelope.ProcessId)
}
} else {
return false, nil, fmt.Errorf("no process ID found on poll URL")
// If no match is found, we try to match a shortened process ID
matches = reShortened.FindStringSubmatch(string(frameAction.Url))
if len(matches) > 1 {
if matches[1] != generateShortenedProcessID(envelope.ProcessId) {
return false, nil, fmt.Errorf("shortened process ID mismatch (got %s, expected %s)",
matches[1], generateShortenedProcessID(envelope.ProcessId))
}
} else {
// If no match is found, we return an error
return false, nil, fmt.Errorf("no process ID found on poll URL")
}
}
}

Expand Down Expand Up @@ -209,3 +223,11 @@ func GenerateNullifier(farcasterID uint64, processID []byte) []byte {
binary.LittleEndian.PutUint64(fidBytes, farcasterID)
return ethereum.HashRaw(append(fidBytes, processID...))
}

func generateShortenedProcessID(processID []byte) string {
// We take the 8 first chars of the base64 encoded hash of the processID.
// The probability of at least one collision among 100,000 generated hashes is approximately 0.0018%.
// The probability of at least one collision among 1,000,000 generated hashes is approximately 0.177%.
hash := blake3.Sum256(processID)
return base64.StdEncoding.EncodeToString(hash[:])[:8]
}

0 comments on commit 21f05df

Please sign in to comment.