Skip to content

Commit

Permalink
fix(utils): update id validation utils
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Nov 21, 2024
1 parent 152e3e0 commit f96ffee
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed

- Fixed the Remove-Record api to return appropriate notices on calls.
- Update ID checks to use appropriate regexs and check both arweave and ethereum addresses

<!-- eslint-disable-next-line -->

Expand Down
42 changes: 38 additions & 4 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,54 @@ describe("utils.camelCase", function()
end)
end)

describe("utils.validateArweaveId", function()
describe("isValidEthAddress", function()
it("should validate eth address", function()
assert.is_true(utils.isValidEthAddress(testEthAddress))
end)

it("should fail on non-hexadecimal character ", function()
-- invalid non-hexadecimal G character
assert.is_false(utils.isValidEthAddress("0xFCAd0B19bB29D4674531d6f115237E16AfCE377G"))
end)

it("should return false on an an invalid-length address", function()
assert.is_false(utils.isValidEthAddress("0xFCAd0B19bB29D4674531d6f115237E16AfCE37"))
end)

it("should return false on passing in non-string value", function()
assert.is_false(utils.isValidEthAddress(3))
end)
end)

describe("utils.isValidArweaveAddress", function()
it("should throw an error for invalid Arweave IDs", function()
local invalid, error = pcall(utils.validateArweaveId, "invalid-arweave-id-123")
local invalid = pcall(utils.isValidArweaveAddress, "invalid-arweave-id-123")
assert.is_false(invalid)
assert.is_not_nil(error)
end)

it("should not throw an error for a valid Arweave ID", function()
local valid, error = pcall(utils.validateArweaveId, "0E7Ai_rEQ326_vLtgB81XHViFsLlcwQNqlT9ap24uQI")
local valid, error = pcall(utils.isValidArweaveAddress, "0E7Ai_rEQ326_vLtgB81XHViFsLlcwQNqlT9ap24uQI")
assert.is_true(valid)
assert.is_nil(error)
end)
end)

describe("utils.isValidAOAddress", function()
it("should throw an error for invalid Arweave IDs", function()
local invalid = pcall(utils.isValidAOAddress, "invalid-arweave-id-123")
assert.is_false(invalid)
end)

it("should not throw an error for a valid Arweave ID", function()
local valid = pcall(utils.isValidAOAddress, "0E7Ai_rEQ326_vLtgB81XHViFsLlcwQNqlT9ap24uQI")
assert.is_true(valid)
end)

it("should validate eth address", function()
assert.is_true(utils.isValidAOAddress(testEthAddress))
end)
end)

describe("utils.validateUndername", function()
it("should throw an error for invalid undernames", function()
local invalid, error = pcall(utils.validateUndername, "_invalid_undername_")
Expand Down
6 changes: 3 additions & 3 deletions src/common/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local balances = {}
---@param to string - The wallet address to transfer the balance to.
---@return table<string, integer>
function balances.transfer(to)
utils.validateArweaveId(to)
assert(utils.isValidAOAddress(to), "Invalid AO Address")
Balances = { [to] = 1 }
--luacheck: ignore Owner Controllers
Owner = to
Expand All @@ -22,7 +22,7 @@ end
---@param address string - The wallet address to retrieve the balance from.
---@return integer - Returns the balance of the specified wallet.
function balances.balance(address)
utils.validateArweaveId(address)
assert(utils.isValidAOAddress(address), "Invalid AO Address")
local balance = Balances[address] or 0
return balance
end
Expand Down Expand Up @@ -75,7 +75,7 @@ end
---@param logo string - The Arweave transaction ID that represents the logo.
---@return table<string, string>
function balances.setLogo(logo)
utils.validateArweaveId(logo)
assert(utils.isValidArweaveAddress(logo), "Invalid arweave ID")
Logo = logo
return { Logo = Logo }
end
Expand Down
4 changes: 2 additions & 2 deletions src/common/controllers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local controllers = {}
---@param controller string The controller to set.
---@return string[]
function controllers.setController(controller)
utils.validateArweaveId(controller)
assert(utils.isValidAOAddress(controller), "Invalid AO Address")

for _, c in ipairs(Controllers) do
assert(c ~= controller, "Controller already exists")
Expand All @@ -20,7 +20,7 @@ end
---@param controller string The controller to remove.
---@return string[]
function controllers.removeController(controller)
utils.validateArweaveId(controller)
assert(utils.isValidAOAddress(controller), "Invalid AO Address")
local controllerExists = false

for i, v in ipairs(Controllers) do
Expand Down
2 changes: 1 addition & 1 deletion src/common/initialize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function initialize.initializeANTState(state)
for k, v in pairs(records) do
utils.validateUndername(k)
assert(type(v) == "table", "records values must be tables")
utils.validateArweaveId(v.transactionId)
assert(utils.isValidArweaveAddress(v.transactionId), "Invalid arweave ID")
utils.validateTTLSeconds(v.ttlSeconds)
end

Expand Down
10 changes: 5 additions & 5 deletions src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function ant.init()

createActionHandler(ActionMap.ReleaseName, function(msg)
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["IO-Process-Id"])
assert(utils.isValidArweaveAddress(msg.Tags["IO-Process-Id"]), "Invalid Arweave ID")

assert(msg.Tags.Name, "Name is required")

Expand All @@ -247,7 +247,7 @@ function ant.init()

createActionHandler(ActionMap.ReassignName, function(msg)
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["Process-Id"])
assert(utils.isValidArweaveAddress(msg.Tags["Process-Id"]), "Invalid Arweave ID")

assert(msg.Tags.Name, "Name is required")

Expand Down Expand Up @@ -276,8 +276,8 @@ function ant.init()
createActionHandler(ActionMap.ApproveName, function(msg)
--- NOTE: this could be modified to allow specific users/controllers to create claims
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["IO-Process-Id"])
utils.validateArweaveId(msg.Tags.Recipient)
assert(utils.isValidArweaveAddress(msg.Tags["IO-Process-Id"]), "Invalid Arweave ID")
assert(utils.isValidAOAddress(msg.Tags.Recipient), "Invalid AO Address")

assert(msg.Tags.Name, "Name is required")

Expand All @@ -296,7 +296,7 @@ function ant.init()
createActionHandler(ActionMap.RemoveNames, function(msg)
--- NOTE: this could be modified to allow specific users/controllers to remove primary names
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["IO-Process-Id"])
assert(utils.isValidArweaveAddress(msg.Tags["IO-Process-Id"]), "Invalid Arweave ID")

assert(msg.Tags.Names, "Names are required")

Expand Down
2 changes: 1 addition & 1 deletion src/common/records.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Records = Records or { ["@"] = { transactionId = "-k7t8xMoB8hW482609Z9F4bTFMC3Mn
---@return Record
function records.setRecord(name, transactionId, ttlSeconds)
utils.validateUndername(name)
utils.validateArweaveId(transactionId)
assert(utils.isValidArweaveAddress(transactionId), "Invalid Arweave ID")
utils.validateTTLSeconds(ttlSeconds)

local recordsCount = #Records
Expand Down
31 changes: 19 additions & 12 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,25 @@ function utils.validateUndername(name)
assert(valid, constants.UNDERNAME_DOES_NOT_EXIST_MESSAGE)
end

---@param id string
---@description Asserts that the provided id is a valid arweave id
---@example
---```lua
---utils.validateArweaveId("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-")
---```
function utils.validateArweaveId(id)
-- the provided id matches the regex, and is not nil
local validLength = #id == 43
local validChars = string.match(id, "^[a-zA-Z0-9_-]+$") ~= nil
local valid = validLength and validChars
assert(valid, constants.INVALID_ARWEAVE_ID_MESSAGE)
--- Checks if an address is a valid Arweave address
--- @param address string The address to check
--- @return boolean isValidArweaveAddress - whether the address is a valid Arweave address
function utils.isValidArweaveAddress(address)
return type(address) == "string" and #address == 43 and string.match(address, "^[%w-_]+$") ~= nil
end

--- Checks if an address is a valid Ethereum address
--- @param address string The address to check
--- @return boolean isValidEthAddress - whether the address is a valid Ethereum address
function utils.isValidEthAddress(address)
return type(address) == "string" and #address == 42 and string.match(address, "^0x[%x]+$") ~= nil
end

--- Checks if an address is a valid AO address
--- @param url string|nil The address to check
--- @return boolean isValidAOAddress - whether the address is a valid AO address
function utils.isValidAOAddress(url)
return url and (utils.isValidArweaveAddress(url) or utils.isValidEthAddress(url)) or false
end

---@param ttl integer
Expand Down

0 comments on commit f96ffee

Please sign in to comment.