-
Notifications
You must be signed in to change notification settings - Fork 8
/
contractKit.ts
128 lines (111 loc) · 4.56 KB
/
contractKit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { OdisUtils } from '@celo/identity'
import { AuthSigner, OdisContextName, ServiceContext } from '@celo/identity/lib/odis/query'
import { ContractKit, newKit } from '@celo/contractkit'
import { Account } from 'web3-core'
const ALFAJORES_RPC = 'https://alfajores-forno.celo-testnet.org'
const ISSUER_PRIVATE_KEY = '0x199abda8320f5af0bb51429d246a4e537d1c85fbfaa30d52f9b34df381bd3a95'
class ASv2 {
kit: ContractKit
issuer: Account
authSigner: AuthSigner
serviceContext: ServiceContext
constructor(kit: ContractKit) {
this.kit = kit
this.issuer = kit.web3.eth.accounts.privateKeyToAccount(ISSUER_PRIVATE_KEY)
this.kit.addAccount(ISSUER_PRIVATE_KEY)
this.kit.defaultAccount = this.issuer.address
this.serviceContext = OdisUtils.Query.getServiceContext(OdisContextName.ALFAJORES)
this.authSigner = {
authenticationMethod: OdisUtils.Query.AuthenticationMethod.WALLET_KEY,
contractKit: this.kit,
}
}
async registerAttestation(phoneNumber: string, account: string, attestationIssuedTime: number) {
await this.checkAndTopUpODISQuota()
// get identifier from phone number using ODIS
const { obfuscatedIdentifier } = await OdisUtils.Identifier.getObfuscatedIdentifier(
phoneNumber,
OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER,
this.issuer.address,
this.authSigner,
this.serviceContext,
)
const federatedAttestationsContract = await this.kit.contracts.getFederatedAttestations()
// upload identifier <-> address mapping to onchain registry
await federatedAttestationsContract
.registerAttestationAsIssuer(obfuscatedIdentifier, account, attestationIssuedTime)
.send()
}
async lookupAddresses(phoneNumber: string) {
// get identifier from phone number using ODIS
const { obfuscatedIdentifier } = await OdisUtils.Identifier.getObfuscatedIdentifier(
phoneNumber,
OdisUtils.Identifier.IdentifierPrefix.PHONE_NUMBER,
this.issuer.address,
this.authSigner,
this.serviceContext,
)
const federatedAttestationsContract = await this.kit.contracts.getFederatedAttestations()
// query on-chain mappings
const attestations = await federatedAttestationsContract.lookupAttestations(
obfuscatedIdentifier,
[this.issuer.address],
)
return attestations.accounts
}
private async checkAndTopUpODISQuota() {
//check remaining quota
const { remainingQuota } = await OdisUtils.Quota.getPnpQuotaStatus(
this.issuer.address,
this.authSigner,
this.serviceContext,
)
console.log('remaining ODIS quota', remainingQuota)
if (remainingQuota < 1) {
const stableTokenContract = await this.kit.contracts.getStableToken()
const odisPaymentsContract = await this.kit.contracts.getOdisPayments()
// give odis payment contract permission to use cUSD
const currentAllowance = await stableTokenContract.allowance(
this.issuer.address,
odisPaymentsContract.address,
)
console.log('current allowance:', currentAllowance.toString())
let enoughAllowance: boolean = false
const ONE_CENT_CUSD_WEI = this.kit.web3.utils.toWei('0.01', 'ether')
if (currentAllowance.lt(ONE_CENT_CUSD_WEI)) {
const approvalTxReceipt = await stableTokenContract
.increaseAllowance(odisPaymentsContract.address, ONE_CENT_CUSD_WEI)
.sendAndWaitForReceipt()
console.log('approval status', approvalTxReceipt.status)
enoughAllowance = approvalTxReceipt.status
} else {
enoughAllowance = true
}
// increase quota
if (enoughAllowance) {
const odisPayment = await odisPaymentsContract
.payInCUSD(this.issuer.address, ONE_CENT_CUSD_WEI)
.sendAndWaitForReceipt()
console.log('odis payment tx status:', odisPayment.status)
console.log('odis payment tx hash:', odisPayment.transactionHash)
} else {
throw 'cUSD approval failed'
}
}
}
}
;(async () => {
const kit = await newKit(ALFAJORES_RPC)
const asv2 = new ASv2(kit)
const userAccount = '0xf14790BAdd2638cECB5e885fc7fAD1b6660AAc34'
const userPhoneNumber = '+18009099999'
const timeAttestationWasVerified = Math.floor(new Date().getTime() / 1000)
try {
await asv2.registerAttestation(userPhoneNumber, userAccount, timeAttestationWasVerified)
console.log('attestation registered')
} catch (err) {
// mostly likely reason registering would fail is if this issuer has already
// registered a mapping between this number and account
}
console.log(await asv2.lookupAddresses(userPhoneNumber))
})()