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 415f2e6 commit f497ae6
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions lib/rouge.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0

import { Buffer } from 'buffer'
import { utils, constants, ethers } from 'ethers'
import { ethers } from 'ethers'

import { TokenAmount } from 'erc-token-js'

Expand All @@ -27,29 +27,29 @@ export const NFT_LIMIT = 281474976710655
export const ALL_CHANNELS = 65535

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


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

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

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

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


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


// Make Class with asXXX as Qr stamp etc handler
export const calculateStampProof = async ({ contract, signer, secret, tokenId, channelId, salt, index }) => {
trace('[calculateStampProof IN]', { contract, signer, secret, tokenId, channelId, salt, index })
const signerAddress = await signer.getAddress()
const { chainId } = await signer.provider.getNetwork()
const secretHash = utils.id(`${signerAddress}:${secret}`)
const secretHash = ethers.id(`${signerAddress}:${secret}`)
if (tokenId) {
if (!contract) fatal('not enough element to calculate stamp proof')
void({ channelId } = await contract.getTokenInfos(tokenId))
Expand All @@ -93,13 +93,13 @@ export const calculateStampProof = async ({ contract, signer, secret, tokenId, c
trace('[calculateStampProof OUT]', { channelId, secretHash, salt, index })
// XXX trace wrong secret ?
// console.log('MAIN LIB', `${chainId}:${contract.address}:${channelId}:${secretHash}:${salt}:${index}`)
return utils.id(`${chainId}:${contract.address}:${channelId}:${secretHash}:${salt}:${index}`)
return ethers.id(`${chainId}:${contract.address}:${channelId}:${secretHash}:${salt}:${index}`)
}

export const encodeAnnotatedProof = ({ contract, bearer, tokenId, proof }) => {
trace('[encodeAnnotatedProofShort]', { contract, bearer, tokenId, proof })
// length is 6 + 20 + 20 + 32 = 78 bytes
const packed = utils.solidityPack(
const packed = ethers.solidityPack(
[ 'uint48', 'address', 'address', 'bytes32' ], [ tokenId, contract, bearer, proof ]
).slice(2).replace(/^(00)+/,'')
const b64 = Buffer.from(packed, 'hex').toString('base64')
Expand All @@ -108,17 +108,17 @@ export const encodeAnnotatedProof = ({ contract, bearer, tokenId, proof }) => {

export const decodeAnnotatedProof = data => {
const tokenId = parseInt('0x' + data.slice(0, 12), 16)
const contract = utils.getAddress('0x' + data.slice(12, 52))
const bearer = utils.getAddress('0x' + data.slice(52, 92))
const contract = ethers.getAddress('0x' + data.slice(12, 52))
const bearer = ethers.getAddress('0x' + data.slice(52, 92))
const proof = '0x' + data.slice(92, 156)
return { contract, bearer, tokenId, proof }
}

export const encodeAnnotatedCertificate = ({ contract, bearer, tokenId, signature, selector = '0x0', expire = 0}) => {
const { r, s, v } = utils.splitSignature(signature)
const { r, s, v } = ethers.splitSignature(signature)
trace('[encodeAnnotatedCertificate]', { contract, bearer, tokenId, signature, selector, expire })
// length is 6 + 20 + 20 + 4 + 4 + 1 + 32 + 32 = 119 bytes
const packed = utils.solidityPack(
const packed = ethers.solidityPack(
[ '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 All @@ -127,8 +127,8 @@ export const encodeAnnotatedCertificate = ({ contract, bearer, tokenId, signatur

export const decodeAnnotatedCertificate = data => {
const tokenId = parseInt('0x' + data.slice(0, 12), 16)
const contract = utils.getAddress('0x' + data.slice(12, 52))
const bearer = utils.getAddress('0x' + data.slice(52, 92))
const contract = ethers.getAddress('0x' + data.slice(12, 52))
const bearer = ethers.getAddress('0x' + data.slice(52, 92))
const expire = '0x' + data.slice(92, 100)
const selector = '0x' + data.slice(100, 108)
const v = '0x' + data.slice(108, 110)
Expand All @@ -150,7 +150,7 @@ export const abiEncodeAcquire = async ({ channels, signer, secret, contract, acq
if (acquisitions.length < 1) throw new Error('no acquisitions')
const signerAddress = await signer.getAddress()
const { chainId } = await signer.provider.getNetwork()
const secretHash = utils.id(`${signerAddress}:${secret}`)
const secretHash = ethers.id(`${signerAddress}:${secret}`)
// XXX get optionally salt from args ? => no since it could be manipulated to create custom metadata certificate
const salt = await signer.provider.getTransactionCount(signerAddress)
trace('[abiEncodeAcquire]', { signerAddress, chainId, secretHash, salt })
Expand All @@ -161,7 +161,7 @@ export const abiEncodeAcquire = async ({ channels, signer, secret, contract, acq
// XXX check uint8 qty, uint16 channelId ?
const stamps = []
for (let i = 1; i <= qty; i++) {
const proof = utils.id(`${chainId}:${contract.address}:${channelId}:${secretHash}:${salt}:${index}`)
const proof = ethers.id(`${chainId}:${contract.address}:${channelId}:${secretHash}:${salt}:${index}`)
trace('[abiEncodeAcquire]', { channelId, qty, index: i, proof })
stamps.push(getStamp(proof))
index++;
Expand Down Expand Up @@ -221,8 +221,8 @@ export const generateCertificate = async ({
}

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

Expand Down

0 comments on commit f497ae6

Please sign in to comment.