Skip to content

Commit

Permalink
feat(pns): support sending primary name messages to io process
Browse files Browse the repository at this point in the history
These can be extended to allow controllers to create and remove primary name claims.
  • Loading branch information
dtfiedler committed Nov 14, 2024
1 parent 0587c4a commit 7fa09a4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
90 changes: 83 additions & 7 deletions src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function ant.init()
-- IO Network Contract Handlers
ReleaseName = "Release-Name",
ReassignName = "Reassign-Name",
CreateClaim = "Create-Primary-Name-Claims",
RevokeClaims = "Revoke-Primary-Name-Claims",
RemovePrimaryNames = "Remove-Primary-Name-Claims",
}

local TokenSpecActionMap = {
Expand All @@ -86,10 +89,6 @@ function ant.init()
Balance = "Balance",
Transfer = "Transfer",
TotalSupply = "Total-Supply",
-- not implemented
-- CreditNotice = "Credit-Notice",
-- Mint = "Mint",
-- Burn = "Burn",
}

createActionHandler(TokenSpecActionMap.Transfer, function(msg)
Expand Down Expand Up @@ -251,8 +250,9 @@ function ant.init()
createActionHandler(ActionMap.ReassignName, function(msg)
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["Process-Id"])
utils.validateUndername(msg.Tags.Name)

local name = string.lower(msg.Tags["Name"])
local name = msg.Tags.Name and string.lower(msg.Tags.Name) or nil
local ioProcess = msg.Tags["IO-Process-Id"]
local antProcessIdToReassign = msg.Tags["Process-Id"]

Expand All @@ -267,14 +267,90 @@ function ant.init()

ao.send({
Target = msg.From,
-- This is out of our pattern, should be <action>-Notice
Action = "Reassign-Name-Submit-Notice",
Action = "Reassign-Name-Notice",
Initiator = msg.From,
Name = name,
["Process-Id"] = antProcessIdToReassign,
})
end)

createActionHandler(ActionMap.CreateClaim, function(msg)
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["Process-Id"])
utils.validateArweaveId(msg.Tags.Recipient)
utils.validateUndername(msg.Tags.Name)

local name = string.lower(msg.Tags.Name)
local recipient = msg.Tags.Recipient
local ioProcess = msg.Tags["IO-Process-Id"]

-- send the release message to the provided IO Process Id
ao.send({
Target = ioProcess,
Action = "Create-Primary-Name-Claim",
Name = name,
Recipient = recipient,
})

ao.send({
Target = msg.From,
Action = "Create-Primary-Name-Claim-Notice",
Name = name,
Recipient = recipient,
})
end)

createActionHandler(ActionMap.RevokeClaims, function(msg)
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["Process-Id"])
utils.validateUndername(msg.Tags.Name)

local ioProcess = msg.Tags["IO-Process-Id"]
local names = msg.Tags.Names and utils.split(msg.Tags.Names, ",") or {}
for _, name in ipairs(names) do
utils.validateUndername(name)
end

-- send the release message to the provided IO Process Id
ao.send({
Target = ioProcess,
Action = "Revoke-Primary-Name-Claims",
Names = json.encode(names),
})

ao.send({
Target = msg.From,
Action = "Revoke-Primary-Name-Claims-Notice",
Names = json.encode(names),
})
end)

createActionHandler(ActionMap.RemovePrimaryNames, function(msg)
utils.validateOwner(msg.From)
utils.validateArweaveId(msg.Tags["Process-Id"])

local ioProcess = msg.Tags["IO-Process-Id"]
local names = msg.Tags.Names and utils.split(msg.Tags.Names, ",") or {}
for _, name in ipairs(names) do
utils.validateUndername(name)
end

--- if no names are provided, all primary names are removed for base name owned by this process

-- send the release message to the provided IO Process Id
ao.send({
Target = ioProcess,
Action = "Remove-Primary-Names",
Names = json.encode(names),
})

ao.send({
Target = msg.From,
Action = "Remove-Primary-Names-Notice",
Names = json.encode(names),
})
end)

Handlers.prepend(
camel(ActionMap.Evolve),
Handlers.utils.continue(utils.hasMatchingTag("Action", "Eval")),
Expand Down
8 changes: 8 additions & 0 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function utils.deepCopy(original)
return copy
end

--- @param str string
--- @param delimiter string
--- @description Splits a string by the delimiter
--- @return string[]
function utils.split(str, delimiter)
return { string.match(str, "([^" .. delimiter .. "]+)") }
end

-- NOTE: lua 5.3 has limited regex support, particularly for lookaheads and negative lookaheads or use of {n}
---@param name string
---@description Asserts that the provided name is a valid undername
Expand Down
5 changes: 2 additions & 3 deletions test/io.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ describe('IO Network Updates', async () => {
);
assert(ioProcessMessage, 'Message to IO Process not found');

// Check if there is a message to the sender with Action 'Reassign-Name-Submit-Notice'
// Check if there is a message to the sender with Action 'Reassign-Name-Notice'
const senderMessage = result.Messages.find(
(msg) =>
msg.Target === STUB_ADDRESS &&
msg.Tags.some(
(tag) =>
tag.name === 'Action' &&
tag.value === 'Reassign-Name-Submit-Notice',
tag.name === 'Action' && tag.value === 'Reassign-Name-Notice',
) &&
msg.Tags.some(
(tag) => tag.name === 'Initiator' && tag.value === STUB_ADDRESS,
Expand Down

0 comments on commit 7fa09a4

Please sign in to comment.