This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
ledger.js
79 lines (79 loc) · 2.89 KB
/
ledger.js
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
var Ledger = {
init: function(options) {
Ledger._options = options
Ledger._poll_session = false;
Ledger._createProxy();
addEventListener("message", Ledger._callback, false);
},
isAppAvailable: function() {
Ledger._message({ command:"ping" });
},
launchApp: function() {
Ledger._message({ command:"launch" });
},
hasSession: function() {
Ledger._message({ command:"has_session" });
},
bitid: function(uri, silent) {
Ledger._messageAfterSession({ command:"bitid", uri:uri, silent:silent })
},
getAccounts: function() {
Ledger._messageAfterSession({ command:"get_accounts" })
},
getOperations: function(account_id) {
Ledger._messageAfterSession({ command:"get_operations", account_id: account_id })
},
getNewAddresses: function(account_id, count) {
Ledger._messageAfterSession({ command:"get_new_addresses", account_id: account_id, count: count })
},
sendPayment: function(address, amount, data) {
Ledger._messageAfterSession({ command:"send_payment", address:address, amount:amount, data:data })
},
getXPubKey: function(path) {
Ledger._messageAfterSession({ command:"get_xpubkey", path:path })
},
signMessage: function(path, message) {
Ledger._messageAfterSession({ command:"sign_message", path:path, message:message })
},
signP2SH: function(inputs, scripts, outputs_number, outputs_script, paths) {
Ledger._messageAfterSession({ command:"sign_p2sh", inputs: inputs, scripts: scripts, outputs_number: outputs_number, outputs_script: outputs_script, paths: paths })
},
_createProxy: function() {
var div = document.createElement('div');
div.id = 'ledger-iframe';
div.style.position = 'absolute'
div.style.left = '-5000px'
document.body.appendChild(div);
Ledger._iframe = document.createElement('iframe');
Ledger._iframe.setAttribute("src", "https://www.ledgerwallet.com/proxy");
document.getElementById('ledger-iframe').appendChild(Ledger._iframe);
Ledger._iframe.addEventListener("load", function() {
if (Ledger._after_session) {
Ledger.launchApp();
}
}, false);
},
_message: function(data) {
Ledger._iframe.contentWindow.postMessage(data, "*");
},
_messageAfterSession: function(data) {
Ledger._after_session = data
Ledger._message("launch");
Ledger._should_poll_session = true;
Ledger._do_poll_session();
},
_callback: function(event) {
if (typeof event.data.response == "object" && event.data.response.command == "has_session" && event.data.response.success && typeof Ledger._after_session == "object") {
Ledger._message(Ledger._after_session);
Ledger._after_session = null;
Ledger._should_poll_session = false;
}
Ledger._options.callback(event.data);
},
_do_poll_session: function() {
Ledger.hasSession();
if (Ledger._should_poll_session) {
setTimeout(Ledger._do_poll_session, 500);
}
}
};