Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token standard: 0x address and fix token.lua code #941

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua-examples/ao-standard-token/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ If there is an insufficient balance, then you will receive a `Transfer-Error` Me
}
```

If the Transfer's Recipient is a 0x address, it must be an [EIP55](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md) standard address, since 0x addresses on ao only support the EIP55 address format

### Mint

The Process itself can `mint` more `Points Coin`!
Expand Down
46 changes: 45 additions & 1 deletion lua-examples/ao-standard-token/token.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
them the Processes' balance
]] --
local json = require('json')
local crypto = require(".crypto.init")

--[[
Initialize State
Expand Down Expand Up @@ -72,14 +73,15 @@ end)
Balances
]] --
handlers.add('balances', handlers.utils.hasMatchingTag('Action', 'Balances'),
function(msg) ao.send({ Target = msg.From, Data = json.encode(Balances) }) end)
function(msg) ao.send({ Target = msg.From, Data = json.encode(Balances) }) end)

--[[
Transfer
]] --
handlers.add('transfer', handlers.utils.hasMatchingTag('Action', 'Transfer'), function(msg)
assert(type(msg.Tags.Recipient) == 'string', 'Recipient is required!')
assert(type(msg.Tags.Quantity) == 'string', 'Quantity is required!')
assert(verifyEccAddress(msg.Tags.Recipient) == true, 'Ecc recipient must be EIP55')

if not Balances[msg.From] then Balances[msg.From] = 0 end

Expand Down Expand Up @@ -137,3 +139,45 @@ handlers.add('mint', handlers.utils.hasMatchingTag('Action', 'Mint'), function(m
})
end
end)

function verifyEccAddress(address)
if isEthereumAddress(address) then
-- EIP-55
local mixedAddress = toChecksumAddress(address)
return mixedAddress == address
end
return true
end

function isEthereumAddress(address)
Copy link

@kunstmusik kunstmusik Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been using this:

function isEthereumAddress(address)
	return #address == 42 and string.match(address, "^0x[%x]+$") ~= nil
end

But I should perhaps add the type(address) == "string" to have:

function isEthereumAddress(address)
	return type(address) == "string" and #address == 42 and string.match(address, "^0x[%x]+$") ~= nil
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regular expression is more concise and my implementation is a bit more readable, I'm not very familiar with regulars, so the implementation is a bit more readable I guess

if type(address) == "string" and address:sub(1, 2) == "0x" and #address == 42 then
local isValid = true
for i = 3, #address do
local char = address:sub(i, i)
if not string.match(char, "%x") then
isValid = false
break
end
end
return isValid
else
return false
end
end

function toChecksumAddress(address)
address = string.lower(string.gsub(address, "^0x", ""))
local hash = crypto.digest.keccak256(address):asHex()
local ret = "0x"

for i = 1, #address do
local hashChar = tonumber(string.sub(hash, i, i), 16)
if hashChar >= 8 then
ret = ret .. string.upper(string.sub(address, i, i))
else
ret = ret .. string.sub(address, i, i)
end
end

return ret
end