Skip to content

Commit e54d3f8

Browse files
committed
feat: ping, pong, senddsq, and various options
1 parent 7904138 commit e54d3f8

File tree

3 files changed

+153
-28
lines changed

3 files changed

+153
-28
lines changed

public/dashjoin.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ var DashJoin = ('object' === typeof module && exports) || {};
22
(function (window, DashJoin) {
33
'use strict';
44

5-
// let DashTx = window.DashTx || require('dashtx');
5+
let DashP2P = window.DashP2P || require('dashp2p');
66

77
const DENOM_LOWEST = 100001;
88
const PREDENOM_MIN = DENOM_LOWEST + 193;
99
const COLLATERAL = 10000; // DENOM_LOWEST / 10
1010
const PAYLOAD_SIZE_MAX = 4 * 1024 * 1024;
1111

12+
const DSQ_SIZE = 1; // 1-byte bool
13+
1214
// https://github.com/dashpay/dash/blob/v19.x/src/coinjoin/coinjoin.h#L39
1315
// const COINJOIN_ENTRY_MAX_SIZE = 9; // real
1416
// const COINJOIN_ENTRY_MAX_SIZE = 2; // just for testing right now
@@ -27,6 +29,10 @@ var DashJoin = ('object' === typeof module && exports) || {};
2729
let reverseDenoms = DashJoin.DENOMS.slice(0);
2830
reverseDenoms.reverse();
2931

32+
let Packers = {};
33+
let Parsers = {};
34+
let Utils = {};
35+
3036
// Ask Niles if there's an layman-ish obvious way to do this
3137
DashJoin.getDenom = function (sats) {
3238
for (let denom of reverseDenoms) {
@@ -39,9 +45,39 @@ var DashJoin = ('object' === typeof module && exports) || {};
3945
return 0;
4046
};
4147

42-
DashJoin.utils = {};
48+
/**
49+
* Turns on or off DSQ messages (necessary for CoinJoin, but off by default)
50+
* @param {Object} opts
51+
* @param {NetworkName} opts.network - "mainnet", "testnet", etc
52+
* @param {Uint8Array?} [opts.message]
53+
* @param {Boolean?} [opts.send]
54+
*/
55+
Packers.senddsq = function ({ network, message = null, send = true }) {
56+
// const command = 'senddsq';
57+
// if (!message) {
58+
// let dsqSize = DashP2P.sizes.HEADER_SIZE + DSQ_SIZE;
59+
// message = new Uint8Array(dsqSize);
60+
// }
61+
62+
let payload = new Uint8Array(1);
63+
if (send) {
64+
payload.set([0x01], 0);
65+
} else {
66+
payload.set([0x00], 0);
67+
}
4368

44-
DashJoin.utils.hexToBytes = function (hex) {
69+
// let payload = message.subarray(DashP2P.sizes.HEADER_SIZE);
70+
// payload.set(sendByte, 0);
71+
// void DashP2P.packers.message({ network, command, bytes: message });
72+
// return {
73+
// message,
74+
// payload,
75+
// };
76+
77+
return payload;
78+
};
79+
80+
Utils.hexToBytes = function (hex) {
4581
let bufLen = hex.length / 2;
4682
let u8 = new Uint8Array(bufLen);
4783

@@ -64,7 +100,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
64100
return u8;
65101
};
66102

67-
DashJoin.utils.bytesToHex = function (u8) {
103+
Utils.bytesToHex = function (u8) {
68104
/** @type {Array<String>} */
69105
let hex = [];
70106

@@ -76,7 +112,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
76112
return hex.join('');
77113
};
78114

79-
DashJoin.utils._evonodeMapToList = function (evonodesMap) {
115+
Utils._evonodeMapToList = function (evonodesMap) {
80116
console.log('[debug] get evonode list...');
81117
let evonodes = [];
82118
{
@@ -104,7 +140,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
104140
}
105141

106142
// void shuffle(evonodes);
107-
evonodes.sort(DashJoin.utils.sortMnListById);
143+
evonodes.sort(Utils.sortMnListById);
108144
return evonodes;
109145
};
110146

@@ -114,7 +150,7 @@ var DashJoin = ('object' === typeof module && exports) || {};
114150
* @param {Object} b
115151
* @param {String} b.id
116152
*/
117-
DashJoin.utils.sortMnListById = function (a, b) {
153+
Utils.sortMnListById = function (a, b) {
118154
if (a.id > b.id) {
119155
return 1;
120156
}
@@ -124,6 +160,10 @@ var DashJoin = ('object' === typeof module && exports) || {};
124160
return 0;
125161
};
126162

163+
DashJoin.packers = Packers;
164+
DashJoin.parsers = Parsers;
165+
DashJoin.utils = Utils;
166+
127167
//@ts-ignore
128168
window.DashJoin = DashJoin;
129169
})(globalThis.window || {}, DashJoin);

public/dashp2p.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ var DashP2P = ('object' === typeof module && exports) || {};
145145
p2p.header = Parsers.header(chunk);
146146
} catch (e) {
147147
p2p.state = 'error';
148-
p2p.error = new Error('header parse error');
148+
p2p.error = new Error(`header parse error: ${e.message}`);
149+
console.error(e);
150+
console.error(chunk);
149151
return;
150152
}
151153

@@ -209,8 +211,7 @@ var DashP2P = ('object' === typeof module && exports) || {};
209211
SIZES.PAYLOAD_SIZE + // 4
210212
SIZES.CHECKSUM; // 4
211213
Sizes.HEADER_SIZE = TOTAL_HEADER_SIZE; // 24
212-
213-
Parsers.PING_SIZE = SIZES.NONCE;
214+
Sizes.PING_SIZE = SIZES.NONCE; // same as pong
214215

215216
Packers.PROTOCOL_VERSION = 70227;
216217
Packers.NETWORKS = {};
@@ -372,6 +373,41 @@ var DashP2P = ('object' === typeof module && exports) || {};
372373
return message;
373374
};
374375

376+
Packers.verack = function ({ network }) {
377+
let verackBytes = Packers.message({
378+
network: network,
379+
command: 'verack',
380+
payload: null,
381+
});
382+
return verackBytes;
383+
};
384+
385+
/**
386+
* In this case the only bytes are the nonce
387+
* Use a .subarray(offset) to define an offset.
388+
* (a manual offset will not work consistently, and .byteOffset is context-sensitive)
389+
* @param {Object} opts
390+
* @param {NetworkName} opts.network - "mainnet", "testnet", etc
391+
* @param {Uint8Array?} [opts.message]
392+
* @param {Uint8Array} opts.nonce
393+
*/
394+
Packers.pong = function ({ network, message = null, nonce }) {
395+
// const command = 'pong';
396+
397+
if (!message) {
398+
let pongSize = Sizes.HEADER_SIZE + Sizes.PING_SIZE;
399+
message = new Uint8Array(pongSize);
400+
}
401+
402+
let payload = message.subarray(Sizes.HEADER_SIZE);
403+
payload.set(nonce, 0);
404+
405+
// void CJPacker.packMessage({ network, command, bytes: message });
406+
// return message;
407+
408+
return payload;
409+
};
410+
375411
/**
376412
* First 4 bytes of SHA256(SHA256(payload)) in internal byte order.
377413
* @param {Uint8Array} payload

public/wallet-app.js

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,28 +1056,77 @@
10561056
});
10571057
wsc.addEventListener('open', async function () {
10581058
// p2p.initWebSocket(wsc);
1059+
{
1060+
let payload = DashP2P.packers.version({
1061+
addr_recv_ip: App._evonode.hostname,
1062+
addr_recv_port: App._evonode.port,
1063+
start_height: App._chaininfo.blocks,
1064+
});
1065+
let command = 'version';
1066+
let versionBytes = DashP2P.packers.message({
1067+
network,
1068+
command,
1069+
payload,
1070+
});
1071+
wsc.send(versionBytes);
1072+
}
10591073

1060-
let payload = DashP2P.packers.version({
1061-
addr_recv_ip: App._evonode.hostname,
1062-
addr_recv_port: App._evonode.port,
1063-
start_height: App._chaininfo.blocks,
1064-
});
1065-
let command = 'version';
1066-
let messageBytes = DashP2P.packers.message({ network, command, payload });
1067-
wsc.send(messageBytes);
1074+
{
1075+
let verackBytes = DashP2P.packers.verack({ network: network });
1076+
console.log('wsc.send(verackBytes)');
1077+
wsc.send(verackBytes);
1078+
}
10681079
});
10691080

1081+
// initialize connection
1082+
// {
1083+
// let versionReq = DashP2P.packers.version({
1084+
// addr_recv_ip: App._evonode.hostname,
1085+
// addr_recv_port: App._evonode.port,
1086+
// start_height: App._chaininfo.blocks,
1087+
// });
1088+
// wsc.send(versionReq);
1089+
// void (await p2p.accept(['version']));
1090+
// }
1091+
// {
1092+
// }
1093+
// let msg = await p2p.accept(['verack']);
1094+
// for (;;) {
1095+
// let msg = await p2p.accept(['ping', 'inv']);
1096+
// if (msg.header.command === 'ping') {
1097+
// let pongBytes = DashP2P.packers.pong({
1098+
// network: network,
1099+
// nonce: msg.payload,
1100+
// });
1101+
// wsc.send(pongBytes);
1102+
// }
1103+
// }
1104+
10701105
for (;;) {
1071-
let msg = await p2p.accept([
1072-
'*',
1073-
'inv',
1074-
'ping',
1075-
'pong',
1076-
'version',
1077-
'verack',
1078-
]);
1079-
console.log('p2p.accept():');
1080-
console.log(msg);
1106+
let subs = ['*', 'inv', 'ping', 'pong', 'version', 'verack'];
1107+
let msg = await p2p.accept(subs);
1108+
let command = msg.header.command;
1109+
console.log('p2p.accept():', command);
1110+
let isSub = subs.includes(command);
1111+
if (isSub) {
1112+
console.log(msg);
1113+
}
1114+
1115+
// if (command === 'verack') {
1116+
// let verackBytes = DashP2P.packers.verack({ network: network });
1117+
// console.log('wsc.send(verackBytes)');
1118+
// wsc.send(verackBytes);
1119+
// } else
1120+
if (command === 'ping') {
1121+
let pongBytes = DashP2P.packers.pong({
1122+
network: network,
1123+
nonce: msg.payload,
1124+
});
1125+
console.log('wsc.send(pongBytes)');
1126+
wsc.send(pongBytes);
1127+
} else if (command === 'inv') {
1128+
console.log('(ignore inv)');
1129+
}
10811130
}
10821131
}
10831132

0 commit comments

Comments
 (0)