-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
133 lines (125 loc) · 3.19 KB
/
index.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
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
// DogecoinJS v@VERSION
var fetch = require( "node-fetch" );
var NodeSocket = require( "ws" );
let listeners = {};
let ws = null;
let wsCommandQueue = [];
async function getSocket() {
return new Promise( ( res ) => {
if( typeof window !== "undefined" ) {
ws = new WebSocket( "wss://ws.dogechain.info/inv" );
}
else {
ws = new NodeSocket( "wss://ws.dogechain.info/inv" );
}
const heartbeatInterval = 1000 * 60; //ms between PING's
let heartbeatHandle;
ws.onopen = function( event ) {
heartbeatHandle = setInterval( () => {
ws.send( JSON.stringify( { op: "ping_block" } ) );
}, heartbeatInterval );
wsCommandQueue.forEach( c => {
ws.send( JSON.stringify( c ) );
});
};
ws.onerror = function( error ) {
console.error( error );
};
ws.onmessage = function( event ) {
message = JSON.parse( event.data );
// console.log( message );
switch( message.op ) {
case "status":
break;
case "utx":
// Unconfirmed transaction
Object.keys( listeners ).forEach( addr => {
if( listeners[ addr ].listen ) {
// Check inputs
message.x.inputs.forEach( input => {
if( input.prev_out && input.prev_out.addr === addr ) {
// This wallet is sending
let amount = input.prev_out.value / 100000000;
listeners[ addr ].listen( addr, -amount, {
id: message.x.hash,
data: message.x
} );
}
});
// Check outputs
message.x.outputs.forEach( output => {
if( output.addr === addr ) {
// This wallet is receiving
let amount = output.value / 100000000;
listeners[ addr ].listen( addr, amount, {
id: message.x.hash,
data: message.x
} );
}
});
}
});
break;
}
};
ws.onclose = function() {
clearInterval( heartbeatHandle );
console.log( "closed" );
};
res( ws );
});
}
var dogecoinJS = {
isDebug: false,
version: function() {
return "@VERSION";
},
lookup: async function( address, handler = null ) {
let promise = fetch( `https://my.dogechain.info/api/v2/get_address_balance/DOGE/${address}` ).then( r => r.json() );
if( handler ) {
handler( await promise );
}
else {
return promise;
}
},
qrcode: async function( address, handler = null ) {
let promise = fetch( `https://dogechain.info/api/v1/address/qrcode/${address}` ).then( r => r.blob() );
if( handler ) {
handler( await promise );
}
else {
return promise;
}
},
listen: async function( address, handler ) {
let socket = ws || await getSocket();
let addresses = [ address ];
if( Array.isArray( address ) ) {
addresses = address;
}
addresses.forEach( addr => {
listeners[ addr ] = listeners[ addr ] || {};
listeners[ addr ].listen = handler;
if( !socket.readyState ) {
wsCommandQueue.push( {
op: "addr_sub",
addr: addr
});
}
else {
socket.send( JSON.stringify( {
op: "addr_sub",
addr: addr
} ) );
}
});
},
};
// Expose everything, for browser and Node..
if( typeof module !== "undefined" && module.exports ) {
module.exports = dogecoinJS;
}
if( typeof window !== "undefined" ) {
window.Dogecoin = dogecoinJS;
}