-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample.ts
More file actions
102 lines (87 loc) · 3.35 KB
/
sample.ts
File metadata and controls
102 lines (87 loc) · 3.35 KB
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
/* eslint-disable prefer-destructuring */
// // Intialize oreId
// const oreId = new OreId({
// appName: 'AI Market',
// appId: '{your appId}',
// apiKey: '{your api key}',
// })
// const newAccountParams = {
// accountType: 'native',
// idToken: 'eyJhbGci...', // idToken from OAuth provider
// userPassword: 'Password123!',
// // name: 'John Q Test', - optional: overrides value in token if desired
// // userName: 'jqtest', - optional: overrides value in token if desired
// // email: 'email@example.com', - optional: overrides value in token if desired
// }
// const { accountName } = await oreId.custodialNewAccount(newAccountParams)
// Store the accountName and userPassword along with user's account in your database
let errors: any
const showErrors = (newErrors: any) => {
errors = newErrors
console.log('errors:', newErrors)
}
export async function authExample() {
// Intialize oreId
const oreid = new OreId({ appId: '1234' })
// get web popup to show UX
const oreidWebPopUp = new OreidWebPopUp(oreid)
// popup login flow - when completed, oreid.auth object is populated with account and accessToken
await oreidWebPopUp.login({
data: { provider: 'google' },
onSuccess: (data: any) => {
console.log('user account logged-in:', data.account)
},
onError: showErrors,
})
if (errors) return // dont continue if errors during auth
const user = oreid.auth.user // expects auth to have logged in first (has accessToken)
await user.getData() // load user name, email, chain accounts, etc.
console.log('user logged in: ', user.info.name)
await oreid.auth.logout()
}
export async function signExample() {
// Intialize oreId
const oreid = new OreId({ appId: '1234', apiKey: 'key_123' })
// get web popup to show UX
const oreidWebPopUp = new OreidWebPopUp(oreid)
// Expects login to have been completed and auth set with valid accessToken
const user = oreid.auth.user
await user.getData()
// get the first ETH account in user's wallet
const ethAccount = user.data.chainAccounts.find(ca => ca.chainNetwork === 'eth_main')
console.log('Permission name to use to sign tx:', ethAccount.defaultPermission.name)
// Transaction class validates transaction, accounts, etc. and adds user's account param
const transaction = await oreid.createTransaction({
chainAccount: ethAccount,
chainNetwork: 'eth_main',
transaction: { to: '0x123...', amount: '.0001' },
signOptions: { broadcast: true },
})
if (await transaction.checkCanAutoSign()) {
const data = await transaction.autoSign()
console.log('transaction signed:', data.transactionId)
} else {
// popup sign flow - when completed, transaction info is returned (if requested in options)
await oreidWebPopUp.sign({
transaction, // just a JSON object - same used as with createTransaction() above
onSuccess: (data: any) => {
console.log('transaction signed:', data.transactionId)
},
onError: showErrors,
})
await oreidWebPopUp.sign({
signString: {
account: 'ore123...',
chainNetwork: 'eth_main',
chainAccount: '0x123...',
string: 'sign this',
// signMethod: 'ethereum.sign-typed-data',
},
onSuccess: (data: any) => {
console.log('transaction signed:', data.transactionId)
},
onError: showErrors,
})
}
await oreid.auth.logout()
}