Repository navigation
Expand file tree
/
Copy pathpromise-fancy.html
More file actions
204 lines (179 loc) · 8.09 KB
/
Copy pathpromise-fancy.html
File metadata and controls
204 lines (179 loc) · 8.09 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Xumm client side only integration (OAuth2 PKCE Sample)</title>
<link rel="icon" type="image/x-icon" href="https://xumm.app/favicon.ico">
<script>
// To see logging/debug messages in your console
localStorage.debug = '*'
</script>
<!--
NPM (JS/TS): https://www.npmjs.com/package/xumm-oauth2-pkce
CDN: <script src="https://xumm.app/assets/cdn/xumm-oauth2-pkce.min.js"></script>
-->
<script src="https://xumm.app/assets/cdn/xumm-oauth2-pkce.min.js?v=2.4.0"></script>
</head>
<body>
<div class="container mt-2 pb-4">
<h2>Hello, world!</h2>
<p class="alert alert-primary mb-2">
<span class="h5 d-inline-block mb-2">This is <q>
<b>Web3</b> wallet <b>identity</b> & <b>transaction initiation</b>
</q> for the XRP Ledger ecosystem.</span>
<br/>
Tech under the hood: OAuth2 Implicit PKCE flow for the Xumm platform and XRP Ledger ecosystem.
<b>
Using nothing but simple client side Javascript:
<a href="https://github.com/XRPL-Labs/XummPkce/blob/main/sample/index.html" target="_blank">source (this page)</a>
or
<a href="https://github.com/XRPL-Labs/XummPkce/blob/main/sample/jsmodule.html" target="_blank">event based sample</a>
</b>.
</p>
<div class="text-end py-0 my-0">
<div class="float-start pt-0">
<code class="text-dark bg-white">SDK:</code>
<a class="me-3" target="_blank" href="https://badge.fury.io/js/xumm-oauth2-pkce">
<span aria-label="npm version" class="img" role="button" tabindex="0"><img src="https://badge.fury.io/js/xumm-oauth2-pkce.svg" alt="npm version" align="" caption="" height="auto" title="" width="auto" loading="lazy"></span></a>
</div>
<a href="https://github.com/XRPL-Labs/XummPkce/blob/main/sample/promise-fancy.html" target="_blank" class="mt-2 py-1 btn btn-primary">Source (this page)</a>
<a href="https://xumm.readme.io" target="_blank" class="mt-2 ms-2 py-1 btn btn-outline-primary">Xumm Developer Docs</a>
</div>
<div class="mt-4 card shadow">
<div class="card-body">
<div class="py-3 text-center">
<div class="alert alert-danger" id="error">...</div>
<button id="signinbtn" onclick="go()" class="btn btn-outline-primary">...</button>
<div class="alert alert-success" id="signedin">Signed in :)</div>
<pre style="text-align: left; display: none;" id="results">...</pre>
<button style="display: none; float: right;" id="logout" onclick="go_logout()" class="btn btn-danger">Logout</button>
<button style="display: none;" id="trypayload" onclick="go_payload()" class="btn btn-primary">Now try a Sign Request</button>
</div>
</div>
</div>
<div class="mt-5 pt-2 pb-2 border-top border-1 border-muted text-end">
<small>
Samples:
<a href="https://github.com/XRPL-Labs/XummPkce/blob/main/sample/promise-fancy.html" target="_blank">Source on Github</a> /
<a href="https://github.com/XRPL-Labs/XummPkce/blob/main/sample/jsmodule.html" target="_blank">Event-based</a> /
<a href="https://github.com/XRPL-Labs/XummPkce/blob/main/sample/jsmodule-payload.html" target="_blank">Event-based & payload</a>
</small>
</div>
</div>
<script>
/**
* Construct & handle async (mobile)
**/
var auth = new XummPkce('47d328db-0b34-4451-a258-393480c9b4cd')
var sdk = null
function signedInHandler (authorized) {
// Assign to global,
// please don't do this but for the sake of the demo it's easy
sdk = authorized.sdk
console.log('Authorized', /* authorized.jwt, */ authorized.me)
signinbtn.style.display = 'none'
document.getElementById('signedin').style.display = 'block'
var resultspre = document.getElementById('results')
resultspre.style.display = 'block'
resultspre.innerText = JSON.stringify(authorized.me, null, 2)
document.getElementById('trypayload').style.display = 'block'
document.getElementById('logout').style.display = 'block'
sdk.ping().then(pong => console.log({pong}))
}
auth.on('error', error => {
console.log('error', error)
})
auth.on('success', async () => {
console.log('success')
auth.state().then(state => {
if (state.me) {
console.log('success, me', JSON.stringify(state.me))
}
})
})
auth.on('retrieved', async () => {
// Redirect, e.g. mobile. Mobile may return to new tab, this
// must retrieve the state and process it like normally the authorize method
// would do
console.log('Results are in, mobile flow, process sign in')
auth.state().then(state => {
console.log(state)
if (state) {
console.log('retrieved, me:', JSON.stringify(state.me))
signedInHandler(state)
}
})
})
/**
* UI stuff
**/
var signinbtn = document.getElementById('signinbtn')
function reset() {
signinbtn.innerText = 'Sign in'
document.getElementById('signedin').style.display = 'none'
document.getElementById('error').style.display = 'none'
document.getElementById('trypayload').style.display = 'none'
document.getElementById('logout').style.display = 'none'
document.getElementById('results').style.display = 'none'
}
// Start in default UI state
reset()
/**
* Fn to deal with a "Sign In" button click or redirect
**/
function go() {
reset()
signinbtn.innerText = 'Signing in...'
return auth.authorize().then(signedInHandler).catch(e => {
console.log('Auth error', e)
reset()
document.getElementById('error').style.display = 'block'
document.getElementById('error').innerText = e.message
})
}
function go_logout() {
auth.logout()
reset()
signinbtn.style.display = 'inline-block'
}
function go_payload() {
/**
* xumm-oauth2-pkce package returns `sdk` property,
* allowing access to the Xumm SDK (`xumm-sdk`) package.
* Xumm SDK methods, docs:
* https://www.npmjs.com/package/xumm-sdk
**/
var payload = {
txjson: {
TransactionType: 'Payment',
Destination: 'rfCarbonVNTuXckX6x2qTMFmFSnm6dEWGX',
Amount: '1337' // Drops, so: 0.001337 XRP
}
}
sdk
.payload
.createAndSubscribe(payload, function (payloadEvent) {
if (typeof payloadEvent.data.signed !== 'undefined') {
// What we return here will be the resolved value of the `resolved` property
return payloadEvent.data
}
})
.then(function ({created, resolved}) {
alert(created.pushed
? 'Now check Xumm, there should be a push notification + sign request in your event list waiting for you ;)'
: 'Now check Xumm, there should be a sign request in your event list waiting for you ;) (This would have been pushed, but it seems you did not grant Xumm the push permission)'
)
resolved.then(function (payloadOutcome) {
alert('Payload ' + (payloadOutcome.signed ? 'signed (TX Hash: ' + payloadOutcome.txid + ')' : 'rejected') + ', see the browser console for more info')
console.log(payloadOutcome)
})
})
.catch(function (payloadError) {
alert('Paylaod error', e.message)
})
}
</script>
</body>
</html>