2018-08-27 05:22:10 -07:00
|
|
|
import helpers from './helpers'
|
2019-01-07 10:43:51 -08:00
|
|
|
import { constants } from '../utils/constants'
|
2018-08-14 13:28:48 -07:00
|
|
|
|
2018-09-06 08:24:34 -07:00
|
|
|
const REGISTER_ADDRESS_EVENT_NAME = 'LogAddressRegistered'
|
2018-08-28 12:42:52 -07:00
|
|
|
|
2018-08-14 13:28:48 -07:00
|
|
|
export default class ProofOfPhysicalAddress {
|
2018-08-27 05:22:10 -07:00
|
|
|
async init({ web3, netId, addresses }) {
|
|
|
|
const { PROOF_OF_PHYSICAL_ADDRESS } = addresses
|
2018-10-10 08:55:10 -07:00
|
|
|
const branch = constants.NETWORKS[netId].BRANCH
|
2019-01-17 05:41:04 -08:00
|
|
|
|
|
|
|
if (branch !== constants.branches.CORE) {
|
2018-09-06 08:24:34 -07:00
|
|
|
throw new Error(`ProofOfPhysicalAddress contract not deployed on network "${branch}"`)
|
2018-08-28 17:58:21 -07:00
|
|
|
}
|
2019-01-17 05:41:04 -08:00
|
|
|
|
2018-10-10 08:55:10 -07:00
|
|
|
const proofOfPhysicalAddressAbi = await helpers.getABI(branch, 'ProofOfPhysicalAddress')
|
2018-10-11 00:08:49 -07:00
|
|
|
this.instance = new web3.eth.Contract(proofOfPhysicalAddressAbi, PROOF_OF_PHYSICAL_ADDRESS)
|
2018-08-14 13:28:48 -07:00
|
|
|
|
2018-10-02 08:08:02 -07:00
|
|
|
this.getPhysicalAddressesOfWalletAddress = this.getPhysicalAddressesOfWalletAddress.bind(this)
|
2018-08-28 12:42:52 -07:00
|
|
|
this.getAllEvents = this.getAllEvents.bind(this)
|
2018-10-02 08:08:02 -07:00
|
|
|
this.getAllAddressRegisteredEvents = this.getAllAddressRegisteredEvents.bind(this)
|
2018-08-30 17:02:08 -07:00
|
|
|
this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray = this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray.bind(
|
2018-08-29 09:31:52 -07:00
|
|
|
this
|
|
|
|
)
|
2018-08-14 13:28:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-02 08:08:02 -07:00
|
|
|
* Given a wallet address, return a promise that resolves to an array of physical addresses from PoPA contract or null.
|
|
|
|
* If a cached registeredAddressEvents is provided, it will be used to get the corresponding keccakIdentifiers from it.
|
|
|
|
* @param {String} walletAddress
|
|
|
|
* @param {Object[]} registeredAddressEvents (optional)
|
2018-08-14 13:28:48 -07:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2018-10-02 08:08:02 -07:00
|
|
|
async getPhysicalAddressesOfWalletAddress(walletAddress, registeredAddressEvents) {
|
|
|
|
let result = null
|
2018-08-14 13:28:48 -07:00
|
|
|
try {
|
2018-10-02 08:08:02 -07:00
|
|
|
if (!registeredAddressEvents) {
|
|
|
|
registeredAddressEvents = await this.getAllEvents(REGISTER_ADDRESS_EVENT_NAME)
|
|
|
|
}
|
|
|
|
|
|
|
|
let keccakIdentifiers = registeredAddressEvents
|
|
|
|
.filter(event => event.returnValues.wallet === walletAddress)
|
|
|
|
.map(event => event.returnValues.keccakIdentifier)
|
2018-09-06 08:24:34 -07:00
|
|
|
|
2018-10-02 08:08:02 -07:00
|
|
|
if (keccakIdentifiers.length > 0) {
|
|
|
|
let physicalAddresses = await this.getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray(
|
|
|
|
walletAddress,
|
|
|
|
keccakIdentifiers
|
|
|
|
)
|
|
|
|
result = physicalAddresses.length > 0 ? physicalAddresses : result
|
|
|
|
}
|
2018-08-14 13:28:48 -07:00
|
|
|
} catch (e) {
|
2018-10-02 08:08:02 -07:00
|
|
|
console.error(`Error in getPhysicalAddressesOfWalletAddress`, e)
|
2018-08-14 13:28:48 -07:00
|
|
|
}
|
2018-09-06 08:24:34 -07:00
|
|
|
return result
|
2018-08-14 13:28:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-29 09:31:52 -07:00
|
|
|
* Given a walletAddress and an array of keccakIdentifiers, return a promise that resolves to an array
|
2018-09-11 11:49:43 -07:00
|
|
|
* of the corresponding confirmed and unconfirmed physical addresses, or an empty array (unregistered
|
|
|
|
* addresses are not included).
|
2018-08-29 09:31:52 -07:00
|
|
|
* @param {String} walletAddress
|
|
|
|
* @param {String[]} keccakIdentifierArray
|
2018-08-14 13:28:48 -07:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2018-08-30 17:02:08 -07:00
|
|
|
async getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray(walletAddress, keccakIdentifierArray) {
|
2018-08-29 09:31:52 -07:00
|
|
|
let promises = keccakIdentifierArray.map(keccakIdentifier => {
|
|
|
|
return this.instance.methods
|
|
|
|
.userAddressByKeccakIdentifier(walletAddress, keccakIdentifier)
|
|
|
|
.call()
|
2018-08-30 17:02:08 -07:00
|
|
|
.then(async addressStatusTuple => {
|
2018-08-29 09:31:52 -07:00
|
|
|
const addressFound = addressStatusTuple[0]
|
|
|
|
const addressIndex = addressStatusTuple[1]
|
|
|
|
const addressConfirmed = addressStatusTuple[2]
|
2018-09-11 11:49:43 -07:00
|
|
|
if (addressFound === false) {
|
2018-08-29 09:31:52 -07:00
|
|
|
return null
|
|
|
|
} else {
|
2018-08-30 17:02:08 -07:00
|
|
|
return {
|
|
|
|
isConfirmed: addressConfirmed,
|
|
|
|
data: await this.instance.methods.userAddress(walletAddress, addressIndex).call()
|
|
|
|
}
|
2018-08-29 09:31:52 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2018-08-30 17:02:08 -07:00
|
|
|
console.error(`Error in getPhysicalAddressesByWalletAddressAndKeccakIdentifierArray`, e)
|
2018-08-29 09:31:52 -07:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
let physicalAddressArray = await Promise.all(promises)
|
|
|
|
return physicalAddressArray.filter(address => address !== null)
|
2018-08-14 13:28:48 -07:00
|
|
|
}
|
2018-08-28 12:42:52 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all event objects, with the given eventName.
|
|
|
|
* @param {String} eventName
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
async getAllEvents(eventName) {
|
|
|
|
let result = []
|
|
|
|
try {
|
|
|
|
result = await this.instance.getPastEvents(eventName, { fromBlock: 0, toBlock: 'latest' })
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2018-10-02 08:08:02 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all addressRegistered event objects.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
async getAllAddressRegisteredEvents() {
|
|
|
|
return this.getAllEvents(REGISTER_ADDRESS_EVENT_NAME)
|
|
|
|
}
|
2018-08-14 13:28:48 -07:00
|
|
|
}
|