-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.html
198 lines (172 loc) · 8.83 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Simple Signer Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script
src="https://cdnjs.cloudflare.com/ajax/libs/stellar-sdk/10.1.0/stellar-sdk.min.js"
integrity="sha512-EqNQsxKR6rZ5xKl29xXa+ez7xgtVSUpj9UDzZmTqoyF0wHbusLkrP8S7dOsKa9DmkoHbssoWUA4+n/0KYY1EAQ=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
</head>
<body>
<button onclick="openConnectWindow()">Connect</button>
<button onclick="openSignWindowUsingUrlParams()">Open sign via URL</button>
<button onclick="openSignWindowUsingPostMessage()">Open sign via postMessage</button>
<button onclick="openPaymentWindowUsingUrlParams()">Open payment via URL</button>
<button onclick="openPaymentWindowUsingPostMessage()">Open payment via postMessage</button>
<button onclick="openLogOutUsingPostMessage()">Log Out</button>
<script type="text/javascript">
const simpleSignerURL = 'https://sign.bigger.systems';
const sampleXdr =
'AAAAAgAAAADhqXT1t6e85DlUDyM5OzmJ2KPmujX8gegA027HvKSMpQAAAZAADGyCAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAQAAAADhqXT1t6e85DlUDyM5OzmJ2KPmujX8gegA027HvKSMpQAAAAAAAAAAAvrwgAAAAAAAAAABAAAAAOGpdPW3p7zkOVQPIzk7OYnYo+a6NfyB6ADTbse8pIylAAAAAAAAAAAC+vCAAAAAAAAAAAEAAAAA4al09benvOQ5VA8jOTs5idij5ro1/IHoANNux7ykjKUAAAAAAAAAAAL68IAAAAAAAAAAAQAAAADhqXT1t6e85DlUDyM5OzmJ2KPmujX8gegA027HvKSMpQAAAAAAAAAAAvrwgAAAAAAAAAAA';
const sampleIssuer = 'none';
const amount = '50';
const assetCode = 'native';
let signWindow;
let connectWindow;
let paymentWindow;
let logoutWindow;
function openConnectWindow() {
console.log('Opening connect window');
connectWindow = window.open(`${simpleSignerURL}/connect`, 'Connect_Window', 'width=360, height=450');
}
function openSignWindowUsingUrlParams() {
console.log('Opening sign window using URL params');
window.open(
`${simpleSignerURL}/sign/?xdr=${sampleXdr}&description=A sample description`,
'Sign_Window',
'width=360, height=700',
);
}
function openSignWindowUsingPostMessage() {
console.log('Opening sign window without URL params');
signWindow = window.open(`${simpleSignerURL}/sign`, 'Sign_Window', 'width=360, height=700');
}
function openPaymentWindowUsingUrlParams() {
console.log('Opening payment window using URL params');
window.open(
`${simpleSignerURL}/payment?receiver=${sampleReceiver}&issuer=${sampleIssuer}&amount=${amount}&assetCode=${assetCode}`,
'Payment_Window',
'width=360, height=700',
);
}
function openPaymentWindowUsingPostMessage() {
console.log('Opening payment window without URL params');
paymentWindow = window.open(`${simpleSignerURL}/payment`, 'Payment_Window', 'width=360, height=700');
}
function openLogOutUsingPostMessage() {
console.log('Opening log out window');
logoutWindow = window.open(`${simpleSignerURL}/logout`, 'Logout_Window', 'width=100, height=100');
}
async function handleMessage(e) {
if (e.origin !== `${simpleSignerURL}`) {
return;
}
const eventMessage = e.data;
console.log({ eventMessage });
console.log('Message from simple signer received: ', eventMessage);
if (eventMessage.type === 'onReady') {
if (eventMessage.page === 'connect') {
console.log('The connect page is ready');
}
if (eventMessage.page === 'sign') {
console.log('The sign page is ready');
}
if (eventMessage.page === 'payment') {
console.log('The payment page is ready');
}
if (signWindow) {
console.log('Sending parameters via postMessage sign');
signWindow.postMessage(
{
xdr: sampleXdr,
description: 'This is the transaction description',
operationGroups: [
{
from: 0,
to: 1,
title: 'This is an operation group title',
description: 'This is the description for operations 1 and 2',
},
],
},
simpleSignerURL,
);
}
if (paymentWindow) {
console.log('Sending parameters via postMessage payment');
paymentWindow.postMessage(
{
receiver: sampleReceiver,
issuer: sampleIssuer,
amount,
assetCode,
},
simpleSignerURL,
);
}
}
if (eventMessage.type === 'onConnect') {
const publicKey = eventMessage.message.publicKey;
// validate the public key
if (StellarSdk.Keypair.fromPublicKey(publicKey)) {
console.log('User connected with public key', publicKey);
}
}
if (eventMessage.type === 'onSign') {
const signedXdr = eventMessage.message.signedXDR;
// validate the transaction
if (StellarSdk.xdr.TransactionEnvelope.validateXDR(signedXdr, 'base64')) {
console.log('The user has signed the transaction, the result is', signedXdr);
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org/');
// Construct the transaction from the signedXDR
// see https://stellar.github.io/js-stellar-sdk/TransactionBuilder.html#.fromXDR
const transaction = StellarSdk.TransactionBuilder.fromXDR(
signedXdr,
'Test SDF Network ; September 2015',
);
try {
const transactionResult = await server.submitTransaction(transaction);
console.log(transactionResult);
} catch (err) {
console.error('This is expected to fail.', err);
}
}
}
if (eventMessage.type === 'onPayment') {
const receiver = eventMessage.message.receiver;
const amount = eventMessage.message.amount;
const assetCode = eventMessage.message.assetCode;
const issuer = eventMessage.message.issuer;
console.log(
'Received payment confirmation from Simple Signer:',
receiver,
amount,
assetCode,
issuer,
);
}
if (eventMessage.type === 'onLogOut') {
logoutWindow.postMessage({ type: 'logout' }, simpleSignerURL);
console.log('The user has been disconnected');
}
if (eventMessage.type === 'onCancel') {
if (eventMessage.page === 'connect') {
console.log('The user canceled the connect action');
}
if (eventMessage.page === 'sign') {
console.log('The user canceled the sign action');
}
if (eventMessage.page === 'payment') {
console.log('The user canceled the payment action');
}
}
}
window.addEventListener('message', handleMessage);
</script>
</body>
</html>