-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsimple_payment_api_usage.html
139 lines (113 loc) · 4.25 KB
/
simple_payment_api_usage.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
<html>
<body>
<input id="walletIdentfier" type="text" placeholder="identifier" value="" required="true" />
<input id="walletPassword" type="password" placeholder="password" value="" required="true" />
<select id="walletVersion">
<option value="v1">V1</option>
<option value="v2">V2</option>
<option value="v3" selected>V3</option>
</select>
<button onclick="createWallet()">create wallet</button>
<button onclick="initWallet()">init wallet</button>
<div id="address" style="display: none;"></div>
<div id="send" style="display: none;">
<input id="sendAddress" type="text" placeholder="address" value="" required="true" />
<input id="sendAmount" type="text" placeholder="amount" value="" required="true" />
<button onclick="send()">send</button>
</div>
<script src="../build/asmcrypto.min.js"></script>
<script src="../build/blocktrail-sdk-full.min.js"></script>
<script>
var blocktrail = window.blocktrailSDK;
blocktrail.debug.enable('*');
var client = blocktrail.BlocktrailSDK({
apiKey : "MY_APIKEY",
apiSecret : "MY_APISECRET",
testnet : true
});
var wallet;
function displaySend() {
document.getElementById('send').style.display = 'block';
}
function displayAddress(address) {
document.getElementById('address').style.display = 'block';
document.getElementById('address').innerHTML = address;
}
function createWallet() {
var identifier = document.getElementById('walletIdentfier').value;
var password = document.getElementById('walletPassword').value;
var version = document.getElementById('walletVersion').value;
if (!identifier) {
alert("Missing identifier");
return;
}
if (!password) {
alert("Missing password");
return;
}
client.createNewWallet({
identifier: identifier,
passphrase: password,
keyIndex: 9999,
walletVersion: version
}, function(err, _wallet, backupInfo) {
if (err) {
return console.log("createNewWallet ERR", err);
}
console.log('backupInfo', backupInfo);
wallet = _wallet;
displaySend();
wallet.getNewAddress(function(err, address) {
displayAddress(address);
});
});
}
function initWallet() {
var identifier = document.getElementById('walletIdentfier').value;
var password = document.getElementById('walletPassword').value;
var version = document.getElementById('walletVersion').value;
if (!identifier) {
alert("Missing identifier");
return;
}
if (!password) {
alert("Missing password");
return;
}
client.initWallet({
identifier: identifier,
passphrase: password
}, function(err, _wallet) {
if (err) {
return console.log("initWallet ERR", err);
}
wallet = _wallet;
displaySend();
wallet.getNewAddress(function(err, address) {
displayAddress(address);
});
});
}
function send() {
var address = document.getElementById('sendAddress').value;
var amount = document.getElementById('sendAmount').value;
if (!address) {
alert("Missing address");
return;
}
if (!amount) {
alert("Missing amount");
return;
}
var pay = {};
pay[address] = blocktrail.toSatoshi(amount);
wallet.pay(pay, null, true, function(err, txId) {
if (err) {
return console.log("send ERR", err);
}
console.log(txId);
});
}
</script>
</body>
</html>