Skip to content

Commit 4eeb15c

Browse files
committed
Added scatter desktop support.
1 parent fb259bc commit 4eeb15c

File tree

6 files changed

+98
-32
lines changed

6 files changed

+98
-32
lines changed

example/example/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,5 @@
131131

132132
STATIC_URL = '/static/'
133133
LOGIN_REDIRECT_URL = '/'
134+
SCATTERAUTH_USER_SIGNUP_FIELDS = []
135+
SCATTERAUTH_DOMAIN = 'www.baf.ru'

example/example/urls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121

2222
def auto_login(request):
23+
from django.conf import settings
2324
if not request.user.is_authenticated:
24-
return render(request, 'scatterauth/autologin.html')
25+
return render(request, 'scatterauth/autologin.html', context={'settings': settings})
2526
else:
2627
return redirect('/')
2728

example/templates/scatterauth/autologin.html

+28-15
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,40 @@
22
{% block content %}
33
<div class="row m-t-2">
44
<div class="container">
5-
<h4 class="col-md-12 text-center" style="margin-top: 15px" id='container-text'>Click at any menu item to interact
6-
with contract</h4>
5+
<h4 class="col-md-12 text-center" style="margin-top: 15px" id='container-text'>
6+
<button onclick="login()">Login with a scatter</button>
7+
</h4>
78
</div>
89
</div>
910
{% endblock content %}
1011
{% block javascript %}
1112
{{ block.super }}
13+
1214
<script>
13-
var login_url = '{% url 'scatterauth_login_api' %}';
14-
document.addEventListener('scatterLoaded', scatterExtension => {
15-
console.log('scatter loaded');
16-
if (scatter.identity) {
17-
console.log("Identity found");
18-
loginWithAuthenticate(login_url,console.log,console.log,console.log,console.log, function (resp) {
19-
window.location.replace(resp.redirect_url);
20-
});
21-
document.getElementById('container-text').innerHTML = "Welcome, " + scatter.identity.name + '<br>Your public key: ' + scatter.identity.publicKey
22-
} else {
23-
console.log('identity not found, have to signup');
24-
document.getElementById('container-text').innerHTML = "Please sign up first"
15+
async function login(){
16+
const network = ScatterJS.Network.fromJson({
17+
blockchain: 'eos',
18+
chainId: 'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473',
19+
host: 'jungle2.cryptolions.io',
20+
port: 80,
21+
protocol: 'http'
22+
});
23+
var login_url = '{% url 'scatterauth_login_api' %}';
24+
var text_to_sign = "{{ settings.SCATTERAUTH_DOMAIN }}";
25+
26+
connected = await ScatterJS.connect('baf', {network});
27+
console.log('connected: ', connected);
28+
let scatter = ScatterJS.scatter;
29+
if (connected){
30+
await loginWithAuthenticate(
31+
login_url,
32+
text_to_sign,
33+
alert,
34+
alert,
35+
alert,
36+
response => alert(response.error),
37+
response => document.location=response.redirect_url)
2538
}
26-
});
39+
}
2740
</script>
2841
{% endblock javascript %}

example/templates/scatterauth/base.html

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<!--[if lt IE 9]>
1414
<script src="https://html5shim.googlecode.com/svn/trunk/html5.js"></script>
1515
<![endif]-->
16+
<script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-core.min.js"></script>
17+
<script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-plugin-eosjs.min.js"></script>
18+
<script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-plugin-lynx.min.js"></script>
1619

1720
{% block css %}
1821
<!-- Latest compiled and minified Bootstrap 4 beta CSS -->

example/templates/scatterauth/signup.html

+56-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{ form.as_p }}
77
<button type="submit">Sign Up</button>
88
</form>
9+
<button onclick="signup2()">Sigup with scatter</button>
910
</div>
1011
{% endblock content %}
1112
{% block javascript %}
@@ -27,20 +28,61 @@
2728
chainId: CHAIN_ID
2829
};
2930
*/
30-
var signup = '{% url 'scatterauth_signup_api' %}';
31-
var fields = ['email'];
32-
var public_key_field_name = 'username';
31+
function signup2() {
32+
const network = ScatterJS.Network.fromJson({
33+
blockchain: 'eos',
34+
chainId: 'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473',
35+
host: 'jungle2.cryptolions.io',
36+
port: 80,
37+
protocol: 'http'
38+
});
39+
40+
const signup_url = '{% url 'scatterauth_signup_api' %}';
41+
const fields = [];
42+
const public_key_field_name = 'username';
43+
44+
ScatterJS.connect('baf', network).then(
45+
connected => {
46+
if (connected) {
47+
let scatter = ScatterJS.scatter;
48+
console.log("Scatter installed!");
49+
console.log(scatter.identity)
50+
if (scatter.identity) {
51+
// login the user with api
52+
console.log("Identity found");
53+
console.log(scatter.identity);
54+
console.log("Welcome, " + scatter.identity.name + '<br>Your public key: ' + scatter.identity.publicKey);
55+
signupWithData(
56+
scatter.identity.publicKey,
57+
public_key_field_name,
58+
null,
59+
signup_url,
60+
x=>console.log(x),
61+
response => {document.location = response.redirect_url},
62+
response => alert(JSON.stringify(response.error))
63+
)
64+
} else {
65+
requestIdentity(
66+
fields,
67+
public_key_field_name,
68+
signup_url,
69+
network,
70+
console.log
71+
)
72+
}
73+
console.log(x);
74+
if(x.success){
75+
document.location = result.redirect_url;
76+
} else {
77+
alert(x.error);
78+
}
79+
}
80+
else {
81+
alert('not connected');
82+
}
83+
}
84+
);
85+
}
3386

34-
document.addEventListener('scatterLoaded', scatterExtension => {
35-
console.log("Scatter installed!");
36-
if (scatter.identity) {
37-
// login the user with api
38-
console.log("Identity found");
39-
console.log(scatter.identity);
40-
console.log("Welcome, " + scatter.identity.name + '<br>Your public key: ' + scatter.identity.publicKey);
41-
} else {
42-
requestIdentity(fields,public_key_field_name, signup, null, console.log);
43-
}
44-
});
4587
</script>
4688
{% endblock javascript %}

scatterauth/static/scatterauth/js/scatterauth.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ function getCookie(name) {
2121
return cookieValue;
2222
}
2323

24-
function loginWithAuthenticate(login_url, onSignatureFail, onSignatureSuccess,
24+
async function loginWithAuthenticate(login_url, text_to_sign, onSignatureFail, onSignatureSuccess,
2525
onLoginRequestError, onLoginFail, onLoginSuccess) {
26-
scatter.authenticate().then(signature => {
26+
27+
scatter = ScatterJS.scatter;
28+
await scatter.getArbitrarySignature(scatter.identity.publicKey, text_to_sign).then(
29+
signature => {
30+
2731
if (typeof onSignatureSuccess === 'function') {
2832
onSignatureSuccess(signature);
2933
}
@@ -110,6 +114,7 @@ function signupWithData(pubkey, pubkeyFieldName, email, signup_url, onSignupRequ
110114
}
111115

112116
async function requestIdentity(requiredFields, pubkeyFieldName, signup_url, network, onIdentityReject) {
117+
const scatter = ScatterJS.scatter;
113118
let identitySettings = {
114119
personal: requiredFields,
115120
};

0 commit comments

Comments
 (0)