Skip to content

Commit

Permalink
ethers6 lib migration
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Apr 18, 2023
1 parent 49baa19 commit 9e3693a
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/rouge.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,35 @@ export const setLogger = logger => {
void({ fatal, error, warn, info, debug, trace } = logger)
}


// TODO lookup for contracts...

export const NFT_LIMIT = 281474976710655
export const ALL_CHANNELS = 65535

export const tokens = {
'ETH': ethers.AddressZero,
'USDC': ethers.AddressZero,
'ETH': ethers.ZeroAddress,
'USDC': ethers.ZeroAddress,
}


export const abiEncodeChannel = ({
supply = NFT_LIMIT,
amount = '0', // uint256
token = ethers.AddressZero,
token = ethers.ZeroAddress,
free
} = {}) => {

if (free) {
amount = '0'
token = ethers.AddressZero
token = ethers.ZeroAddress
}

if (amount instanceof TokenAmount) {
token = amount.token.address || ethers.AddressZero
token = amount.token.address || ethers.ZeroAddress
amount = amount.valueOf()
} else if (typeof token === 'object') {
// old format to remove ...
token = token.address || ethers.AddressZero
token = token.address || ethers.ZeroAddress
}

return [ supply, 0, 0, token, amount ]
Expand All @@ -60,22 +59,22 @@ export const abiEncodeChannel = ({
// { scope: 'acquire', hasRole: [ /*addresses_lists*/ ] }
export const abiEncodeAuth = ({
iface,
address = ethers.AddressZero,
address = ethers.ZeroAddress,
scope,
channels = [ ALL_CHANNELS ],
grant = true
} = {}) => {

return [
address,
/^0x/.test(scope) ? scope : iface.getSighash(scope),
/^0x/.test(scope) ? scope : iface.getFunction(scope).selector,
channels,
grant
]
}


export const getStamp = proof => ethers.keccak256(ethers.solidityPack(['bytes32'],[proof])).slice(0,34)
export const getStamp = proof => ethers.keccak256(ethers.solidityPacked(['bytes32'],[proof])).slice(0,34)


// Make Class with asXXX as Qr stamp etc handler
Expand All @@ -99,7 +98,7 @@ export const calculateStampProof = async ({ contract, signer, secret, tokenId, c
export const encodeAnnotatedProof = ({ contract, bearer, tokenId, proof }) => {
trace('[encodeAnnotatedProofShort]', { contract, bearer, tokenId, proof })
// length is 6 + 20 + 20 + 32 = 78 bytes
const packed = ethers.solidityPack(
const packed = ethers.solidityPacked(
[ 'uint48', 'address', 'address', 'bytes32' ], [ tokenId, contract, bearer, proof ]
).slice(2).replace(/^(00)+/,'')
const b64 = Buffer.from(packed, 'hex').toString('base64')
Expand All @@ -115,10 +114,10 @@ export const decodeAnnotatedProof = data => {
}

export const encodeAnnotatedCertificate = ({ contract, bearer, tokenId, signature, selector = '0x0', expire = 0}) => {
const { r, s, v } = ethers.splitSignature(signature)
const { r, s, v } = ethers.Signature.from(signature)
trace('[encodeAnnotatedCertificate]', { contract, bearer, tokenId, signature, selector, expire })
// length is 6 + 20 + 20 + 4 + 4 + 1 + 32 + 32 = 119 bytes
const packed = ethers.solidityPack(
const packed = ethers.solidityPacked(
[ 'uint48', 'address', 'address', 'uint32', 'bytes4', 'uint8', 'bytes32', 'bytes32' ], [ tokenId, contract, bearer, expire, selector, v, r, s ]
).slice(2).replace(/^(00)+/,'')
const b64 = Buffer.from(packed, 'hex').toString('base64')
Expand Down Expand Up @@ -221,14 +220,14 @@ export const generateCertificate = async ({
}

export const abiEncodeCertificate = ({ from, tokenId, selector, expire, signature, r, s, v } = {}) => {
if (!signature && !r) return [ ethers.AddressZero, 0, [ '0x00000000', 0 ], ethers.HashZero, ethers.HashZero, 27 ]
if (signature && !r) void({ r, s, v } = ethers.splitSignature(signature))
if (!signature && !r) return [ ethers.ZeroAddress, 0, [ '0x00000000', 0 ], ethers.ZeroHash, ethers.ZeroHash, 27 ]
if (signature && !r) void({ r, s, v } = ethers.Signature.from(signature))
return [ from, tokenId, [ selector, expire ], r, s, v ]
}

export const decodeRoles = async (contract, selectors, account, channels) => {
const raw = await contract.getRoles(
selectors.map(s => contract.interface.getSighash(s)),
selectors.map(s => contract.interface.getFunction(s).selector,
account
)
return raw.map(c => {
Expand Down

0 comments on commit 9e3693a

Please sign in to comment.