/
userSignUp.ts
82 lines (68 loc) · 2.4 KB
/
userSignUp.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
import { ethers } from 'ethers'
import base64url from 'base64url'
import { Unirep, UnirepFactory } from '@unirep/contracts'
import { DEFAULT_ETH_PROVIDER } from './defaults'
import { identityCommitmentPrefix } from './prefix'
import { getProvider } from './utils'
const configureSubparser = (subparsers: any) => {
const parser = subparsers.add_parser('userSignUp', { add_help: true })
parser.add_argument('-e', '--eth-provider', {
action: 'store',
type: 'str',
help: `A connection string to an Ethereum provider. Default: ${DEFAULT_ETH_PROVIDER}`,
})
parser.add_argument('-c', '--identity-commitment', {
required: true,
type: 'str',
help: "The user's identity commitment (in hex representation)",
})
parser.add_argument('-x', '--contract', {
required: true,
type: 'str',
help: 'The Unirep contract address',
})
parser.add_argument('-d', '--eth-privkey', {
required: true,
action: 'store',
type: 'str',
help: "The user's Ethereum private key",
})
parser.add_argument('-a', '--airdrop', {
type: 'int',
help: 'The requested airdrop amount',
})
}
const userSignUp = async (args: any) => {
// Ethereum provider
const ethProvider = args.eth_provider ?? DEFAULT_ETH_PROVIDER
const provider = getProvider(ethProvider)
// Unirep contract
const unirepContract: Unirep = UnirepFactory.connect(
args.contract,
provider
)
// Connect a signer
const wallet = new ethers.Wallet(args.eth_privkey, provider)
// Parse identity commitment
const encodedCommitment = args.identity_commitment.slice(
identityCommitmentPrefix.length
)
const airdropAmount = args.airdrop ?? 0
const decodedCommitment = base64url.decode(encodedCommitment)
const commitment = decodedCommitment
// Submit the user sign up transaction
let tx: ethers.ContractTransaction
try {
tx = await unirepContract
.connect(wallet)
['userSignUp(uint256,uint256)'](BigInt(commitment), airdropAmount)
await tx.wait()
} catch (error) {
console.log('Transaction Error', error)
return
}
const epoch = await unirepContract.currentEpoch()
console.log('Transaction hash:', tx.hash)
console.log('Sign up epoch:', epoch.toString())
}
export { userSignUp, configureSubparser }