Skip to content

Commit

Permalink
feat: check permission before excuting actual logic
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Dec 20, 2024
1 parent fa34746 commit ffd80ea
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions _deploy/r/gnoswap/gnft/gnft.gno
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func GetApproved(tid grc721.TokenID) (std.Address, bool) {
// - tid: The token ID to approve
func Approve(user pusers.AddressOrName, tid grc721.TokenID) {
common.IsHalted()
assertCallerIsOwnerOfToken(tid)

err := gnft.Approve(users.Resolve(user), tid)
if err != nil {
Expand All @@ -151,10 +152,22 @@ func SetApprovalForAll(user pusers.AddressOrName, approved bool) {
// - tid: The token ID to transfer
func TransferFrom(from, to pusers.AddressOrName, tid grc721.TokenID) {
common.IsHalted()
checkErr(gnft.TransferFrom(users.Resolve(from), users.Resolve(to), tid))

caller := getPrevAddr()
ownerOf := OwnerOf(tid)
_, approved := GetApproved(tid)

if caller != ownerOf && !approved {
panic(addDetailToError(
errNoPermission,
ufmt.Sprintf("caller (%s) is not the owner or operator of token (%s)", caller, string(tid)),
))
}

removeTokenList(users.Resolve(from), tid)
appendTokenList(users.Resolve(to), tid)

checkErr(gnft.TransferFrom(users.Resolve(from), users.Resolve(to), tid))
}

// Mint creates a new NFT and assigns it to the specified address (only callable by owner)
Expand All @@ -168,9 +181,9 @@ func Mint(to pusers.AddressOrName, tid grc721.TokenID) grc721.TokenID {
owner.AssertCallerIsOwner()
common.IsHalted()

checkErr(gnft.Mint(users.Resolve(to), tid))

toAddr := users.Resolve(to)
checkErr(gnft.Mint(toAddr, tid))

appendTokenList(toAddr, tid)
return tid
}
Expand All @@ -182,9 +195,8 @@ func Burn(tid grc721.TokenID) {
owner.AssertCallerIsOwner()
common.IsHalted()

removeTokenList(OwnerOf(tid), tid)
checkErr(gnft.Burn(tid))

removeTokenList(getPrevAddr(), tid)
}

// Render returns the HTML representation of the NFT
Expand Down Expand Up @@ -223,7 +235,7 @@ func SetTokenURI(tid grc721.TokenID) {
func SetTokenURILast() {
common.IsHalted()

caller := std.PrevRealm().Addr()
caller := getPrevAddr()
tokenListByCaller, _ := getTokenList(caller)
lenTokenListByCaller := len(tokenListByCaller)
if lenTokenListByCaller == 0 {
Expand Down

0 comments on commit ffd80ea

Please sign in to comment.