Skip to content

Commit

Permalink
Merge pull request #45 from ar-io/develop
Browse files Browse the repository at this point in the history
feat(PE-7235): release ID validation
  • Loading branch information
atticusofsparta authored Dec 4, 2024
2 parents fa1817e + ddb9f48 commit f9449fa
Show file tree
Hide file tree
Showing 21 changed files with 705 additions and 175 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
allow_defined = true
exclude_files = {
"dist/",
"src/common/crypto"
}
globals = {
"Handlers",
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

## [9] - [ezS3Z57rq_0skoG0WYmIqJ33mJiu0HbYNn9vEu12Mc4] - (2024-12-4)

### Changed

- Added Ethereum address support.
- Added Allow-Unsafe-Addresses flag to allow skipping of address validation for future compatibility with different signature algorithms on the below API's.
- Transfer
- Add-Controller
- Balance
- Approve-Primary-Name

## [8] - [XP9_LFTae8C0yvCb_DUJaC5LXaiZIbiGT1yY25X0JCg] - (2024-11-25)

### Added
Expand Down Expand Up @@ -34,6 +45,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
45 changes: 40 additions & 5 deletions spec/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- spec/utils_spec.lua
local utils = require(".common.utils")

local testEthAddress = "0xFCAd0B19bB29D4674531d6f115237E16AfCE377c"

describe("utils.camelCase", function()
it("should convert snake_case to camelCase", function()
assert.are.equal(utils.camelCase("start_end"), "startEnd")
Expand Down Expand Up @@ -42,17 +44,50 @@ 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 = 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 = 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 = utils.isValidAOAddress("invalid-arweave-id-123", false)
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", false)
assert.is_true(valid)
end)

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

Expand Down
14 changes: 9 additions & 5 deletions src/common/balances.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ local utils = require(".common.utils")

local balances = {}

---@alias AllowUnsafeAddresses boolean Whether to allow unsafe addresses

--- Transfers the ANT to a specified wallet.
---@param to string - The wallet address to transfer the balance to.
---@param allowUnsafeAddresses AllowUnsafeAddresses
---@return table<string, integer>
function balances.transfer(to)
utils.validateArweaveId(to)
function balances.transfer(to, allowUnsafeAddresses)
assert(utils.isValidAOAddress(to, allowUnsafeAddresses), "Invalid AO Address")
Balances = { [to] = 1 }
--luacheck: ignore Owner Controllers
Owner = to
Expand All @@ -20,9 +23,10 @@ end

--- Retrieves the balance of a specified wallet.
---@param address string - The wallet address to retrieve the balance from.
---@param allowUnsafeAddresses AllowUnsafeAddresses
---@return integer - Returns the balance of the specified wallet.
function balances.balance(address)
utils.validateArweaveId(address)
function balances.balance(address, allowUnsafeAddresses)
assert(utils.isValidAOAddress(address, allowUnsafeAddresses), "Invalid AO Address")
local balance = Balances[address] or 0
return balance
end
Expand Down Expand Up @@ -75,7 +79,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
9 changes: 5 additions & 4 deletions src/common/controllers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ local controllers = {}

--- Set a controller.
---@param controller string The controller to set.
---@param allowUnsafeAddresses AllowUnsafeAddresses
---@return string[]
function controllers.setController(controller)
utils.validateArweaveId(controller)
function controllers.setController(controller, allowUnsafeAddresses)
assert(utils.isValidAOAddress(controller, allowUnsafeAddresses), "Invalid AO Address")

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

for i, v in ipairs(Controllers) do
Expand All @@ -31,7 +32,7 @@ function controllers.removeController(controller)
end
end

assert(controllerExists ~= nil, "Controller does not exist")
assert(controllerExists ~= false, "Controller does not exist")
return Controllers
end

Expand Down
8 changes: 8 additions & 0 deletions src/common/crypto/digest/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local SHA3 = require(".crypto.digest.sha3")

local digest = {
_version = "0.0.1",
keccak256 = SHA3.keccak256,
}

return digest
Loading

0 comments on commit f9449fa

Please sign in to comment.