Skip to content

Commit

Permalink
Add Intent.IsValid & BasePacket.IsValid
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Dec 1, 2023
1 parent ab6f968 commit 084400d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions intents/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/0xsequence/go-sequence/intents/packets"
"github.com/gibson042/canonicaljson-go"

"github.com/0xsequence/ethkit/go-ethereum/common"
Expand Down Expand Up @@ -77,6 +78,29 @@ func (intent *Intent) Signers() []string {
return signers
}

func (intent *Intent) IsValid() bool {
// Check if there are any signatures
if len(intent.signatures) == 0 {
return false
}

// Check if all signatures are valid
for _, signature := range intent.signatures {
if !intent.isValidSignature(signature.Session, signature.Signature) {
return false
}
}

// Check if the packet is valid
var packet packets.BasePacket
err := json.Unmarshal(intent.Packet, &packet)
if err != nil {
return false
}

return packet.IsValid()
}

func (intent *Intent) isValidSignature(session string, signature string) bool {
// Get hash of the packet
hash, err := intent.Hash()
Expand Down
7 changes: 7 additions & 0 deletions intents/packets/packet.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package packets

import "time"

type BasePacket struct {
Code string `json:"code"`
Issued uint64 `json:"issued"`
Expires uint64 `json:"expires"`
}

func (p *BasePacket) IsValid() bool {
now := uint64(time.Now().Unix())
return p.Code != "" && p.Issued <= now && p.Expires > now
}

type BasePacketForWallet struct {
BasePacket
Wallet string `json:"wallet"`
Expand Down

0 comments on commit 084400d

Please sign in to comment.