Skip to content

Instantly share code, notes, and snippets.

@critesjosh

critesjosh/lookup.js

Created Sep 28, 2020
Embed
What would you like to do?
A node script to run a phone number lookup with ODIS on the Celo blockchain
// In this example, we will go over how to look up a Celo address that is registered with a phone number with ODIS
// 1. Import the appropriate packages
const ContractKit = require('@celo/contractkit')
const OdisUtils = require('@celo/contractkit').OdisUtils
const AuthSigner = require('@celo/contractkit/lib/identity/odis/query').AuthSigner
// 2. Import these packages to help with private key management for the example
const privateKeyToAddress = require('@celo/utils/lib/address').privateKeyToAddress
const normalizeAddressWith0x = require('@celo/utils/lib/address').normalizeAddressWith0x
// 3. connect contractKit to mainnet via Forno
const contractKit = ContractKit.newKit('https://rc1-forno.celo-testnet.org')
// 4. Specify the authentication method and contractKit instance
const authSigner = {
authenticationMethod: OdisUtils.Query.AuthenticationMethod.WALLET_KEY,
contractKit: contractKit
}
// 5. Set a phone number to lookup
let phoneNumber = '+11234567890' // this is a fake number
let context = 'mainnet'
// 6. You will need a private key from which to query ODIS. The address associated with this private key
// will be the account from which the query is made. The account must have a small balance (0.005 CELO or 0.01 cUSD).
const privateKey = '3a26ebc37944c305670a21ea6a5d16c1084db2f18bc6635ba95f487e5c59868f'
const from = normalizeAddressWith0x(privateKeyToAddress(privateKey))
// 7. Import the account to contractKit to sign the query request
contractKit.addAccount(privateKey)
// 8. Wrap the async functions so we can await the responses
let run = async function(){
// 9. Make the request to ODIS
const res = await OdisUtils.PhoneNumberIdentifier.getPhoneNumberIdentifier(
phoneNumber,
from,
authSigner,
OdisUtils.Query.getServiceContext(context)
)
console.log(res)
/*
{ e164Number: '+11234567890',
phoneHash:
'0xd5b4028307ee557404bc6819790326dc0194cfc62c0ae5adcd79adb25da0bae8',
pepper: '+8swDgOD5m138' }
*/
// 10. Look up the address registered to the returned phoneHash
const attesationsContract = await contractKit.contracts.getAttestations()
let mapping = await attesationsContract.lookupIdentifiers([res.phoneHash])
console.log(mapping)
/*
{ '0xd5b4028307ee557404bc6819790326dc0194cfc62c0ae5adcd79adb25da0bae8':
{ '0xDcD7335735F2c4bC7228E3d59D3D05e69Bb73809': { completed: 3, total: 4 },
'0xE609135E96aA3424c05e940A6D2693d674bc9fDD': { completed: 3, total: 3 } } }
*/
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment