From 084400d936efcb7aefee8639bae3f7beb9cda3e0 Mon Sep 17 00:00:00 2001 From: Marcin Gorzynski Date: Fri, 1 Dec 2023 10:41:43 +0100 Subject: [PATCH] Add Intent.IsValid & BasePacket.IsValid --- intents/intent.go | 24 ++++++++++++++++++++++++ intents/packets/packet.go | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/intents/intent.go b/intents/intent.go index 699c5116..208e5b41 100644 --- a/intents/intent.go +++ b/intents/intent.go @@ -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" @@ -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() diff --git a/intents/packets/packet.go b/intents/packets/packet.go index eb966665..4ccaa406 100644 --- a/intents/packets/packet.go +++ b/intents/packets/packet.go @@ -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"`