├── .gitignore ├── .nvmrc ├── LICENSE.txt ├── README.md ├── bin └── run-witness.sh ├── migrations └── 001-initial.sql ├── package-lock.json ├── package.json ├── src ├── api │ ├── api.test.ts │ └── api.ts ├── common │ ├── ControlledPromise.ts │ ├── HDKey.ts │ ├── HDKeys.ts │ ├── Signature.ts │ ├── address.ts │ ├── base32.ts │ ├── checksum_hash.test.ts │ ├── checksum_hash.ts │ ├── common.ts │ ├── conf.ts │ ├── desktop.test.ts │ ├── desktop.ts │ ├── encrypt.ts │ ├── hash.test.ts │ ├── hash.ts │ ├── hdkeys.test.ts │ ├── log.ts │ ├── mail.ts │ ├── merkle.ts │ ├── object_hash.test.ts │ ├── object_hash.ts │ ├── object_length.test.ts │ ├── object_length.ts │ ├── signature.test.ts │ ├── string_utils.ts │ ├── timeoutPromise.ts │ ├── utils.ts │ └── validation_utils.ts ├── core │ ├── authentifiers.ts │ ├── author.ts │ ├── balances.ts │ ├── composeParent.ts │ ├── composer.test.ts │ ├── composer.ts │ ├── definition.ts │ ├── device.test.ts │ ├── device.ts │ ├── genesis.ts │ ├── graph.ts │ ├── headers_commission.ts │ ├── main_chain.ts │ ├── mc_outputs.ts │ ├── message.ts │ ├── payload_commission.ts │ ├── signer.test.ts │ ├── signer.ts │ ├── unit.ts │ └── validation.ts ├── events │ ├── eventbus.test.ts │ ├── eventbus.ts │ └── events.ts ├── main.ts ├── models │ ├── Authors.ts │ ├── Balls.ts │ ├── MainChain.ts │ ├── Messages.ts │ ├── MyAddresses.ts │ ├── MyWitnesses.ts │ ├── Parents.ts │ ├── Units.ts │ ├── Witnesses.ts │ ├── XPubKeys.ts │ ├── myaddresses.test.ts │ └── xpubkey.test.ts ├── network │ ├── Peer.ts │ ├── SocketAddr.ts │ ├── WebSocketClient.ts │ ├── WebSocketServer.ts │ ├── peer.test.ts │ ├── pool.test.ts │ └── websocket.test.ts ├── storage │ ├── kvstore.test.ts │ ├── kvstore.ts │ ├── sqlstore.test.ts │ └── sqlstore.ts ├── typings │ └── type.d.ts ├── wallets │ ├── Wallets.ts │ ├── wallet.ts │ ├── wallet_general.ts │ └── wallets.test.ts └── witness │ └── witness.ts ├── tsconfig.json └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | 4 | logs 5 | *.log 6 | 7 | node_modules/ 8 | .npm 9 | 10 | dist 11 | *.sqlite 12 | tmp 13 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 8.0 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2015 - present Microsoft Corporation 4 | 5 | All rights reserved. 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A Typescript implementation of itc. 2 | 3 | ## Project Stage 4 | 5 | Pre-alpha, a try-out of Typescript on blockchain, still under active development. 6 | Any implementation details may be rapidly changed without prior notice. 7 | 8 | ## Documentation 9 | the draft of yellow paper has been published 10 | TBD 11 | 12 | ## License 13 | 14 | MIT. 15 | -------------------------------------------------------------------------------- /bin/run-witness.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | pm2 flush # empty logs 4 | 5 | LISTEN_PORT=3001 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 6 | LISTEN_PORT=3002 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 7 | LISTEN_PORT=3003 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 8 | LISTEN_PORT=3004 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 9 | LISTEN_PORT=3005 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 10 | LISTEN_PORT=3006 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 11 | LISTEN_PORT=3007 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 12 | LISTEN_PORT=3008 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 13 | LISTEN_PORT=3009 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 14 | LISTEN_PORT=3010 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 15 | LISTEN_PORT=3011 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 16 | LISTEN_PORT=3012 POOL_SEEDS="ws://localhost:3001" pm2 start ./dist/witness/witness.js -f 17 | -------------------------------------------------------------------------------- /migrations/001-initial.sql: -------------------------------------------------------------------------------- 1 | -- Up 2 | CREATE TABLE units ( 3 | unit CHAR(44) NOT NULL PRIMARY KEY, 4 | creation_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 5 | version VARCHAR(3) NOT NULL DEFAULT '1.0', 6 | alt VARCHAR(3) NOT NULL DEFAULT '1', 7 | witness_list_unit CHAR(44) NULL, 8 | last_ball_unit CHAR(44) NULL, 9 | content_hash CHAR(44) NULL, 10 | headers_commission INT NOT NULL, 11 | payload_commission INT NOT NULL, 12 | is_free TINYINT NOT NULL DEFAULT 1, 13 | is_on_main_chain TINYINT NOT NULL DEFAULT 0, 14 | main_chain_index INT NULL, -- when it first appears 15 | latest_included_mc_index INT NULL, -- latest MC ball that is included in this ball (excluding itself) 16 | level INT NULL, 17 | witnessed_level INT NULL, 18 | is_stable TINYINT NOT NULL DEFAULT 0, 19 | sequence TEXT CHECK (sequence IN('good','temp-bad','final-bad')) NOT NULL DEFAULT 'good', 20 | best_parent_unit CHAR(44) NULL, 21 | CONSTRAINT unitsByLastBallUnit FOREIGN KEY (last_ball_unit) REFERENCES units(unit), 22 | FOREIGN KEY (best_parent_unit) REFERENCES units(unit), 23 | CONSTRAINT unitsByWitnessListUnit FOREIGN KEY (witness_list_unit) REFERENCES units(unit) 24 | ); 25 | CREATE INDEX byLB ON units(last_ball_unit); 26 | CREATE INDEX byBestParent ON units(best_parent_unit); 27 | CREATE INDEX byWL ON units(witness_list_unit); 28 | CREATE INDEX byMainChain ON units(is_on_main_chain); 29 | CREATE INDEX byMcIndex ON units(main_chain_index); 30 | CREATE INDEX byLimci ON units(latest_included_mc_index); 31 | CREATE INDEX byLevel ON units(level); 32 | CREATE INDEX byFree ON units(is_free); 33 | CREATE INDEX byStableMci ON units(is_stable, main_chain_index); 34 | 35 | CREATE TABLE balls ( 36 | ball CHAR(44) NOT NULL PRIMARY KEY, -- sha256 in base64 37 | creation_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 38 | unit CHAR(44) NOT NULL UNIQUE, -- sha256 in base64 39 | count_paid_witnesses TINYINT NULL, 40 | FOREIGN KEY (unit) REFERENCES units(unit) 41 | ); 42 | CREATE INDEX byCountPaidWitnesses ON balls(count_paid_witnesses); 43 | 44 | -- must be sorted by parent_unit 45 | CREATE TABLE parenthoods ( 46 | child_unit CHAR(44) NOT NULL, 47 | parent_unit CHAR(44) NOT NULL, 48 | PRIMARY KEY (parent_unit, child_unit), 49 | CONSTRAINT parenthoodsByChild FOREIGN KEY (child_unit) REFERENCES units(unit), 50 | CONSTRAINT parenthoodsByParent FOREIGN KEY (parent_unit) REFERENCES units(unit) 51 | ); 52 | CREATE INDEX byChildUnit ON parenthoods(child_unit); 53 | 54 | CREATE TABLE definitions ( 55 | definition_chash CHAR(32) NOT NULL PRIMARY KEY, 56 | definition TEXT NOT NULL, 57 | has_references TINYINT NOT NULL 58 | ); 59 | 60 | -- current list of all known from-addresses 61 | CREATE TABLE addresses ( 62 | address CHAR(32) NOT NULL PRIMARY KEY, 63 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 64 | ); 65 | 66 | -- must be sorted by address 67 | CREATE TABLE unit_authors ( 68 | unit CHAR(44) NOT NULL, 69 | address CHAR(32) NOT NULL, 70 | definition_chash CHAR(32) NULL, -- only with 1st ball from this address, and with next ball after definition change 71 | _mci INT NULL, 72 | PRIMARY KEY (unit, address), 73 | FOREIGN KEY (unit) REFERENCES units(unit), 74 | CONSTRAINT unitAuthorsByAddress FOREIGN KEY (address) REFERENCES addresses(address), 75 | FOREIGN KEY (definition_chash) REFERENCES definitions(definition_chash) 76 | ); 77 | CREATE INDEX byDefinitionChash ON unit_authors(definition_chash); 78 | CREATE INDEX unitAuthorsIndexByAddress ON unit_authors(address); 79 | CREATE INDEX unitAuthorsIndexByAddressDefinitionChash ON unit_authors(address, definition_chash); 80 | CREATE INDEX unitAuthorsIndexByAddressMci ON unit_authors(address, _mci); 81 | 82 | CREATE TABLE authentifiers ( 83 | unit CHAR(44) NOT NULL, 84 | address CHAR(32) NOT NULL, 85 | path VARCHAR(40) NOT NULL, 86 | authentifier VARCHAR(4096) NOT NULL, 87 | PRIMARY KEY (unit, address, path), 88 | FOREIGN KEY (unit) REFERENCES units(unit), 89 | CONSTRAINT authentifiersByAddress FOREIGN KEY (address) REFERENCES addresses(address) 90 | ); 91 | CREATE INDEX authentifiersIndexByAddress ON authentifiers(address); 92 | 93 | -- must be sorted by address 94 | CREATE TABLE unit_witnesses ( 95 | unit CHAR(44) NOT NULL, 96 | address VARCHAR(32) NOT NULL, 97 | PRIMARY KEY (unit, address), 98 | FOREIGN KEY (unit) REFERENCES units(unit) 99 | ); 100 | CREATE INDEX byAddress ON unit_witnesses(address); 101 | 102 | CREATE TABLE witness_list_hashes ( 103 | witness_list_unit CHAR(44) NOT NULL PRIMARY KEY, 104 | witness_list_hash CHAR(44) NOT NULL UNIQUE, 105 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 106 | FOREIGN KEY (witness_list_unit) REFERENCES units(unit) 107 | ); 108 | 109 | 110 | -- if this ball wins headers commission from at least one of the included balls, how it is distributed 111 | -- required if more than one author 112 | -- if one author, all commission goes to the author by default 113 | CREATE TABLE earned_headers_commission_recipients ( 114 | unit CHAR(44) NOT NULL, 115 | address VARCHAR(32) NOT NULL, 116 | earned_headers_commission_share INT NOT NULL, -- percentage 117 | PRIMARY KEY (unit, address), 118 | FOREIGN KEY (unit) REFERENCES units(unit) 119 | ); 120 | CREATE INDEX earnedbyAddress ON earned_headers_commission_recipients(address); 121 | 122 | CREATE TABLE messages ( 123 | unit CHAR(44) NOT NULL, 124 | message_index TINYINT NOT NULL, 125 | app VARCHAR(30) NOT NULL, 126 | payload_location TEXT CHECK (payload_location IN ('inline','uri','none')) NOT NULL, 127 | payload_hash VARCHAR(44) NOT NULL, 128 | payload TEXT NULL, 129 | payload_uri_hash VARCHAR(44) NULL, 130 | payload_uri VARCHAR(500) NULL, 131 | PRIMARY KEY (unit, message_index), 132 | FOREIGN KEY (unit) REFERENCES units(unit) 133 | ); 134 | 135 | -- must be sorted by spend_proof 136 | CREATE TABLE spend_proofs ( 137 | unit CHAR(44) NOT NULL, 138 | message_index TINYINT NOT NULL, 139 | spend_proof_index TINYINT NOT NULL, 140 | spend_proof CHAR(44) NOT NULL, 141 | address CHAR(32) NOT NULL, 142 | PRIMARY KEY (unit, message_index, spend_proof_index), 143 | UNIQUE (spend_proof, unit), 144 | FOREIGN KEY (unit) REFERENCES units(unit), 145 | CONSTRAINT spendProofsByAddress FOREIGN KEY (address) REFERENCES addresses(address) 146 | ); 147 | CREATE INDEX spendProofsIndexByAddress ON spend_proofs(address); 148 | 149 | -- ------------------------- 150 | -- Payments 151 | 152 | CREATE TABLE inputs ( 153 | unit CHAR(44) NOT NULL, 154 | message_index TINYINT NOT NULL, 155 | input_index TINYINT NOT NULL, 156 | asset CHAR(44) NULL, 157 | denomination INT NOT NULL DEFAULT 1, 158 | is_unique TINYINT NULL DEFAULT 1, 159 | type TEXT CHECK (type IN('transfer','headers_commission','witnessing','issue')) NOT NULL, 160 | src_unit CHAR(44) NULL, -- transfer 161 | src_message_index TINYINT NULL, -- transfer 162 | src_output_index TINYINT NULL, -- transfer 163 | from_main_chain_index INT NULL, -- witnessing/hc 164 | to_main_chain_index INT NULL, -- witnessing/hc 165 | serial_number BIGINT NULL, -- issue 166 | amount BIGINT NULL, -- issue 167 | address CHAR(32) NOT NULL, 168 | PRIMARY KEY (unit, message_index, input_index), 169 | UNIQUE (src_unit, src_message_index, src_output_index, is_unique), -- UNIQUE guarantees there'll be no double spend for type=transfer 170 | UNIQUE (type, from_main_chain_index, address, is_unique), -- UNIQUE guarantees there'll be no double spend for type=hc/witnessing 171 | UNIQUE (asset, denomination, serial_number, address, is_unique), -- UNIQUE guarantees there'll be no double issue 172 | FOREIGN KEY (unit) REFERENCES units(unit), 173 | CONSTRAINT inputsBySrcUnit FOREIGN KEY (src_unit) REFERENCES units(unit), 174 | CONSTRAINT inputsByAddress FOREIGN KEY (address) REFERENCES addresses(address), 175 | CONSTRAINT inputsByAsset FOREIGN KEY (asset) REFERENCES assets(unit) 176 | ); 177 | CREATE INDEX inputsIndexByAddress ON inputs(address); 178 | CREATE INDEX inputsIndexByAddressTypeToMci ON inputs(address, type, to_main_chain_index); 179 | CREATE INDEX inputsIndexByAssetType ON inputs(asset, type); 180 | 181 | CREATE TABLE outputs ( 182 | output_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 183 | unit CHAR(44) NOT NULL, 184 | message_index TINYINT NOT NULL, 185 | output_index TINYINT NOT NULL, 186 | asset CHAR(44) NULL, 187 | denomination INT NOT NULL DEFAULT 1, 188 | address VARCHAR(32) NULL, -- NULL if hidden by output_hash 189 | amount BIGINT NOT NULL, 190 | blinding CHAR(16) NULL, 191 | output_hash CHAR(44) NULL, 192 | is_serial TINYINT NULL, -- NULL if not stable yet 193 | is_spent TINYINT NOT NULL DEFAULT 0, 194 | UNIQUE (unit, message_index, output_index), 195 | FOREIGN KEY (unit) REFERENCES units(unit), 196 | CONSTRAINT outputsByAsset FOREIGN KEY (asset) REFERENCES assets(unit) 197 | ); 198 | CREATE INDEX outputsByAddressSpent ON outputs(address, is_spent); 199 | CREATE INDEX outputsIndexByAsset ON outputs(asset); 200 | CREATE INDEX outputsIsSerial ON outputs(is_serial); 201 | 202 | -- ------------ 203 | -- Commissions 204 | 205 | -- updated immediately after main chain is updated 206 | CREATE TABLE headers_commission_contributions ( 207 | unit CHAR(44) NOT NULL, -- child unit that receives (and optionally redistributes) commission from parent units 208 | address CHAR(32) NOT NULL, -- address of the commission receiver: author of child unit or address named in earned_headers_commission_recipients 209 | amount BIGINT NOT NULL, 210 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 211 | PRIMARY KEY (unit, address), 212 | FOREIGN KEY (unit) REFERENCES units(unit) 213 | ); 214 | CREATE INDEX hccbyAddress ON headers_commission_contributions(address); 215 | 216 | CREATE TABLE headers_commission_outputs ( 217 | main_chain_index INT NOT NULL, 218 | address CHAR(32) NOT NULL, -- address of the commission receiver 219 | amount BIGINT NOT NULL, 220 | is_spent TINYINT NOT NULL DEFAULT 0, 221 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 222 | PRIMARY KEY (main_chain_index, address) 223 | ); 224 | -- CREATE INDEX hcobyAddressSpent ON headers_commission_outputs(address, is_spent); 225 | CREATE UNIQUE INDEX hcobyAddressMci ON headers_commission_outputs(address, main_chain_index); 226 | CREATE UNIQUE INDEX hcobyAddressSpentMci ON headers_commission_outputs(address, is_spent, main_chain_index); 227 | 228 | CREATE TABLE paid_witness_events ( 229 | unit CHAR(44) NOT NULL, 230 | address CHAR(32) NOT NULL, -- witness address 231 | delay TINYINT NULL, -- NULL if expired 232 | PRIMARY KEY (unit, address), 233 | FOREIGN KEY (unit) REFERENCES units(unit), 234 | FOREIGN KEY (address) REFERENCES addresses(address) 235 | ); 236 | CREATE INDEX pweIndexByAddress ON paid_witness_events(address); 237 | 238 | CREATE TABLE witnessing_outputs ( 239 | main_chain_index INT NOT NULL, 240 | address CHAR(32) NOT NULL, 241 | amount BIGINT NOT NULL, 242 | is_spent TINYINT NOT NULL DEFAULT 0, 243 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 244 | PRIMARY KEY (main_chain_index, address), 245 | FOREIGN KEY (address) REFERENCES addresses(address) 246 | ); 247 | -- CREATE INDEX byWitnessAddressSpent ON witnessing_outputs(address, is_spent); 248 | CREATE UNIQUE INDEX byWitnessAddressMci ON witnessing_outputs(address, main_chain_index); 249 | CREATE UNIQUE INDEX byWitnessAddressSpentMci ON witnessing_outputs(address, is_spent, main_chain_index); 250 | 251 | -- ----------------------- 252 | -- wallet tables 253 | 254 | -- wallets composed of BIP44 keys, the keys live on different devices, each device knows each other's extended public key 255 | CREATE TABLE wallets ( 256 | wallet CHAR(44) NOT NULL PRIMARY KEY, 257 | account INT NOT NULL, 258 | definition_template TEXT NOT NULL, 259 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 260 | full_approval_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, 261 | ready_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP -- when all members notified me that they saw the wallet fully approved 262 | ); 263 | 264 | CREATE TABLE extended_pubkeys ( 265 | wallet CHAR(44) NOT NULL, -- no FK because xpubkey may arrive earlier than the wallet is approved by the user and written to the db 266 | extended_pubkey CHAR(112) NULL, -- base58 encoded, see bip32, NULL while pending 267 | device_address CHAR(33) NOT NULL, 268 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 269 | approval_date TIMESTAMP NULL, 270 | member_ready_date TIMESTAMP NULL, -- when this member notified us that he has collected all member xpubkeys 271 | PRIMARY KEY (wallet, device_address) 272 | -- own address is not present in correspondents 273 | -- FOREIGN KEY byDeviceAddress(device_address) REFERENCES correspondent_devices(device_address) 274 | ); 275 | 276 | CREATE TABLE wallet_signing_paths ( 277 | wallet CHAR(44) NOT NULL, -- no FK because xpubkey may arrive earlier than the wallet is approved by the user and written to the db 278 | signing_path VARCHAR(255) NULL, -- NULL if xpubkey arrived earlier than the wallet was approved by the user 279 | device_address CHAR(33) NOT NULL, 280 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 281 | PRIMARY KEY (wallet, signing_path), 282 | FOREIGN KEY (wallet) REFERENCES wallets(wallet) 283 | -- own address is not present in correspondents 284 | -- FOREIGN KEY byDeviceAddress(device_address) REFERENCES correspondent_devices(device_address) 285 | ); 286 | 287 | -- BIP44 addresses. Coin type and key are fixed and stored in credentials in localstorage. 288 | -- derivation path is m/44'/0'/key'/is_change/address_index 289 | CREATE TABLE my_addresses ( 290 | address CHAR(32) NOT NULL PRIMARY KEY, 291 | wallet CHAR(44) NOT NULL, 292 | is_change TINYINT NOT NULL, 293 | address_index INT NOT NULL, 294 | definition TEXT NOT NULL, 295 | creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 296 | UNIQUE (wallet, is_change, address_index), 297 | FOREIGN KEY (wallet) REFERENCES wallets(wallet) 298 | ); 299 | 300 | CREATE TABLE my_witnesses ( 301 | address VARCHAR(32) NOT NULL PRIMARY KEY 302 | ); 303 | 304 | -- Down 305 | DROP TABLE units; 306 | DROP TABLE balls; 307 | DROP TABLE parenthoods; 308 | DROP TABLE definitions; 309 | DROP TABLE unit_authors; 310 | DROP TABLE authentifiers; 311 | DROP TABLE unit_witnesses; 312 | DROP TABLE witness_list_hashes; 313 | DROP TABLE earned_headers_commission_recipients; 314 | DROP TABLE messages; 315 | DROP TABLE spend_proofs; 316 | DROP TABLE inputs; 317 | DROP TABLE outputs; 318 | DROP TABLE addresses; 319 | 320 | -- commissions 321 | DROP TABLE headers_commission_contributions; 322 | DROP TABLE headers_commission_outputs; 323 | DROP TABLE paid_witness_events; 324 | DROP TABLE witnessing_outputs; 325 | 326 | -- wallet 327 | DROP TABLE wallets; 328 | DROP TABLE extended_pubkeys; 329 | DROP TABLE wallet_signing_paths; 330 | DROP TABLE my_addresses; 331 | DROP TABLE my_witnesses; 332 | 333 | -- peers 334 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "itc-ts", 3 | "version": "1.0.0", 4 | "scripts": { 5 | "clean": "rimraf dist", 6 | "build": "tsc", 7 | "dev": "tsc -w", 8 | "test": "jest --runInBand" 9 | }, 10 | "author": "dbjbear@gmail.com", 11 | "devDependencies": { 12 | "@types/crypto-js": "^3.1.38", 13 | "@types/jest": "^22.0.1", 14 | "@types/node": "^8.5.7", 15 | "jest": "^22.1.4", 16 | "ts-jest": "^22.0.1", 17 | "tsify": "^3.0.4", 18 | "vinyl-buffer": "^1.0.1", 19 | "vinyl-source-stream": "^2.0.0", 20 | "watchify": "^3.9.0" 21 | }, 22 | "dependencies": { 23 | "@types/async": "^2.0.46", 24 | "@types/bitcoinjs-lib": "^3.3.1", 25 | "@types/fs-extra": "^5.0.0", 26 | "@types/koa": "^2.0.43", 27 | "@types/lodash": "^4.14.92", 28 | "@types/pino": "^4.7.1", 29 | "@types/readline-sync": "^1.4.3", 30 | "@types/sqlite3": "^3.1.1", 31 | "@types/websocket": "0.0.36", 32 | "@types/ws": "^3.2.1", 33 | "async": "^2.6.0", 34 | "bitcoinjs-lib": "^3.3.2", 35 | "bitcore-lib": "^0.15.0", 36 | "bitcore-mnemonic": "^1.5.0", 37 | "clime": "^0.5.9", 38 | "co": "^4.6.0", 39 | "crypto": "^1.0.1", 40 | "fs-extra": "^5.0.0", 41 | "koa": "^2.4.1", 42 | "koa-compose": "^4.0.0", 43 | "leveldown": "^2.1.1", 44 | "levelup": "^2.0.0-rc2", 45 | "lodash": "^4.17.4", 46 | "pino": "^4.10.3", 47 | "process": "^0.11.10", 48 | "readline-sync": "^1.4.7", 49 | "secp256k1": "^3.4.0", 50 | "sqlite": "^2.9.1", 51 | "thirty-two": "^1.0.2", 52 | "typescript": "^2.6.2", 53 | "uuid": "^3.2.1", 54 | "websocket": "^1.0.25", 55 | "websocket-as-promised": "^0.6.0", 56 | "ws": "^4.0.0" 57 | }, 58 | "jest": { 59 | "testEnvironment": "node", 60 | "transform": { 61 | "^.+\\.tsx?$": "ts-jest" 62 | }, 63 | "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", 64 | "moduleFileExtensions": [ 65 | "ts", 66 | "tsx", 67 | "js", 68 | "jsx", 69 | "json", 70 | "node" 71 | ] 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/api/api.test.ts: -------------------------------------------------------------------------------- 1 | import API from './api'; 2 | import {Peer} from '../network/Peer'; 3 | import {sleep} from '../common/utils'; 4 | 5 | jest.setTimeout(1000 * 60); 6 | 7 | test('test api', async () => { 8 | const pool = new Peer(); 9 | pool.listen(3000); 10 | 11 | const nodes: API[] = []; 12 | for (let i = 0; i < 12; i++) { 13 | const keyPath = `./tmp/witness${i}.json`; 14 | const api = await API.create(keyPath); 15 | api.peer.listen(3001 + i); 16 | await api.peer.sendMyAddr('ws://localhost:3000'); 17 | nodes.push(api); 18 | } 19 | 20 | await sleep(1000); 21 | 22 | for (let i = 0; i < 12; i++) { 23 | await nodes[i].peer.sendGetPeers('ws://localhost:3000'); 24 | } 25 | 26 | const alice = await API.create('./tmp/alice.json'); 27 | await alice.peer.sendGetPeers('ws://localhost:3000'); 28 | 29 | const witnesses = nodes.map(x => x.address); 30 | console.log(witnesses); 31 | 32 | await alice.issueGenesis(witnesses); 33 | 34 | await sleep(1000); 35 | for (let i = 0; i < 12; i++) { 36 | const balance = await nodes[i].getBalance(); 37 | console.log(`balance of witness ${i} address ${nodes[i].address}: ${JSON.stringify(balance)}`); 38 | } 39 | 40 | const bob = await API.create('./tmp/bob.json'); 41 | await nodes[0].sendPayment(bob.address, 10000, witnesses); 42 | await sleep(1000); 43 | console.log(await bob.getBalance()); 44 | 45 | pool.close(); 46 | alice.peer.close(); 47 | bob.peer.close(); 48 | nodes.forEach(x => x.peer.close()); 49 | }); 50 | -------------------------------------------------------------------------------- /src/api/api.ts: -------------------------------------------------------------------------------- 1 | import Wallets from '../wallets/Wallets'; 2 | import {default as Wallet} from '../wallets/wallet'; 3 | import MyAddresses from '../models/MyAddresses'; 4 | import {composeGenesisUnit} from '../core/composer'; 5 | import {Peer} from '../network/Peer'; 6 | 7 | export default class API { 8 | constructor(readonly wallet: Wallet, readonly address: Address, readonly peer: Peer) { 9 | } 10 | 11 | 12 | static async create(keyPath: string) { 13 | const wallet = await Wallets.readOrCreate('', keyPath); 14 | const address = await MyAddresses.issueOrSelectNextAddress(wallet.wallet); 15 | const peer = new Peer(); 16 | return new API(wallet, address, peer); 17 | } 18 | 19 | async sendPayment(to: Address, amount: number, witnesses?: Address[]) { 20 | return this.wallet.sendPayment(to, amount, witnesses); 21 | } 22 | 23 | async issueGenesis(witnesses: Address[]) { 24 | const genesisUnit = await composeGenesisUnit(witnesses, this.wallet); 25 | return this.peer.broadcastUnit(genesisUnit); 26 | } 27 | 28 | async getBalance() { 29 | return this.wallet.readBalance(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/common/ControlledPromise.ts: -------------------------------------------------------------------------------- 1 | export default class ControlledPromise { 2 | private _resolve = null; 3 | private _reject = null; 4 | private _isPending = false; 5 | private _isFulfilled = false; 6 | private _isRejected = false; 7 | private _value: any = undefined; 8 | private _promise: Promise = null; 9 | private _timer = null; 10 | private _timeout = 0; 11 | private _timeoutReason = 'Promise rejected by timeout'; 12 | 13 | /** 14 | * Returns promise itself. 15 | */ 16 | get promise(): Promise { 17 | return this._promise; 18 | } 19 | 20 | /** 21 | * Returns value with that promise was fulfilled (resolved or rejected). 22 | */ 23 | get value(): any { 24 | return this._value; 25 | } 26 | 27 | /** 28 | * Returns true if promise is pending. 29 | */ 30 | get isPending(): boolean { 31 | return this._isPending; 32 | } 33 | 34 | /** 35 | * Returns true if promise is fulfilled. 36 | */ 37 | get isFulfilled(): boolean { 38 | return this._isFulfilled; 39 | } 40 | 41 | /** 42 | * Returns true if promise rejected. 43 | */ 44 | get isRejected(): boolean { 45 | return this._isRejected; 46 | } 47 | 48 | /** 49 | * Returns true if promise fulfilled or rejected. 50 | */ 51 | get isSettled(): boolean { 52 | return this._isFulfilled || this._isRejected; 53 | } 54 | 55 | /** 56 | * Returns true if promise already called via `.call()` method. 57 | */ 58 | get isCalled(): boolean { 59 | return this.isPending || this.isSettled; 60 | } 61 | 62 | /** 63 | * This method executes `fn` and returns promise. While promise is pending all subsequent calls of `.call(fn)` 64 | * will return the same promise. To fulfill that promise you can use `.resolve() / .reject()` methods. 65 | */ 66 | call(fn): Promise { 67 | if (!this._isPending) { 68 | this.reset(); 69 | this._createPromise(); 70 | this._callFn(fn); 71 | this._createTimer(); 72 | } 73 | return this._promise; 74 | } 75 | 76 | /** 77 | * Resolves pending promise with specified `value`. 78 | */ 79 | resolve(value: any) { 80 | if (this._isPending) { 81 | this._resolve(value); 82 | } 83 | } 84 | 85 | /** 86 | * Rejects pending promise with specified `value`. 87 | */ 88 | reject(value: any) { 89 | if (this._isPending) { 90 | this._reject(value); 91 | } 92 | } 93 | 94 | /** 95 | * Resets to initial state. 96 | */ 97 | reset() { 98 | if (this._isPending) { 99 | this.reject(new Error('Promise rejected by reset')); 100 | } 101 | this._promise = null; 102 | this._isPending = false; 103 | this._isFulfilled = false; 104 | this._isRejected = false; 105 | this._value = undefined; 106 | this._clearTimer(); 107 | } 108 | 109 | /** 110 | * Sets timeout to reject promise automatically. 111 | * @param {String|Error|Function} [reason] rejection value. If it is string or error - promise will be rejected with 112 | * that error. If it is function - this function will be called after delay where you can manually resolve or reject 113 | * promise via `.resolve() / .reject()` methods. 114 | */ 115 | timeout(ms: number, reason) { 116 | this._timeout = ms; 117 | if (reason !== undefined) { 118 | this._timeoutReason = reason; 119 | } 120 | } 121 | 122 | _createPromise() { 123 | const internalPromise = new Promise((resolve, reject) => { 124 | this._isPending = true; 125 | this._resolve = resolve; 126 | this._reject = reject; 127 | }); 128 | this._promise = internalPromise 129 | .then(value => this._handleFulfill(value), error => this._handleReject(error)); 130 | } 131 | 132 | _handleFulfill(value) { 133 | this._settle(value); 134 | this._isFulfilled = true; 135 | return this._value; 136 | } 137 | 138 | _handleReject(value) { 139 | this._settle(value); 140 | this._isRejected = true; 141 | return Promise.reject(this._value); 142 | } 143 | 144 | _handleTimeout() { 145 | if (typeof this._timeoutReason === 'function') { 146 | this._timeoutReason(); 147 | } else { 148 | const error = typeof this._timeoutReason === 'string' 149 | ? new Error(this._timeoutReason) 150 | : this._timeoutReason; 151 | this.reject(error); 152 | } 153 | } 154 | 155 | _createTimer() { 156 | if (this._timeout) { 157 | this._timer = setTimeout(() => this._handleTimeout(), this._timeout); 158 | } 159 | } 160 | 161 | _clearTimer() { 162 | if (this._timer) { 163 | clearTimeout(this._timer); 164 | this._timer = null; 165 | } 166 | } 167 | 168 | _settle(value) { 169 | this._isPending = false; 170 | this._value = value; 171 | this._clearTimer(); 172 | } 173 | 174 | _callFn(fn) { 175 | if (typeof fn === 'function') { 176 | let result; 177 | try { 178 | result = fn(); 179 | } catch (e) { 180 | this.reject(e); 181 | } 182 | this._tryAttachToPromise(result); 183 | } 184 | } 185 | 186 | _tryAttachToPromise(p) { 187 | const isPromise = p && typeof p.then === 'function'; 188 | if (isPromise) { 189 | p.then(value => this.resolve(value), e => this.reject(e)); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/common/HDKey.ts: -------------------------------------------------------------------------------- 1 | import * as fs from 'fs-extra'; 2 | import * as path from 'path'; 3 | import {KeyStore} from './HDKeys'; 4 | import {sha256B64} from './hash'; 5 | import * as chash from './checksum_hash'; 6 | import * as secp256k1 from 'secp256k1'; 7 | 8 | export default class HDKey { 9 | readonly xPubKey; 10 | readonly walletId: Base64; 11 | readonly devicePrivKey: DevicePrivKey; 12 | readonly devicePubKey: DevicePubKey; 13 | 14 | constructor(readonly xPrivKey, readonly mnemonic: string) { 15 | this.xPubKey = xPrivKey.hdPublicKey; 16 | this.walletId = sha256B64(this.xPubKey.toString()); 17 | this.devicePrivKey = HDKey.deriveDevicePrivKey(this.xPrivKey); 18 | this.devicePubKey = secp256k1.publicKeyCreate(this.devicePrivKey, true).toString('base64'); 19 | } 20 | 21 | async save(filePath: string) { 22 | const keystore: KeyStore = { 23 | mnemonic: this.mnemonic, 24 | }; 25 | await fs.mkdirp(path.dirname(filePath)); 26 | await fs.writeFile(filePath, JSON.stringify(keystore), 'utf-8'); 27 | } 28 | 29 | deriveBIP44(type: number, account: number, change: number, addressIndex: number) { 30 | const purpose = 44; 31 | return this.xPrivKey.derive(purpose, true).derive(type, true).derive(account, true) 32 | .derive(change) 33 | .derive(addressIndex); 34 | } 35 | 36 | derivePublicKey(isChange: number, addressIndex: number) { 37 | return this.xPubKey.derive(isChange).derive(addressIndex); 38 | } 39 | 40 | static deriveDevicePrivKey(xPrivKey): DevicePrivKey { 41 | return xPrivKey.derive(1, true).privateKey.bn.toBuffer({size: 32}); 42 | } 43 | 44 | deriveDeviceAddress(): DeviceAddress { 45 | return '0' + chash.getChash160(this.devicePubKey); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/common/HDKeys.ts: -------------------------------------------------------------------------------- 1 | import * as fs from 'fs-extra'; 2 | import * as rl from 'readline-sync'; 3 | import * as Mnemonic from 'bitcore-mnemonic'; 4 | import * as Bitcore from 'bitcore-lib'; 5 | import HDKey from './HDKey'; 6 | import * as conf from '../common/conf'; 7 | import logger from './log'; 8 | 9 | export type KeyStore = { 10 | mnemonic: string, 11 | }; 12 | 13 | export default class HDKeys { 14 | static async readOrCreate(passphrase?: string, path?: string): Promise { 15 | if (passphrase === undefined) { 16 | passphrase = rl.question('Passphrase: '); 17 | } 18 | if (path === undefined) { 19 | path = conf.KEY_STORE_PATH; 20 | } 21 | 22 | try { 23 | const data = await fs.readFile(path, 'utf8'); 24 | const keyStore: KeyStore = JSON.parse(data); 25 | return HDKeys.createByMnemonic(keyStore.mnemonic, passphrase); 26 | } catch (e) { 27 | logger.info(`failed to read key at ${path}, will gen by passphrase`); 28 | const key = HDKeys.createByPassphrase(passphrase); 29 | await key.save(path); 30 | return key; 31 | } 32 | } 33 | 34 | static createByPassphrase(passphrase: string): HDKey { 35 | let mnemonic = new Mnemonic(); // generates new mnemonic 36 | while (!Mnemonic.isValid(mnemonic.toString())) 37 | mnemonic = new Mnemonic(); 38 | 39 | const xPrivKey = mnemonic.toHDPrivateKey(passphrase); 40 | return new HDKey(xPrivKey, mnemonic.toString()); 41 | } 42 | 43 | static createByMnemonic(mnemonicPhrase: string, passphrase: string) { 44 | const mnemonic = new Mnemonic(mnemonicPhrase); 45 | const xPrivKey = mnemonic.toHDPrivateKey(passphrase); 46 | return new HDKey(xPrivKey, mnemonicPhrase); 47 | } 48 | 49 | static derivePubKey(xPubKeyStr: PubKey, isChange: number, addressIndex: number): PubKey { 50 | const xPubKey = new Bitcore.HDPublicKey(xPubKeyStr); 51 | return xPubKey.derive(isChange).derive(addressIndex).publicKey.toBuffer().toString('base64'); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/common/Signature.ts: -------------------------------------------------------------------------------- 1 | import * as ecdsa from 'secp256k1'; 2 | import * as _ from 'lodash'; 3 | import * as ohash from './object_hash'; 4 | 5 | export interface ISigned { 6 | signature: Base64; 7 | pubKey: PubKey; 8 | } 9 | 10 | export default class Signature { 11 | static sign(hash: string | Buffer, privateKey: Buffer): Base64 { 12 | const res = ecdsa.sign(hash, privateKey); 13 | return res.signature.toString('base64'); 14 | } 15 | 16 | static verify(hash: string, signature: Base64, pubKey: PubKey): boolean { 17 | try { 18 | const sig = new Buffer(signature, 'base64'); 19 | return ecdsa.verify(hash, sig, new Buffer(pubKey, 'base64')); 20 | } catch (e) { 21 | return false; 22 | } 23 | } 24 | 25 | static verifySigned(signed: ISigned, signature: Base64): boolean { 26 | const cloned = _.clone(signed); 27 | delete cloned.signature; 28 | return this.verify(ohash.getObjHashB64(cloned), signature, signed.pubKey); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/common/address.ts: -------------------------------------------------------------------------------- 1 | import * as chash from './checksum_hash'; 2 | 3 | export function deriveAddress(pubKey: PubKey): Address { 4 | return '0' + chash.getChash160(pubKey); 5 | } 6 | -------------------------------------------------------------------------------- /src/common/base32.ts: -------------------------------------------------------------------------------- 1 | const charTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; 2 | const byteTable = [ 3 | 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 4 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 5 | 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 6 | 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 7 | 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 8 | 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 9 | 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 10 | 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 11 | 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 12 | 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 13 | ]; 14 | 15 | function quintetCount(buff: Buffer) { 16 | const quintets = Math.floor(buff.length / 5); 17 | return buff.length % 5 === 0 ? quintets : quintets + 1; 18 | } 19 | 20 | function encode(plain: string | Buffer) { 21 | if (!Buffer.isBuffer(plain)) { 22 | plain = new Buffer(plain); 23 | } 24 | let i = 0; 25 | let j = 0; 26 | let shiftIndex = 0; 27 | let digit = 0; 28 | const encoded = new Buffer(quintetCount(plain) * 8); 29 | 30 | /* byte by byte isn't as pretty as quintet by quintet but tests a bit 31 | faster. will have to revisit. */ 32 | while (i < plain.length) { 33 | const current = plain[i]; 34 | 35 | if (shiftIndex > 3) { 36 | digit = current & (0xff >> shiftIndex); 37 | shiftIndex = (shiftIndex + 5) % 8; 38 | digit = (digit << shiftIndex) | ((i + 1 < plain.length) ? 39 | plain[i + 1] : 0) >> (8 - shiftIndex); 40 | i++; 41 | } else { 42 | digit = (current >> (8 - (shiftIndex + 5))) & 0x1f; 43 | shiftIndex = (shiftIndex + 5) % 8; 44 | if (shiftIndex === 0) { 45 | i++; 46 | } 47 | } 48 | 49 | encoded[j] = charTable.charCodeAt(digit); 50 | j++; 51 | } 52 | 53 | for (i = j; i < encoded.length; i++) { 54 | encoded[i] = 0x3d; // '='.charCodeAt(0) 55 | } 56 | 57 | return encoded; 58 | } 59 | 60 | function decode(encoded: string | Buffer) { 61 | let shiftIndex = 0; 62 | let plainDigit = 0; 63 | let plainChar; 64 | let plainPos = 0; 65 | if (!Buffer.isBuffer(encoded)) { 66 | encoded = new Buffer(encoded); 67 | } 68 | const decoded = new Buffer(Math.ceil(encoded.length * 5 / 8)); 69 | 70 | /* byte by byte isn't as pretty as octet by octet but tests a bit 71 | faster. will have to revisit. */ 72 | for (let i = 0; i < encoded.length; i++) { 73 | if (encoded[i] === 0x3d) { // '=' 74 | break; 75 | } 76 | 77 | const encodedByte = encoded[i] - 0x30; 78 | 79 | if (encodedByte < byteTable.length) { 80 | plainDigit = byteTable[encodedByte]; 81 | 82 | if (shiftIndex <= 3) { 83 | shiftIndex = (shiftIndex + 5) % 8; 84 | 85 | if (shiftIndex === 0) { 86 | plainChar |= plainDigit; 87 | decoded[plainPos] = plainChar; 88 | plainPos++; 89 | plainChar = 0; 90 | } else { 91 | plainChar |= 0xff & (plainDigit << (8 - shiftIndex)); 92 | } 93 | } else { 94 | shiftIndex = (shiftIndex + 5) % 8; 95 | plainChar |= 0xff & (plainDigit >>> shiftIndex); 96 | decoded[plainPos] = plainChar; 97 | plainPos++; 98 | 99 | plainChar = 0xff & (plainDigit << (8 - shiftIndex)); 100 | } 101 | } else { 102 | throw new Error('Invalid input - it is not base32 encoded string'); 103 | } 104 | } 105 | 106 | return decoded.slice(0, plainPos); 107 | } 108 | 109 | export default {encode, decode}; 110 | -------------------------------------------------------------------------------- /src/common/checksum_hash.test.ts: -------------------------------------------------------------------------------- 1 | import {getChash160} from './checksum_hash'; 2 | 3 | test('test checksum hash', () => { 4 | console.log(getChash160('oho')); 5 | }); 6 | 7 | -------------------------------------------------------------------------------- /src/common/checksum_hash.ts: -------------------------------------------------------------------------------- 1 | import base32 from './base32'; 2 | import * as hash from './hash'; 3 | 4 | const PI = '14159265358979323846264338327950288419716939937510'; 5 | const zeroString = '00000000'; 6 | 7 | const arrRelativeOffsets = PI.split(''); 8 | 9 | // 160: RIPEMD160 10 | // 288: sha256(256 bit) + checksum(4 * 8 = 32 bit, [5, 13, 21, 29]) = 288 bit 11 | function checkLength(chashLength: number) { 12 | if (chashLength !== 160 && chashLength !== 288) { 13 | throw Error(`unsupported c-hash length: ${chashLength}`); 14 | } 15 | } 16 | 17 | function calcOffsets(chashLength: number) { 18 | checkLength(chashLength); 19 | const arrOffsets = []; 20 | let offset = 0; 21 | let index = 0; 22 | 23 | for (let i = 0; offset < chashLength; i++) { 24 | const relativeOffset = parseInt(arrRelativeOffsets[i], 10); 25 | if (relativeOffset === 0) { 26 | continue; 27 | } 28 | offset += relativeOffset; 29 | if (chashLength === 288) { 30 | offset += 4; 31 | } 32 | if (offset >= chashLength) { 33 | break; 34 | } 35 | arrOffsets.push(offset); 36 | index++; 37 | } 38 | 39 | if (index !== 32) { 40 | throw Error('wrong number of checksum bits'); 41 | } 42 | return arrOffsets; 43 | } 44 | 45 | const arrOffsets160 = calcOffsets(160); 46 | const arrOffsets288 = calcOffsets(288); 47 | 48 | function separateIntoCleanDataAndChecksum(bin: string) { 49 | const len = bin.length; 50 | let arrOffsets; 51 | if (len === 160) { 52 | arrOffsets = arrOffsets160; 53 | } else if (len === 288) { 54 | arrOffsets = arrOffsets288; 55 | } else { 56 | throw new Error(`bad length=${len}, bin=${bin}`); 57 | } 58 | const arrFrags = []; 59 | const arrChecksumBits = []; 60 | let start = 0; 61 | for (const offset of arrOffsets) { 62 | arrFrags.push(bin.substring(start, offset)); 63 | arrChecksumBits.push(bin.substr(offset, 1)); 64 | start = offset + 1; 65 | } 66 | // add last frag 67 | if (start < bin.length) { 68 | arrFrags.push(bin.substring(start)); 69 | } 70 | const bincleanData = arrFrags.join(''); 71 | const binChecksum = arrChecksumBits.join(''); 72 | return {clean_data: bincleanData, checksum: binChecksum}; 73 | } 74 | 75 | function mixChecksumIntoCleanData(bincleanData: string, binChecksum: string) { 76 | if (binChecksum.length !== 32) { 77 | throw new Error('bad checksum length'); 78 | } 79 | const len = bincleanData.length + binChecksum.length; 80 | let arrOffsets; 81 | if (len === 160) { 82 | arrOffsets = arrOffsets160; 83 | } else if (len === 288) { 84 | arrOffsets = arrOffsets288; 85 | } else { 86 | throw new Error(`bad length=${len}, clean data=${bincleanData}, checksum=${binChecksum}`); 87 | } 88 | const arrFrags = []; 89 | const arrChecksumBits = binChecksum.split(''); 90 | let start = 0; 91 | for (let i = 0; i < arrOffsets.length; i++) { 92 | const end = arrOffsets[i] - i; 93 | arrFrags.push(bincleanData.substring(start, end)); 94 | arrFrags.push(arrChecksumBits[i]); 95 | start = end; 96 | } 97 | // add last frag 98 | if (start < bincleanData.length) { 99 | arrFrags.push(bincleanData.substring(start)); 100 | } 101 | return arrFrags.join(''); 102 | } 103 | 104 | function buffer2bin(buf: Buffer): string { 105 | const bytes = []; 106 | for (let i = 0; i < buf.length; i++) { 107 | let bin = buf[i].toString(2); 108 | if (bin.length < 8) { 109 | // pad with zeros 110 | bin = zeroString.substring(bin.length, 8) + bin; 111 | } 112 | bytes.push(bin); 113 | } 114 | return bytes.join(''); 115 | } 116 | 117 | function bin2buffer(bin: string): Buffer { 118 | const len = bin.length / 8; 119 | const buf = new Buffer(len); 120 | for (let i = 0; i < len; i++) { 121 | buf[i] = parseInt(bin.substr(i * 8, 8), 2); 122 | } 123 | return buf; 124 | } 125 | 126 | function getChecksum(cleanData: Buffer): Buffer { 127 | const fullChecksum = hash.sha256(cleanData); 128 | return new Buffer([fullChecksum[5], fullChecksum[13], fullChecksum[21], fullChecksum[29]]); 129 | } 130 | 131 | function getChash(data: string, chashLength: number) { 132 | checkLength(chashLength); 133 | const buffer = ((chashLength === 160) ? hash.ripemd160(data) : hash.sha256(data)); 134 | const truncatedHash = (chashLength === 160) ? buffer.slice(4) : buffer; // drop first 4 bytes if 160 135 | const checksum = getChecksum(truncatedHash); 136 | 137 | const binCleanData = buffer2bin(new Buffer(truncatedHash)); 138 | const binChecksum = buffer2bin(checksum); 139 | const binChash = mixChecksumIntoCleanData(binCleanData, binChecksum); 140 | const chash = bin2buffer(binChash); 141 | return (chashLength === 160) ? base32.encode(chash).toString() : chash.toString('base64'); 142 | } 143 | 144 | export function getChash160(data: string): string { 145 | return getChash(data, 160); 146 | } 147 | 148 | export function getChash288(data: string): string { 149 | return getChash(data, 288); 150 | } 151 | 152 | // 判断chash是否为合法chash 153 | // RIPEMD160: 160 bit, base32 编码 -> 160/5 = 32Byte 154 | // SHA256 + checksum: 288 bit, base64 编码 -> 288 / 6 -> 48Byte 155 | // 验证checksum 156 | export function isChashValid(encoded: string): boolean { 157 | const encodedLen = encoded.length; 158 | if (encodedLen !== 32 && encodedLen !== 48) { 159 | // 160/5 = 32, 288/6 = 48 160 | throw new Error('wrong encoded length: ' + encodedLen); 161 | } 162 | let chash; 163 | try { 164 | chash = (encodedLen === 32) ? base32.decode(encoded) : new Buffer(encoded, 'base64'); 165 | } catch (e) { 166 | return false; 167 | } 168 | const binChash = buffer2bin(chash); 169 | const separated = separateIntoCleanDataAndChecksum(binChash); 170 | const cleanData = bin2buffer(separated.clean_data); 171 | const checksum = bin2buffer(separated.checksum); 172 | return checksum.equals(getChecksum(cleanData)); 173 | } 174 | -------------------------------------------------------------------------------- /src/common/common.ts: -------------------------------------------------------------------------------- 1 | type Address = string; 2 | type DeviceAddress = string; 3 | type PubKey = string; 4 | type DevicePrivKey = Buffer; 5 | type DevicePubKey = string; 6 | type Hex = string; 7 | type Base64 = string; 8 | -------------------------------------------------------------------------------- /src/common/conf.ts: -------------------------------------------------------------------------------- 1 | import {getAppDataDir} from './desktop'; 2 | 3 | export let version = '1.0'; 4 | export let alt = '1.0'; 5 | 6 | export const COUNT_WITNESSES = 12; 7 | export const MAX_WITNESS_LIST_MUTATIONS = 1; 8 | export const TOTAL = 1e15; 9 | export const MAJORITY_OF_WITNESSES = 10 | (COUNT_WITNESSES % 2 === 0) ? (COUNT_WITNESSES / 2 + 1) : Math.ceil(COUNT_WITNESSES / 2); 11 | export const COUNT_MC_BALLS_FOR_PAID_WITNESSING = 100; 12 | 13 | export const GENESIS_UNIT = 14 | (alt === '2' && version === '1.0t') ? 15 | 'TvqutGPz3T4Cs6oiChxFlclY92M2MvCvfXR5/FETato=' : 'oj8yEksX9Ubq7lLc+p6F2uyHUuynugeVq4+ikT67X6E='; 16 | 17 | export const HASH_LENGTH = 44; // 256-bit hash | base64 18 | export const PUBKEY_LENGTH = 44; // 256-bit hash | base64 19 | export const SIG_LENGTH = 88; 20 | 21 | // anti-spam limits 22 | export const MAX_PARENTS_PER_UNIT = 16; 23 | export const MAX_INPUTS_PER_PAYMENT_MESSAGE = 128; 24 | export const MAX_OUTPUTS_PER_PAYMENT_MESSAGE = 128; 25 | 26 | export const APP_DATA_DIR = getAppDataDir(); 27 | export const KEY_STORE_PATH = `${APP_DATA_DIR}/keystore.json`; 28 | 29 | 30 | export const PEER_CONF = { 31 | LISTEN_PORT: Number(process.env['LISTEN_PORT']) || 3000, 32 | POOL_SEEDS: (process.env['POOL_SEEDS'] || '').split(',') || [], 33 | }; 34 | -------------------------------------------------------------------------------- /src/common/desktop.test.ts: -------------------------------------------------------------------------------- 1 | import {getAppDataDir} from './desktop'; 2 | 3 | test('test desktop', () => { 4 | console.log(process.mainModule); 5 | console.log(`app data dir: ${getAppDataDir()}`); 6 | }); 7 | -------------------------------------------------------------------------------- /src/common/desktop.ts: -------------------------------------------------------------------------------- 1 | import * as fs from 'fs'; 2 | import * as path from 'path'; 3 | 4 | // app data dir inside user's home directory 5 | export function getAppDataDir() { 6 | return `${getAppsDataDir()}/itc`; 7 | } 8 | 9 | function getAppsDataDir() { 10 | switch (process.platform) { 11 | case 'win32': 12 | return process.env.LOCALAPPDATA; 13 | case 'linux': 14 | return process.env.HOME + '/.config'; 15 | case 'darwin': 16 | return process.env.HOME + '/Library/Application Support'; 17 | default: 18 | throw Error(`unknown platform ${process.platform}`); 19 | } 20 | } 21 | 22 | function getPackageJsonDir(startDir: string) { 23 | try { 24 | fs.accessSync(startDir + '/package.json'); 25 | return startDir; 26 | } catch (e) { 27 | const parentDir = path.dirname(startDir); 28 | if (parentDir === '/' || process.platform === 'win32' && parentDir.match(/^\w:[\/\\]/)) 29 | throw Error('no package.json found'); 30 | return getPackageJsonDir(parentDir); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/common/encrypt.ts: -------------------------------------------------------------------------------- 1 | import * as crypto from 'crypto'; 2 | import {ECDH} from 'crypto'; 3 | 4 | export type EncryptedPackage = { 5 | encrypted_message: string, 6 | iv: Base64, 7 | authtag: Base64, 8 | dh: { 9 | sender_ephemeral_pubkey: PubKey, 10 | recipient_ephemeral_pubkey: PubKey, 11 | }, 12 | }; 13 | 14 | export interface IEncrypt { 15 | encryptMessage(json: any, pubKey: PubKey); 16 | 17 | decryptPackage(encrypted: EncryptedPackage, pubKey: PubKey, privateKey: DevicePrivKey); 18 | } 19 | 20 | function deriveSharedSecret(ecdh: ECDH, peerPubKey: PubKey): Buffer { 21 | const sharedSecretSrc = ecdh.computeSecret(peerPubKey, 'base64'); 22 | return crypto.createHash('sha256').update(sharedSecretSrc).digest().slice(0, 16); 23 | } 24 | 25 | 26 | class Encrypt implements IEncrypt { 27 | encryptMessage(obj: any, pubKey: PubKey) { 28 | const json = JSON.stringify(obj); 29 | const ecdh = crypto.createECDH('secp256k1'); 30 | const senderEphemeralPubKey = ecdh.generateKeys('base64', 'compressed'); 31 | const sharedSecret = deriveSharedSecret(ecdh, pubKey); 32 | 33 | // we could also derive iv from the unused bits of ecdh.computeSecret() and save some bandwidth 34 | // 128 bits (16 bytes) total, we take 12 bytes for random iv and leave 4 bytes for the counter 35 | const iv = crypto.randomBytes(12); 36 | const cipher = crypto.createCipheriv('aes-128-gcm', sharedSecret, iv); 37 | // under browserify, encryption of long strings fails with Array buffer allocation errors 38 | // have to split the string into chunks 39 | const arrChunks = []; 40 | const CHUNK_LENGTH = 2003; 41 | for (let offset = 0; offset < json.length; offset += CHUNK_LENGTH) { 42 | // console.log('offset '+offset); 43 | arrChunks.push(cipher.update(json.slice(offset, Math.min(offset + CHUNK_LENGTH, json.length)), 'utf8')); 44 | } 45 | arrChunks.push(cipher.final()); 46 | const encryptedMessageBuf = Buffer.concat(arrChunks); 47 | const encryptedMessage = encryptedMessageBuf.toString('base64'); 48 | //console.log(encrypted_message); 49 | const authtag = cipher.getAuthTag(); 50 | // this is visible and verifiable by the hub 51 | return { 52 | encrypted_message: encryptedMessage, 53 | iv: iv.toString('base64'), 54 | authtag: authtag.toString('base64'), 55 | dh: { 56 | sender_ephemeral_pubkey: senderEphemeralPubKey, 57 | recipient_ephemeral_pubkey: pubKey, 58 | }, 59 | }; 60 | } 61 | 62 | decryptPackage(encrypted: EncryptedPackage, pubKey: PubKey, privateKey: DevicePrivKey): any { 63 | const ecdh = crypto.createECDH('secp256k1'); 64 | ecdh.setPrivateKey(privateKey); 65 | const sharedSecret = deriveSharedSecret(ecdh, encrypted.dh.sender_ephemeral_pubkey); 66 | const iv = new Buffer(encrypted.iv, 'base64'); 67 | const decipher = crypto.createDecipheriv('aes-128-gcm', sharedSecret, iv); 68 | const authtag = new Buffer(encrypted.authtag, 'base64'); 69 | decipher.setAuthTag(authtag); 70 | const encBuf = new Buffer(encrypted.encrypted_message, 'base64'); 71 | const chunks = []; 72 | const CHUNK_LENGTH = 4096; 73 | for (let offset = 0; offset < encBuf.length; offset += CHUNK_LENGTH) { 74 | chunks.push(decipher.update(encBuf.slice(offset, Math.min(offset + CHUNK_LENGTH, encBuf.length)))); 75 | } 76 | const decrypted1 = Buffer.concat(chunks); 77 | const decrypted2 = decipher.final(); 78 | const decryptedMessageBuf = Buffer.concat([decrypted1, decrypted2]); 79 | const decryptedMessage = decryptedMessageBuf.toString('utf8'); 80 | const json = JSON.parse(decryptedMessage); 81 | if (json.encrypted_package) { 82 | // strip another layer of encryption 83 | return this.decryptPackage(json.encrypted_package, pubKey, privateKey); 84 | } else { 85 | return json; 86 | } 87 | } 88 | } 89 | 90 | const encrypt = new Encrypt(); 91 | export default encrypt; 92 | -------------------------------------------------------------------------------- /src/common/hash.test.ts: -------------------------------------------------------------------------------- 1 | import * as hash from './hash'; 2 | test('test hash', () => { 3 | const text = 'oho'; 4 | expect(hash.sha256B64(text).length).toBe(44); 5 | expect(hash.sha256Hex(text).length).toBe(64); 6 | }); 7 | -------------------------------------------------------------------------------- /src/common/hash.ts: -------------------------------------------------------------------------------- 1 | import * as crypto from 'crypto'; 2 | 3 | export function ripemd160(data: string | Buffer): Buffer { 4 | return crypto.createHash('ripemd160').update(data, 'utf8').digest(); 5 | } 6 | 7 | export function sha256Hex(data: string | Buffer): Hex { 8 | return crypto.createHash('sha256').update(data, 'utf8').digest('hex'); 9 | } 10 | 11 | export function sha256B64(data: string | Buffer): Base64 { 12 | return crypto.createHash('sha256').update(data, 'utf8').digest('base64'); 13 | } 14 | 15 | export function sha256(data: string | Buffer): Buffer { 16 | return crypto.createHash('sha256').update(data, 'utf8').digest(); 17 | } 18 | -------------------------------------------------------------------------------- /src/common/hdkeys.test.ts: -------------------------------------------------------------------------------- 1 | import HDKeys from './HDKeys'; 2 | 3 | test('test hdkeys', async () => { 4 | const key = await HDKeys.readOrCreate(''); 5 | console.log(key); 6 | }); 7 | -------------------------------------------------------------------------------- /src/common/log.ts: -------------------------------------------------------------------------------- 1 | import * as pino from 'pino'; 2 | 3 | const logger = pino({prettyPrint: true}); 4 | export default logger; 5 | -------------------------------------------------------------------------------- /src/common/mail.ts: -------------------------------------------------------------------------------- 1 | import * as childProcess from 'child_process'; 2 | 3 | export function sendMail(from: string, to: string, subject: string, body: string) { 4 | sendMailThroughUnixSendMail(from, to, subject, body); 5 | } 6 | 7 | function sendMailThroughUnixSendMail(from: string, to: string, subject: string, body: string) { 8 | const child = childProcess.spawn('/usr/sbin/sendmail', ['-t', to]); 9 | child.stdout.pipe(process.stdout); 10 | child.stderr.pipe(process.stderr); 11 | child.stdin.write(`Return-path: ${from}> 12 | To: ${to} 13 | From: ${from} 14 | Subject: ${subject} 15 | 16 | 17 | ${body}`); 18 | child.stdin.end(); 19 | } 20 | -------------------------------------------------------------------------------- /src/common/merkle.ts: -------------------------------------------------------------------------------- 1 | import * as hash from './hash'; 2 | 3 | export type MerkleProof = { 4 | root: Base64, 5 | siblings: Base64[], 6 | index: number, 7 | }; 8 | 9 | export function getMerkleRoot(elements: any[]): Base64 { 10 | let hashes = elements.map(hash.sha256B64); 11 | while (hashes.length > 1) { 12 | const overHashes = []; // hashes over hashes 13 | for (let i = 0; i < hashes.length; i += 2) { 14 | const hashIndex = (i + 1 < hashes.length) ? (i + 1) : i; // for odd number of hashes 15 | overHashes.push(hash.sha256B64(hashes[i] + hashes[hashIndex])); 16 | } 17 | hashes = overHashes; 18 | } 19 | return hashes[0]; 20 | } 21 | 22 | export function getMerkleProof(elements: any[], elementIndex: number): MerkleProof { 23 | let hashes = elements.map(hash.sha256B64); 24 | let index = elementIndex; 25 | const siblings = []; 26 | while (hashes.length > 1) { 27 | const overHashes = []; // hashes over hashes 28 | let overIndex = null; 29 | for (let i = 0; i < hashes.length; i += 2) { 30 | const hashIndex = (i + 1 < hashes.length) ? (i + 1) : i; // for odd number of hashes 31 | if (i === index) { 32 | siblings.push(hashes[hashIndex]); 33 | overIndex = i / 2; 34 | } else if (hashIndex === index) { 35 | siblings.push(hashes[i]); 36 | overIndex = i / 2; 37 | } 38 | overHashes.push(hash.sha256B64(hashes[i] + hashes[hashIndex])); 39 | } 40 | hashes = overHashes; 41 | if (overIndex === null) 42 | throw Error('overIndex not defined'); 43 | index = overIndex; 44 | } 45 | return { 46 | root: hashes[0], 47 | siblings: siblings, 48 | index: elementIndex, 49 | }; 50 | } 51 | 52 | // returns a string element_index-siblings_joined_by_dash-root 53 | export function serializeMerkleProof(proof: MerkleProof): string { 54 | let serializedProof = `${proof.index}`; 55 | if (proof.siblings.length > 0) 56 | serializedProof += '-' + proof.siblings.join('-'); 57 | serializedProof += '-' + proof.root; 58 | return serializedProof; 59 | } 60 | 61 | export function deserializeMerkleProof(serializedProof: string) { 62 | const arr = serializedProof.split('-'); 63 | const proof: any = {}; 64 | proof.root = arr.pop(); 65 | proof.index = arr.shift(); 66 | proof.siblings = arr; 67 | return proof; 68 | } 69 | 70 | export function verifyMerkleProof(element: any, proof): boolean { 71 | let index = proof.index; 72 | let theOtherSibling = hash.sha256B64(element); 73 | for (let i = 0; i < proof.siblings.length; i++) { 74 | // this also works for duplicated trailing nodes 75 | if (index % 2 === 0) 76 | theOtherSibling = hash.sha256B64(theOtherSibling + proof.siblings[i]); 77 | else 78 | theOtherSibling = hash.sha256B64(proof.siblings[i] + theOtherSibling); 79 | index = Math.floor(index / 2); 80 | } 81 | return theOtherSibling === proof.root; 82 | } 83 | -------------------------------------------------------------------------------- /src/common/object_hash.test.ts: -------------------------------------------------------------------------------- 1 | import * as ohash from './object_hash'; 2 | 3 | test('test object hash', () => { 4 | const obj = { 5 | 'key': 'oho', 6 | }; 7 | const hash = ohash.getObjHashB64(obj); 8 | console.log(hash); 9 | }); 10 | -------------------------------------------------------------------------------- /src/common/object_hash.ts: -------------------------------------------------------------------------------- 1 | import * as _ from 'lodash'; 2 | import * as chash from './checksum_hash'; 3 | import * as hash from './hash'; 4 | import {getSourceString} from './string_utils'; 5 | import Unit from '../core/unit'; 6 | import logger from './log'; 7 | 8 | export function getChash160(obj: any): string { 9 | return chash.getChash160(getSourceString(obj)); 10 | } 11 | 12 | export function getChash288(obj: any): string { 13 | return chash.getChash288(getSourceString(obj)); 14 | } 15 | 16 | export function getObjHashB64(obj: any): Base64 { 17 | return hash.sha256B64(getSourceString(obj)); 18 | } 19 | 20 | export function getObjHashHex(obj: any): Hex { 21 | return hash.sha256Hex(getSourceString(obj)); 22 | } 23 | 24 | function getNakedUnit(unit: Unit): any { 25 | const nakedMessages = []; 26 | for (const message of unit.messages) { 27 | nakedMessages.push({ 28 | 'app': message.app, 29 | 'payloadHash': message.payloadHash, 30 | 'payloadLocation': message.payloadLocation, 31 | }); 32 | } 33 | 34 | const naked = { 35 | 'version': unit.version, 36 | 'alt': unit.alt, 37 | 'witnesses': unit.witnesses, 38 | 'messages': nakedMessages, 39 | 'authors': unit.authors, 40 | }; 41 | 42 | logger.info(naked, 'naked'); 43 | return naked; 44 | } 45 | 46 | export function getUnitContentHash(unit: Unit): Base64 { 47 | return getObjHashB64(getNakedUnit(unit)); 48 | } 49 | 50 | export function getUnitHash(unit: Unit): Base64 { 51 | const objStrippedUnit: any = { 52 | alt: unit.alt, 53 | authors: unit.authors.map(author => { 54 | return {address: author.address}; 55 | }), 56 | contentHash: getUnitContentHash(unit), 57 | version: unit.version, 58 | }; 59 | if (unit.witnessListUnit) { 60 | objStrippedUnit.witnessListUnit = unit.witnessListUnit; 61 | } else { 62 | objStrippedUnit.witnesses = unit.witnesses; 63 | } 64 | if (unit.parentUnits && unit.parentUnits.length > 0) { 65 | objStrippedUnit.parentUnits = unit.parentUnits; 66 | objStrippedUnit.lastBall = unit.lastBall; 67 | objStrippedUnit.lastBallUnit = unit.lastBallUnit; 68 | } 69 | 70 | return getObjHashB64(objStrippedUnit); 71 | } 72 | 73 | export function getUnitHashToSign(objUnit: any): Buffer { 74 | const objNakedUnit = getNakedUnit(objUnit); 75 | return hash.sha256(getSourceString(objNakedUnit)); 76 | } 77 | 78 | export function getBallHash(unit: any, arrParentBalls: any, arrSkiplistBalls: any, bNonserial: any): Base64 { 79 | const objBall: any = {unit}; 80 | if (arrParentBalls && arrParentBalls.length > 0) { 81 | objBall.parentBalls = arrParentBalls; 82 | } 83 | if (arrSkiplistBalls && arrSkiplistBalls.length > 0) { 84 | objBall.skiplistBalls = arrSkiplistBalls; 85 | } 86 | if (bNonserial) { 87 | objBall.isNonserial = true; 88 | } 89 | return getObjHashB64(objBall); 90 | } 91 | 92 | // export function getJointHash(joint: Joint): Base64 { 93 | // // we use JSON.stringify, we can't use objectHash here because it might throw errors 94 | // return hash.sha256B64(JSON.stringify(joint)); 95 | // } 96 | 97 | function cleanNulls(obj: any) { 98 | Object.keys(obj).forEach((key) => { 99 | if (obj[key] === null) { 100 | delete obj[key]; 101 | } 102 | }); 103 | } 104 | 105 | export function pubKeyToAddress(pubKey: PubKey): Address { 106 | return ('0' + getChash160(pubKey)); 107 | } 108 | 109 | function getDeviceMessageHashToSign(objDeviceMessage: any): Buffer { 110 | const objNakedDeviceMessage = _.clone(objDeviceMessage); 111 | delete objNakedDeviceMessage.signature; 112 | return hash.sha256(getSourceString(objNakedDeviceMessage)); 113 | } 114 | -------------------------------------------------------------------------------- /src/common/object_length.test.ts: -------------------------------------------------------------------------------- 1 | import * as olength from './object_length'; 2 | 3 | test('test object length', () => { 4 | const obj = { 5 | str: 'oho', 6 | arr: [1, 2, 3], 7 | num: 42, 8 | }; 9 | 10 | const len = olength.getLength(obj); 11 | console.log(len); 12 | }); 13 | -------------------------------------------------------------------------------- /src/common/object_length.ts: -------------------------------------------------------------------------------- 1 | import Unit from '../core/unit'; 2 | 3 | const PARENT_UNITS_SIZE = 2 * 44; // 不管实际上有多少个parents,固定取两个,鼓励取尽量多的parents 4 | 5 | export function getHeadersSize(unit: Unit): number { 6 | if (unit.contentHash) 7 | throw Error('trying to get headers size of stripped unit'); 8 | 9 | const header = { 10 | 'witnesses': unit.witnesses, 11 | 'witnessListUnit': unit.witnessListUnit, 12 | 'authors': unit.authors, 13 | 'version': unit.version, 14 | 'alt': unit.alt, 15 | }; 16 | 17 | return getLength(header) + PARENT_UNITS_SIZE; 18 | } 19 | 20 | export function getTotalPayloadSize(unit: Unit): number { 21 | if (unit.contentHash) 22 | throw Error('trying to get payload size of stripped unit'); 23 | return getLength(unit.messages); 24 | } 25 | 26 | export function getLength(value: any): number { 27 | if (value === null) 28 | return 0; 29 | switch (typeof value) { 30 | case 'string': 31 | return value.length; 32 | case 'number': 33 | return 8; 34 | //return value.toString().length; 35 | case 'object': 36 | let len = 0; 37 | if (Array.isArray(value)) 38 | value.forEach(function (element) { 39 | len += getLength(element); 40 | }); 41 | else 42 | for (const key in value) { 43 | if (typeof value[key] === 'undefined') 44 | throw Error('undefined at ' + key + ' of ' + JSON.stringify(value)); 45 | len += getLength(value[key]); 46 | } 47 | return len; 48 | case 'boolean': 49 | return 1; 50 | default: 51 | throw Error('unknown type=' + (typeof value) + ' of ' + value); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/common/signature.test.ts: -------------------------------------------------------------------------------- 1 | test('test signature', () => { 2 | }); 3 | -------------------------------------------------------------------------------- /src/common/string_utils.ts: -------------------------------------------------------------------------------- 1 | const STRING_JOIN_CHAR = '\x00'; 2 | 3 | /** 4 | * Converts the argument into a string by mapping data types to a prefixed string and concatenating all fields together. 5 | * @param obj the value to be converted into a string 6 | * @returns {string} the string version of the value 7 | */ 8 | export function getSourceString(obj: any): string { 9 | const arrComponents: any = []; 10 | 11 | function extractComponents(variable: any) { 12 | if (variable === null) { 13 | throw Error('null value in ' + JSON.stringify(obj)); 14 | } 15 | switch (typeof variable) { 16 | case 'string': 17 | arrComponents.push('s', variable); 18 | break; 19 | case 'number': 20 | arrComponents.push('n', variable.toString()); 21 | break; 22 | case 'boolean': 23 | arrComponents.push('b', variable.toString()); 24 | break; 25 | case 'object': 26 | if (Array.isArray(variable)) { 27 | if (variable.length === 0) { 28 | throw Error('empty array in ' + JSON.stringify(obj)); 29 | } 30 | arrComponents.push('['); 31 | for (const v of variable) { 32 | extractComponents(v); 33 | } 34 | arrComponents.push(']'); 35 | } else { 36 | const keys = Object.keys(variable).sort(); 37 | if (keys.length === 0) { 38 | throw Error('empty object in ' + JSON.stringify(obj)); 39 | } 40 | keys.forEach((key) => { 41 | if (variable[key] === undefined) { 42 | throw Error('undefined at ' + key + ' of ' + JSON.stringify(obj)); 43 | } 44 | arrComponents.push(key); 45 | extractComponents(variable[key]); 46 | }); 47 | } 48 | break; 49 | default: 50 | throw Error('hash: unknown type=' + (typeof variable) + ' of ' + variable + ', object: ' 51 | + JSON.stringify(obj)); 52 | } 53 | } 54 | 55 | extractComponents(obj); 56 | return arrComponents.join(STRING_JOIN_CHAR); 57 | } 58 | -------------------------------------------------------------------------------- /src/common/timeoutPromise.ts: -------------------------------------------------------------------------------- 1 | export default function (promise, ms: number = 0) { 2 | const timeout = new Promise((resolve, reject) => { 3 | const id = setTimeout(() => { 4 | clearTimeout(id); 5 | reject(`Timed out in ${ms} ms.`); 6 | }, ms); 7 | }); 8 | 9 | return Promise.race([promise, timeout]); 10 | } 11 | -------------------------------------------------------------------------------- /src/common/utils.ts: -------------------------------------------------------------------------------- 1 | export function sleep(ms: number) { 2 | return new Promise(resolve => setTimeout(resolve, ms)); 3 | } 4 | -------------------------------------------------------------------------------- /src/common/validation_utils.ts: -------------------------------------------------------------------------------- 1 | import * as chash from './checksum_hash'; 2 | 3 | /** 4 | * True if there is at least one field in obj that is not in arrFields. 5 | */ 6 | export function hasFieldsExcept(obj: any, fields: string[]): boolean { 7 | for (const field in obj) 8 | if (fields.indexOf(field) === -1) 9 | return true; 10 | return false; 11 | } 12 | 13 | /** 14 | * ES6 Number.isInteger Ponyfill. 15 | * 16 | * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger 17 | */ 18 | export function isInteger(value: any): boolean { 19 | return typeof value === 'number' && isFinite(value) && Math.floor(value) === value; 20 | } 21 | 22 | /** 23 | * True if int is an integer strictly greater than zero. 24 | */ 25 | export function isPositiveInteger(int: any): boolean { 26 | return (isInteger(int) && int > 0); 27 | } 28 | 29 | /** 30 | * True if int is an integer greater than or equal to zero. 31 | */ 32 | export function isNonnegativeInteger(int: any): boolean { 33 | return (isInteger(int) && int >= 0); 34 | } 35 | 36 | /** 37 | * True if str is a string and not the empty string. 38 | */ 39 | export function isNonemptyString(str: any): boolean { 40 | return (typeof str === 'string' && str.length > 0); 41 | } 42 | 43 | /** 44 | * True if str is a string and has length len. False if len not provided. 45 | */ 46 | export function isStringOfLength(str: any, len: number): boolean { 47 | return (typeof str === 'string' && str.length === len); 48 | } 49 | 50 | export function isValidChash(str: any, len: number): boolean { 51 | return (isStringOfLength(str, len) && chash.isChashValid(str)); 52 | } 53 | 54 | export function isValidAddressAnyCase(address: Address): boolean { 55 | return isValidChash(address, 32); 56 | } 57 | 58 | export function isValidAddress(address: Address): boolean { 59 | return (typeof address === 'string' && address === address.toUpperCase() && isValidChash(address, 32)); 60 | } 61 | 62 | export function isValidDeviceAddress(address: Address): boolean { 63 | return (isStringOfLength(address, 33) && address[0] === '0' && isValidAddress(address.substr(1))); 64 | } 65 | 66 | export function isNonemptyArray(arr: any[]): boolean { 67 | return (Array.isArray(arr) && arr.length > 0); 68 | } 69 | 70 | export function isArrayOfLength(arr: any[], len: number): boolean { 71 | return (Array.isArray(arr) && arr.length === len); 72 | } 73 | 74 | export function isNonemptyObject(obj: any): boolean { 75 | return (obj && typeof obj === 'object' && !Array.isArray(obj) && Object.keys(obj).length > 0); 76 | } 77 | 78 | export function isValidBase64(b64: Base64, len: number): boolean { 79 | return (b64.length === len && b64 === (new Buffer(b64, 'base64')).toString('base64')); 80 | } 81 | -------------------------------------------------------------------------------- /src/core/authentifiers.ts: -------------------------------------------------------------------------------- 1 | // when a user signs a unit, she must provide 2 | // a set of authentifiers which makes definition of address valid 3 | export type Authentifiers = { 4 | [path: string]: string, 5 | }; 6 | -------------------------------------------------------------------------------- /src/core/author.ts: -------------------------------------------------------------------------------- 1 | import {Authentifiers} from './authentifiers'; 2 | 3 | export default class Author { 4 | // only when a author sends her first unit from an address(UTXO) 5 | // her must reveal the address's definition(address is just a hash of definition) 6 | 7 | constructor( 8 | readonly address: Address, 9 | readonly authentifiers: Authentifiers, 10 | readonly definition?: any[], 11 | ) { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/core/balances.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import logger from '../common/log'; 3 | 4 | type Balance = { 5 | stable: number, 6 | pending: number, 7 | }; 8 | 9 | export type Balances = { 10 | [key: string]: Balance; 11 | }; 12 | 13 | export async function readBalance(address: Address): Promise { 14 | const balances = { 15 | 'base': {stable: 0, pending: 0}, 16 | }; 17 | 18 | let rows = await sqlstore.all(` 19 | SELECT asset, is_stable, SUM(amount) AS balance 20 | FROM outputs CROSS JOIN units USING(unit) 21 | WHERE is_spent=0 AND address=? AND sequence='good' 22 | GROUP BY asset, is_stable`, 23 | address, 24 | ); 25 | 26 | for (let i = 0; i < rows.length; i++) { 27 | const row = rows[i]; 28 | balances['base'][row.is_stable ? 'stable' : 'pending'] = row.balance; 29 | } 30 | 31 | 32 | rows = await sqlstore.all(` 33 | SELECT SUM(total) AS total FROM ( 34 | SELECT SUM(amount) AS total FROM witnessing_outputs WHERE is_spent=0 AND address=? 35 | UNION ALL 36 | SELECT SUM(amount) AS total FROM headers_commission_outputs WHERE is_spent=0 AND address=? ) AS t`, 37 | address, address, 38 | ); 39 | 40 | if (rows.length > 0) { 41 | balances['base'].stable += rows[0].total; 42 | } 43 | return balances; 44 | } 45 | -------------------------------------------------------------------------------- /src/core/composeParent.ts: -------------------------------------------------------------------------------- 1 | import sqlstore, {SqliteStore} from '../storage/sqlstore'; 2 | import * as conf from '../common/conf'; 3 | import logger from '../common/log'; 4 | import Units from '../models/Units'; 5 | 6 | type LastStable = { 7 | ball: string, 8 | unit: string, 9 | mci: number, 10 | }; 11 | 12 | async function pickParentUnits(witnesses: Base64[]) { 13 | const rows = await sqlstore.all(` 14 | SELECT unit, version, alt, ( 15 | SELECT count(*) FROM unit_witnesses 16 | WHERE unit_witnesses.unit IN(units.unit, units.witness_list_unit) AND 17 | address IN(?) 18 | ) AS count_matching_witnesses 19 | FROM units 20 | WHERE +sequence='good' AND is_free=1 ORDER BY unit LIMIT ?`, 21 | witnesses, conf.MAX_PARENTS_PER_UNIT, 22 | ); 23 | return rows.map(row => row.unit); 24 | } 25 | 26 | async function findLastStableMcBall(witnesses: Address[]): Promise { 27 | const rows = await sqlstore.all(` 28 | SELECT ball, unit, main_chain_index FROM units JOIN balls USING(unit) 29 | WHERE is_on_main_chain=1 AND is_stable=1 AND +sequence='good' AND ( 30 | SELECT COUNT(*) 31 | FROM unit_witnesses 32 | WHERE unit_witnesses.unit IN(units.unit, units.witness_list_unit) 33 | )>=? 34 | ORDER BY main_chain_index DESC LIMIT 1`, 35 | conf.COUNT_WITNESSES - conf.MAX_WITNESS_LIST_MUTATIONS, 36 | ); 37 | 38 | return { 39 | ball: rows[0].ball, 40 | unit: rows[0].unit, 41 | mci: rows[0].main_chain_index, 42 | }; 43 | } 44 | 45 | async function trimParentList(parentUnits: Base64[], witnesses: Address[]): Promise { 46 | if (parentUnits.length <= conf.MAX_PARENTS_PER_UNIT) 47 | return parentUnits; 48 | 49 | const rows = await sqlstore.all(` 50 | SELECT unit, (SELECT 1 FROM unit_authors WHERE unit_authors.unit=units.unit AND address IN(?) LIMIT 1) AS is_witness 51 | FROM units WHERE unit IN(${parentUnits.map(SqliteStore.escape).join(',')}) ORDER BY is_witness DESC, RANDOM() LIMIT ?`, 52 | witnesses, conf.MAX_PARENTS_PER_UNIT, 53 | ); 54 | return rows.map(row => row.unit).sort(); 55 | } 56 | 57 | export default async function composeParent(witnesses: Address[]): Promise<[Base64[], LastStable]> { 58 | const parents = await pickParentUnits(witnesses); 59 | logger.info({parents}, 'pickParentUnits'); 60 | const lastStable = await findLastStableMcBall(witnesses); 61 | logger.info({lastStable}, 'findLastStableMcBall'); 62 | const trimmed = await trimParentList(parents, witnesses); 63 | return [trimmed, lastStable]; 64 | } 65 | -------------------------------------------------------------------------------- /src/core/composer.test.ts: -------------------------------------------------------------------------------- 1 | import {composeGenesisUnit} from './composer'; 2 | import Wallets from '../wallets/Wallets'; 3 | import MyAddresses from '../models/MyAddresses'; 4 | import Unit from './unit'; 5 | import Units from '../models/Units'; 6 | import sqlstore from '../storage/sqlstore'; 7 | 8 | test('test genesis composer', async () => { 9 | const witnesses = ['1']; 10 | const wallet = await Wallets.readOrCreate(''); 11 | await MyAddresses.issueOrSelectNextAddress(wallet.wallet); 12 | const unit = await composeGenesisUnit(witnesses, wallet); 13 | console.log(JSON.stringify(unit, null, 2)); 14 | console.log(Unit.validateUnit(unit)); 15 | 16 | await Units.save(unit, 'good'); 17 | console.log(await sqlstore.all('select * from units')); 18 | }); 19 | -------------------------------------------------------------------------------- /src/core/composer.ts: -------------------------------------------------------------------------------- 1 | import * as _ from 'lodash'; 2 | import {Signer} from './signer'; 3 | import {Input, IssueInput, Message, Output, TransferInput} from './message'; 4 | import Unit from './unit'; 5 | import Author from './author'; 6 | import sqlstore from '../storage/sqlstore'; 7 | import * as conf from '../common/conf'; 8 | import composeParent from './composeParent'; 9 | import Witnesses from '../models/Witnesses'; 10 | import Wallet from '../wallets/wallet'; 11 | import {Authentifiers} from './authentifiers'; 12 | import {MyWitnesses} from '../models/MyWitnesses'; 13 | import logger from '../common/log'; 14 | import {Inputs} from '../models/Messages'; 15 | 16 | function composeGenesisInputs(): Input[] { 17 | const issueInput: IssueInput = { 18 | type: 'issue', 19 | serialNumber: 1, 20 | amount: conf.TOTAL, 21 | }; 22 | return [issueInput]; 23 | } 24 | 25 | function composeGenesisOutputs(witnesses: Address[]): Output[] { 26 | const per = conf.TOTAL / conf.COUNT_WITNESSES; 27 | return witnesses.map(witness => new Output(witness, per)); 28 | } 29 | 30 | export async function composeGenesisUnit(witnesses: Address[], wallet: Wallet): Promise { 31 | const inputs = composeGenesisInputs(); 32 | const outputs = composeGenesisOutputs(witnesses); 33 | const message = Message.newPaymentMessage(inputs, outputs); 34 | 35 | const address = await wallet.address(); 36 | const authors = await composeAuthors([address], [wallet.key.deriveDeviceAddress()], wallet.signer); 37 | 38 | const unit = new Unit( 39 | [], 40 | null, 41 | null, 42 | null, 43 | authors, 44 | witnesses, 45 | [message], 46 | ); 47 | 48 | for (const author of authors) { 49 | author.authentifiers['r'] = await wallet.signer.sign(unit, author.address, 'r'); 50 | } 51 | 52 | unit.unit = unit.calcUnit(); 53 | unit.ball = unit.calcBall(); 54 | return unit; 55 | } 56 | 57 | export async function composeInputs( 58 | targetAmount: number, 59 | payingAddresses: Address[], 60 | lastBallMCI: number, 61 | ): Promise<[TransferInput[], number]> { 62 | return readUtxoForAmount(payingAddresses, lastBallMCI, targetAmount); 63 | } 64 | 65 | export function composeOutputs(outputs: Output[], changeAddress: Address, change: number): Output[] { 66 | const changeOutput = new Output(changeAddress, change); 67 | return [changeOutput].concat(outputs.filter(output => output.amount > 0)); 68 | } 69 | 70 | export async function composeAuthors(addresses: Address[], deviceAddresses: DeviceAddress[], signer: Signer): Promise { 71 | return await Promise.all(addresses.map(async (address) => { 72 | const definition = await signer.readDefinitions(address); 73 | const signingPathLengths = await signer.readSigningPaths(address, deviceAddresses); 74 | 75 | const authentifiers: Authentifiers = {}; 76 | for (const [path, length] of signingPathLengths) { 77 | authentifiers[path] = '-'.repeat(length); 78 | } 79 | return new Author(address, authentifiers, definition); 80 | })); 81 | } 82 | 83 | export async function composeWitnessListUnit(witnesses: Address[], lastBallMCI: number): Promise
{ 84 | return await Witnesses.findWitnessListUnit(witnesses, lastBallMCI); 85 | } 86 | 87 | export async function composeUnit( 88 | witnesses: Address[], 89 | signingAddresses: Address[], 90 | payingAddresses: Address[], 91 | changeAddress: Address, 92 | outputs: Output[], 93 | signer: Signer, 94 | ) { 95 | const fromAddresses = _.union(signingAddresses, payingAddresses).sort(); 96 | 97 | const [parentUnits, lastStable] = await composeParent(witnesses); 98 | const witnessListUnit = await composeWitnessListUnit(witnesses, lastStable.mci); 99 | if (!witnessListUnit && !witnesses) { 100 | witnesses = await MyWitnesses.readWitnesses(); 101 | } 102 | logger.info({witnesses}, 'witnesses'); 103 | const authors = await composeAuthors(fromAddresses, [], signer); 104 | logger.info({authors}, 'composeAuthors'); 105 | const externalOutputs: Output[] = outputs.filter(output => output.amount > 0); 106 | const outputAmount = externalOutputs.reduce((acc, cur) => acc + cur.amount, 0); 107 | const [inputs, inputAmount] = await composeInputs(outputAmount, payingAddresses, lastStable.mci); 108 | logger.info({inputs, inputAmount}, 'composeInputs'); 109 | const headersCommission = 0; 110 | const payloadCommission = 0; 111 | const changeAmount = inputAmount - outputAmount - headersCommission - payloadCommission; 112 | const finalOutputs = composeOutputs(outputs, changeAddress, changeAmount); 113 | logger.info({finalOutputs}, 'composeOutputs'); 114 | const message = new Message('payment', 'inline', inputs, finalOutputs); 115 | return new Unit( 116 | parentUnits, 117 | lastStable.ball, 118 | lastStable.unit, 119 | witnessListUnit, 120 | authors, 121 | witnesses, 122 | [message], 123 | ); 124 | } 125 | 126 | async function readUtxo(addresses: Address[], mci: number, amount?: number, limit: number = 1) { 127 | const requireAmount = amount ? `AND amount > ${amount}` : ''; 128 | return sqlstore.all(` 129 | SELECT unit, message_index, output_index, amount, blinding, address 130 | FROM outputs 131 | CROSS JOIN units USING(unit) 132 | WHERE address IN(${sqlstore.escape(addresses)}) AND is_spent=0 ${requireAmount} 133 | AND is_stable=1 AND sequence='good' AND main_chain_index<=? 134 | ORDER BY amount LIMIT ${limit}`, 135 | mci, 136 | ); 137 | } 138 | 139 | async function readUtxoForAmount(addresses: Address[], lastBallMCI: number, amount: number): Promise<[TransferInput[], number]> { 140 | let totalAmount = 0; 141 | const inputsWithProofs = []; 142 | 143 | // first, try to find an output just bigger than the required amount 144 | let inputs = await readUtxo(addresses, lastBallMCI, amount, 1); 145 | if (inputs.length === 0) { 146 | // then, try to add smaller coins until we accumulate the required amount 147 | inputs = await readUtxo(addresses, lastBallMCI, null, conf.MAX_INPUTS_PER_PAYMENT_MESSAGE - 2); 148 | } 149 | 150 | for (const input of inputs) { 151 | totalAmount += input.amount; 152 | inputsWithProofs.push(Inputs.newTransferInput(input.unit, input.message_index, input.output_index)); 153 | if (totalAmount > amount) { 154 | return [inputsWithProofs, totalAmount]; 155 | } 156 | } 157 | 158 | return [inputsWithProofs, totalAmount]; 159 | 160 | // still not enough, try to add earned header commission 161 | // TBD 162 | 163 | // still not enough, try to add witness commission 164 | // TBD 165 | // async function addHeadersCommissionInputs() { 166 | // return addMcInputs( 167 | // 'headers_commission', 168 | // HEADERS_COMMISSION_INPUT_SIZE, 169 | // headerCommission.getMaxSpendableMciForLastBallMci(lastBallMCI), 170 | // ); 171 | // } 172 | // 173 | // async function addWitnessingInputs() { 174 | // return addMcInputs( 175 | // 'witnessing', 176 | // WITNESSING_INPUT_SIZE, 177 | // payloadCommission.getMaxSpendableMciForLastBallMci(lastBallMCI), 178 | // ); 179 | // } 180 | // 181 | // async function addMcInputs(type: string, inputSize: number, maxMCI: number) { 182 | // await Promise.all(addresses.map(async (address) => { 183 | // const targetAmount = amount + inputSize - totalAmount; 184 | // const [from, to, earnings, isSufficient] = 185 | // await mcOutputs.findMcIndexIntervalToTargetAmount(type, address, maxMCI, targetAmount); 186 | // totalAmount += earnings; 187 | // const input = { 188 | // type: type, 189 | // from_main_chain_index: from, 190 | // to_main_chain_index: to, 191 | // }; 192 | // amount += inputSize; 193 | // inputWithProofs.push({input: input}); 194 | // })); 195 | // } 196 | } 197 | 198 | -------------------------------------------------------------------------------- /src/core/definition.ts: -------------------------------------------------------------------------------- 1 | import * as _ from 'lodash'; 2 | 3 | export default class Definition { 4 | static replaceTemplate(templates: any[], params: any) { 5 | function replaceInVar(x) { 6 | switch (typeof x) { 7 | case 'number': 8 | case 'boolean': 9 | return x; 10 | case 'string': 11 | // searching for pattern "$name" 12 | if (x.charAt(0) !== '$') 13 | return x; 14 | const name = x.substring(1); 15 | if (!(name in params)) 16 | throw Error('variable ' + name + ' not specified'); 17 | return params[name]; // may change type if params[name] is not a string 18 | case 'object': 19 | if (Array.isArray(x)) 20 | for (let i = 0; i < x.length; i++) 21 | x[i] = replaceInVar(x[i]); 22 | else 23 | for (const key in x) 24 | x[key] = replaceInVar(x[key]); 25 | return x; 26 | default: 27 | throw Error('unknown type'); 28 | } 29 | } 30 | 31 | return replaceInVar(_.cloneDeep(templates)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/core/device.test.ts: -------------------------------------------------------------------------------- 1 | import {Device} from './device'; 2 | 3 | test('test device', () => { 4 | const device1 = new Device(); 5 | const device2 = new Device(); 6 | 7 | console.log(device1); 8 | console.log(device2); 9 | }); 10 | -------------------------------------------------------------------------------- /src/core/device.ts: -------------------------------------------------------------------------------- 1 | import logger from '../common/log'; 2 | import sqlstore from '../storage/sqlstore'; 3 | import signature from '../common/Signature'; 4 | import * as hash from '../common/hash'; 5 | import encrypt from '../common/encrypt'; 6 | import * as address from '../common/address'; 7 | import * as crypto from 'crypto'; 8 | import * as secp256k1 from 'secp256k1'; 9 | import * as objectHash from '../common/object_hash'; 10 | import WebSocketClient from '../network/WebSocketClient'; 11 | import network from '../network/Peer'; 12 | 13 | type TempPubKey = { 14 | tempPubKey: PubKey; 15 | pubKey: PubKey; 16 | signature: Base64; 17 | }; 18 | 19 | export class Device { 20 | private _permanentPrivateKey: DevicePrivKey; 21 | private _permanentPubKey: PubKey; 22 | private _ephemeralPrivateKey: DevicePrivKey; 23 | private _ephemeralPubKey: PubKey; 24 | private _deviceAddress: Address; 25 | private _deviceName: string; 26 | private _deviceHub: string; 27 | 28 | constructor(privateKey?: DevicePrivKey) { 29 | if (!privateKey) { 30 | privateKey = this.genPrivateKey(); 31 | } 32 | this._permanentPrivateKey = privateKey; 33 | this._permanentPubKey = secp256k1.publicKeyCreate(privateKey, true).toString('base64'); 34 | this._deviceAddress = address.deriveAddress(this._permanentPubKey); 35 | } 36 | 37 | permanentPrivateKey(): DevicePrivKey { 38 | return this._permanentPrivateKey; 39 | } 40 | 41 | permanentPubKey(): PubKey { 42 | return this._permanentPubKey; 43 | } 44 | 45 | ephemeralPrivateKey(): DevicePrivKey { 46 | return this._ephemeralPrivateKey; 47 | } 48 | 49 | ephemeralPubKey(): PubKey { 50 | return this._ephemeralPubKey; 51 | } 52 | 53 | deviceAddress(): Address { 54 | return this._deviceAddress; 55 | } 56 | 57 | deviceName(): string { 58 | return this._deviceName; 59 | } 60 | 61 | deviceHub(): string { 62 | return this._deviceHub; 63 | } 64 | 65 | genPrivateKey(): DevicePrivKey { 66 | let privKey; 67 | do { 68 | privKey = crypto.randomBytes(32); 69 | } 70 | while (!secp256k1.privateKeyVerify(privKey)); 71 | return privKey; 72 | } 73 | 74 | async sendMessageToDevice(to: Address, subject: string, body: any) { 75 | const rows = await sqlstore.all( 76 | 'SELECT hub, pubkey FROM correspondent_devices WHERE device_address=?', 77 | [to], 78 | ); 79 | if (rows.length !== 1) { 80 | throw new Error('correspondent not found'); 81 | } 82 | return this.sendMessageToHub(rows[0].hub, rows[0].pubkey, subject, body); 83 | } 84 | 85 | async sendMessageToHub(ws: WebSocketClient | string | null, recipientPubkey: PubKey, subject: any, body: any): Promise { 86 | const obj = { 87 | from: this.deviceAddress, 88 | device_hub: this.deviceHub, 89 | subject: subject, 90 | body: body, 91 | }; 92 | 93 | if (ws) { 94 | return this._reliablySendPreparedMessageToHub(ws, recipientPubkey, obj); 95 | } 96 | // derive address based on pubkey 97 | const to = address.deriveAddress(recipientPubkey); 98 | const rows = await sqlstore.all(`SELECT hub FROM correspondent_devices where device_address=?`, [to]); 99 | if (rows.length !== 1) { 100 | throw new Error('no hub in correspondents'); 101 | } 102 | return this._reliablySendPreparedMessageToHub(rows[0].hub, recipientPubkey, obj); 103 | } 104 | 105 | async _reliablySendPreparedMessageToHub(ws: WebSocketClient | string, recipientPubKey: PubKey, obj: any) { 106 | const to = address.deriveAddress(recipientPubKey); 107 | logger.info(`will encrypt and send to ${to}: ${obj}`); 108 | const encryptedMessage = encrypt.encryptMessage(obj, recipientPubKey); 109 | const message = { 110 | encrypted_package: encryptedMessage, 111 | }; 112 | const messageHash = objectHash.getObjHashB64(message); 113 | await sqlstore.run( 114 | `INSERT INTO outbox (message_hash, to_address, message) VALUES(?,?,?)`, 115 | [messageHash, to, JSON.stringify(message)], 116 | ); 117 | return this._sendPreparedMessageToHub(ws, recipientPubKey, messageHash, obj); 118 | } 119 | 120 | async _sendPreparedMessageToHub(ws: WebSocketClient | string, recipientPubKey: PubKey, messageHash: Base64, json: string) { 121 | if (typeof ws === 'string') { 122 | ws = await network.getOrCreateClient(ws); 123 | } 124 | let resp: TempPubKey; 125 | try { 126 | resp = await network.sendRequest(ws, 'hub/get_temp_pubkey', recipientPubKey); 127 | } catch (e) { 128 | return await sqlstore.run(`UPDATE outbox SET last_error=? WHERE message=?`, [e, messageHash]); 129 | } 130 | 131 | if (!resp.tempPubKey || !resp.pubKey || !resp.signature) { 132 | throw new Error('missing fields in hub response'); 133 | } 134 | 135 | if (resp.pubKey !== recipientPubKey) { 136 | throw new Error('temp pubkey signed by wrong permanent pubkey'); 137 | } 138 | 139 | if (!signature.verifySigned(resp, resp.signature)) { 140 | throw new Error('wrong sig under temp pubkey'); 141 | } 142 | 143 | const encryptedMessage = encrypt.encryptMessage(json, recipientPubKey); 144 | const to = address.deriveAddress(recipientPubKey); 145 | 146 | const content: any = { 147 | encrypted_package: encryptedMessage, 148 | to: to, 149 | pubkey: this.permanentPubKey, 150 | }; 151 | 152 | content.signature = signature.sign(hash.sha256B64(content), this.permanentPrivateKey()); 153 | 154 | const response = await network.sendRequest(ws, 'hub/deliver', content); 155 | if (response === 'accepted') { 156 | return await sqlstore.run(`DELETE FROM outbox WHERE message_hash=?`, [messageHash]); 157 | } else { 158 | throw (response.error || new Error(`unrecognized response: ${response}`)); 159 | } 160 | } 161 | 162 | async sendPairingMessage(hub: string, recipientPubKey: PubKey, pairingSecret: string, reversePairingSecret: string) { 163 | const body: any = {pairing_secret: pairingSecret, device_name: this.deviceName()}; 164 | if (reversePairingSecret) 165 | body.reverse_pairing_secret = reversePairingSecret; 166 | return this.sendMessageToHub(hub, recipientPubKey, 'pairing', body); 167 | } 168 | } 169 | 170 | const device = new Device(); 171 | export default device; 172 | 173 | -------------------------------------------------------------------------------- /src/core/genesis.ts: -------------------------------------------------------------------------------- 1 | import * as conf from '../common/conf'; 2 | import * as ohash from '../common/object_hash'; 3 | import Unit from './unit'; 4 | 5 | const genesisBall = ohash.getBallHash(conf.GENESIS_UNIT, null, null, null); 6 | 7 | export function isGenesisUnit(unit: Unit): boolean { 8 | // return (unit === conf.GENESIS_UNIT); 9 | // TODO: change later 10 | return !unit.parentUnits || unit.parentUnits.length === 0; 11 | } 12 | 13 | export function isGenesisBall(ball: Base64): boolean { 14 | return (ball === genesisBall); 15 | } 16 | -------------------------------------------------------------------------------- /src/core/graph.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import * as assert from 'assert'; 3 | import * as genesis from '../core/genesis'; 4 | import * as _ from 'lodash'; 5 | 6 | export async function compareUnits(unit1: Base64, unit2: Base64) { 7 | if (unit1 === unit2) 8 | return 0; 9 | const rows = await sqlstore.all( 10 | 'SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free FROM units WHERE unit IN(?)', 11 | [unit1, unit2], 12 | ); 13 | assert(rows.length === 2); 14 | const unitProps1 = (rows[0].unit === unit1) ? rows[0] : rows[1]; 15 | const unitProps2 = (rows[0].unit === unit2) ? rows[0] : rows[1]; 16 | return compareUnitsByProps(unitProps1, unitProps2); 17 | } 18 | 19 | export async function compareUnitsByProps(unitProps1: any, unitProps2: any) { 20 | if (unitProps1.unit === unitProps2.unit) 21 | return 0; 22 | if (unitProps1.level === unitProps2.level) 23 | return null; 24 | if (unitProps1.is_free === 1 && unitProps2.is_free === 1) // free units 25 | return null; 26 | 27 | // genesis 28 | if (unitProps1.latest_included_mc_index === null) 29 | return -1; 30 | if (unitProps2.latest_included_mc_index === null) 31 | return +1; 32 | 33 | if (unitProps1.latest_included_mc_index >= unitProps2.main_chain_index && unitProps2.main_chain_index !== null) 34 | return +1; 35 | if (unitProps2.latest_included_mc_index >= unitProps1.main_chain_index && unitProps1.main_chain_index !== null) 36 | return -1; 37 | 38 | if (unitProps1.level <= unitProps2.level 39 | && unitProps1.latest_included_mc_index <= unitProps2.latest_included_mc_index 40 | && (unitProps1.main_chain_index <= unitProps2.main_chain_index 41 | && unitProps1.main_chain_index !== null && unitProps2.main_chain_index !== null 42 | || unitProps1.main_chain_index === null || unitProps2.main_chain_index === null) 43 | || 44 | unitProps1.level >= unitProps2.level 45 | && unitProps1.latest_included_mc_index >= unitProps2.latest_included_mc_index 46 | && (unitProps1.main_chain_index >= unitProps2.main_chain_index 47 | && unitProps1.main_chain_index !== null && unitProps2.main_chain_index !== null 48 | || unitProps1.main_chain_index === null || unitProps2.main_chain_index === null) 49 | ) { 50 | // still can be comparable 51 | } else 52 | return null; 53 | 54 | const earlierUnit = (unitProps1.level < unitProps2.level) ? unitProps1 : unitProps2; 55 | const laterUnit = (unitProps1.level < unitProps2.level) ? unitProps2 : unitProps1; 56 | const resultIfFound = (unitProps1.level < unitProps2.level) ? -1 : 1; 57 | 58 | // can be negative if main_chain_index === null but that doesn't matter 59 | const earlierUnitDelta = earlierUnit.main_chain_index - earlierUnit.latest_included_mc_index; 60 | const laterUnitDelta = laterUnit.main_chain_index - laterUnit.latest_included_mc_index; 61 | 62 | async function goUp(startUnits) { 63 | const rows = await sqlstore.all(` 64 | SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain 65 | FROM parenthoods JOIN units ON parent_unit=unit 66 | WHERE child_unit IN(?)`, 67 | startUnits, 68 | ); 69 | const newStartUnits = []; 70 | for (let i = 0; i < rows.length; i++) { 71 | const unitProps = rows[i]; 72 | if (unitProps.unit === earlierUnit.unit) 73 | return resultIfFound; 74 | if (unitProps.is_on_main_chain === 0 && unitProps.level > earlierUnit.level) 75 | newStartUnits.push(unitProps.unit); 76 | } 77 | if (newStartUnits.length > 0) { 78 | return goUp(startUnits); 79 | } else { 80 | return null; 81 | } 82 | } 83 | 84 | async function goDown(startUnits) { 85 | const rows = await sqlstore.all(` 86 | SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain 87 | FROM parenthoods JOIN units ON child_unit=unit 88 | WHERE parent_unit IN(?)`, 89 | startUnits, 90 | ); 91 | const newStartUnits = []; 92 | for (let i = 0; i < rows.length; i++) { 93 | const unitProps = rows[i]; 94 | if (unitProps.unit === laterUnit.unit) 95 | return resultIfFound; 96 | if (unitProps.is_on_main_chain === 0 && unitProps.level < laterUnit.level) 97 | newStartUnits.push(unitProps.unit); 98 | } 99 | if (newStartUnits.length > 0) { 100 | return goDown(newStartUnits); 101 | } else { 102 | return null; 103 | } 104 | } 105 | 106 | if (laterUnitDelta > earlierUnitDelta) { 107 | return goUp([laterUnit.unit]); 108 | } else { 109 | return goDown([earlierUnit.unite]); 110 | } 111 | } 112 | 113 | /** 114 | * determines if earlierUnit is included by at least one of LaterUnits 115 | */ 116 | export async function determineIfIncluded(earlierUnit: Base64, laterUnits: Base64[]) { 117 | if (await genesis.isGenesisUnit(earlierUnit)) 118 | return true; 119 | 120 | const rows = await sqlstore.all(` 121 | SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free FROM units WHERE unit IN(?, ?)`, 122 | [earlierUnit, laterUnits], 123 | ); 124 | let earlierUnitProps; 125 | const laterUnitProps = []; 126 | for (let i = 0; i < rows.length; i++) { 127 | if (rows[i].unit === earlierUnit) 128 | earlierUnitProps = rows[i]; 129 | else 130 | laterUnitProps.push(rows[i]); 131 | } 132 | 133 | if (earlierUnitProps.is_free === 1) 134 | return false; 135 | 136 | const maxLaterLimci = Math.max.apply(null, laterUnitProps.map(p => p.latest_included_mc_index)); 137 | if (earlierUnitProps.main_chain_index !== null && maxLaterLimci >= earlierUnitProps.main_chain_index) { 138 | return true; 139 | } 140 | 141 | const maxLaterLevel = Math.max.apply(null, laterUnitProps.map(p => p.level)); 142 | 143 | if (maxLaterLevel < earlierUnitProps.level) 144 | return false; 145 | 146 | async function goUp(startUnits) { 147 | const rows = await sqlstore.all(` 148 | SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain 149 | FROM parenthoods JOIN units ON parent_unit=unit 150 | WHERE child_unit IN(?)`, 151 | startUnits, 152 | ); 153 | 154 | const newStartUnits = []; 155 | for (let i = 0; i < rows.length; i++) { 156 | const unitProps = rows[i]; 157 | if (unitProps.unit === earlierUnit) 158 | return true; 159 | if (unitProps.is_on_main_chain === 0 && unitProps.level > earlierUnitProps.level) 160 | newStartUnits.push(unitProps.unit); 161 | } 162 | if (newStartUnits.length > 0) { 163 | return goUp(_.uniq(newStartUnits)); 164 | } else { 165 | return false; 166 | } 167 | } 168 | 169 | return goUp(laterUnits); 170 | } 171 | 172 | export async function determineIfIncludedOrEqual(earlierUnit: Base64, laterUnits: Base64[]) { 173 | if (laterUnits.indexOf(earlierUnit) >= 0) 174 | return true; 175 | return determineIfIncluded(earlierUnit, laterUnits); 176 | } 177 | -------------------------------------------------------------------------------- /src/core/headers_commission.ts: -------------------------------------------------------------------------------- 1 | /*jslint node: true */ 2 | import sqlstore from '../storage/sqlstore'; 3 | import * as hash from '../common/hash'; 4 | 5 | let maxSpendableMCI = null; 6 | 7 | export async function calcHeadersCommissions() { 8 | // we don't require neither source nor recipient to be majority witnessed 9 | // we don't want to return many times to the same MC index. 10 | if (maxSpendableMCI === null) {// first calc after restart only 11 | maxSpendableMCI = await initMaxSpendableMci(); 12 | } 13 | // max_spendable_mci is old, it was last updated after previous calc 14 | const sinceMCI = maxSpendableMCI; 15 | 16 | // chunits is any child unit and contender for headers commission, punits is hc-payer unit 17 | const rows = await sqlstore.all(` 18 | SELECT chunits.unit AS child_unit, punits.headers_commission, next_mc_units.unit AS next_mc_unit, punits.unit AS payer_unit 19 | FROM units AS chunits 20 | JOIN parenthoods ON chunits.unit=parenthoods.child_unit 21 | JOIN units AS punits ON parenthoods.parent_unit=punits.unit 22 | JOIN units AS next_mc_units ON next_mc_units.is_on_main_chain=1 AND next_mc_units.main_chain_index=punits.main_chain_index+1 23 | WHERE chunits.is_stable=1 24 | AND +chunits.sequence='good' 25 | AND punits.main_chain_index>? 26 | AND +punits.sequence='good' 27 | AND punits.is_stable=1 28 | AND chunits.main_chain_index-punits.main_chain_index<=1 29 | AND next_mc_units.is_stable=1`, 30 | sinceMCI, 31 | ); 32 | 33 | const childrenInfos = {}; 34 | rows.forEach(async (row) => { 35 | const payerUnit = row.payer_unit; 36 | const childUnit = row.child_unit; 37 | if (!childUnit[payerUnit]) { 38 | childUnit[payerUnit] = { 39 | headers_commission: row.headers_commission, 40 | children: [], 41 | }; 42 | } else if (childrenInfos[payerUnit].headers_commission !== row.headers_commission) { 43 | throw new Error('different headers_commission'); 44 | } 45 | delete row.headers_commission; 46 | delete row.payer_unit; 47 | childrenInfos[payerUnit].children.push(row); 48 | 49 | const wonAmounts = {}; 50 | for (const unit in childrenInfos) { 51 | const headersCommission = childrenInfos[payerUnit].headers_commission; 52 | const winnerChildInfo = getWinnerInfo(childrenInfos[payerUnit].children); 53 | const childUnit = winnerChildInfo.child_unit; 54 | if (!wonAmounts[childUnit]) 55 | wonAmounts[childUnit] = {}; 56 | wonAmounts[childUnit][payerUnit] = headersCommission; 57 | } 58 | 59 | const winnerUnits = Object.keys(wonAmounts); 60 | 61 | const rows = await sqlstore.all(` 62 | SELECT unit_authors.unit, unit_authors.address, 100 AS earned_headers_commission_share 63 | FROM unit_authors 64 | LEFT JOIN earned_headers_commission_recipients USING(unit) 65 | WHERE unit_authors.unit IN () AND 66 | earned_headers_commission_recipients.unit IS NULL 67 | UNION ALL 68 | SELECT unit, address, earned_headers_commission_share 69 | WHERE unit IN ()`, 70 | ); 71 | 72 | const values = []; 73 | rows.forEach(row => { 74 | const childUnit = row.unit; 75 | for (const payerUnit in wonAmounts[childUnit]) { 76 | const fullAmount = wonAmounts[childUnit][payerUnit]; 77 | const amount = (row.earned_headers_commission_share === 100) 78 | ? fullAmount 79 | : Math.round(fullAmount * row.earned_headers_commission_share / 100.0); 80 | values.push(`(${payerUnit}, ${row.address}, ${amount}`); 81 | } 82 | }); 83 | 84 | await sqlstore.run('INSERT INTO headers_commission_contributions (unit, address, amount) VALUES ' + values.join(', ')); 85 | }); 86 | } 87 | 88 | function getWinnerInfo(children: any[]) { 89 | if (children.length === 1) 90 | return children[0]; 91 | children.forEach((child) => { 92 | child.hsah = hash.sha256Hex(child.child_unit + child.next_mc_unit); 93 | }); 94 | children.sort((a, b) => { 95 | return ((a.hash < b.hash) ? -1 : 1); 96 | }); 97 | return children[0]; 98 | } 99 | 100 | async function initMaxSpendableMci() { 101 | const row = await sqlstore.get(` 102 | SELECT MAX(main_chain_index) AS max_spendable_mci FROM headers_commission_outputs`); 103 | return row.max_spendable_mci || 0; 104 | } 105 | 106 | export function getMaxSpendableMciForLastBallMci(lastBallMCI: number) { 107 | return lastBallMCI - 1; 108 | } 109 | -------------------------------------------------------------------------------- /src/core/mc_outputs.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | 3 | export async function readNextSpendableMcIndex(type: string, address: Address): Promise { 4 | const rows = await sqlstore.all(` 5 | SELECT to_main_chain_index FROM inputs CROSS JOIN units USING(unit) 6 | WHERE type=? AND address=? AND sequence='good' 7 | ORDER BY to_main_chain_index DESC LIMIT 1`, 8 | [type, address], 9 | ); 10 | 11 | return (rows.length > 0) ? rows[0].to_main_chain_index + 1 : 0; 12 | } 13 | 14 | export async function readMaxSpendableMcIndex(type: string): Promise { 15 | const table = type + '_outputs'; 16 | const mci = await sqlstore.get('SELECT MAX(main_chain_index) AS max_mc_index FROM ' + table); 17 | return mci.max_mc_index || 0; 18 | } 19 | 20 | export async function findMcIndexIntervalToTargetAmount(type: string, address: Address, maxMCI: number, targetAmount: number) { 21 | const table = type + '_outputs'; 22 | const fromMCI = await readNextSpendableMcIndex(type, address); 23 | if (fromMCI > maxMCI) { 24 | return []; 25 | } 26 | 27 | let maxSpendableMci = await readMaxSpendableMcIndex(type); 28 | if (maxSpendableMci <= 0) { 29 | return []; 30 | } 31 | if (maxSpendableMci >= maxMCI) { 32 | maxSpendableMci = maxMCI; 33 | } 34 | 35 | if (targetAmount === Infinity) 36 | targetAmount = 1e15; 37 | 38 | 39 | const MIN_MC_OUTPUT = (type === 'witnessing') ? 11 : 344; 40 | const maxCountOutputs = Math.ceil(targetAmount / MIN_MC_OUTPUT); 41 | const rows = await sqlstore.all(` 42 | SELECT main_chain_index, amount 43 | FROM ${table} 44 | WHERE is_spent=0 AND address=? AND main_chain_index>=? AND main_chain_index<=? 45 | ORDER BY main_chain_index LIMIT ?`, 46 | [address, fromMCI, maxSpendableMci, maxCountOutputs], 47 | ); 48 | 49 | if (rows.length === 0) { 50 | return []; 51 | } 52 | 53 | let accumulated = 0; 54 | let toMCI; 55 | let hasSufficient = false; 56 | 57 | for (let i = 0; i < rows.length; i++) { 58 | accumulated += rows[i].amount; 59 | toMCI = rows[i].main_chain_index; 60 | if (accumulated > targetAmount) { 61 | hasSufficient = true; 62 | break; 63 | } 64 | } 65 | 66 | return [fromMCI, toMCI, accumulated, hasSufficient]; 67 | } 68 | 69 | export async function calcEarnings(type: string, fromMCI: number, toMCI: number, address: Address): Promise { 70 | const table = type + '_outputs'; 71 | const total = (await sqlstore.get(` 72 | SELECT SUM(amount) AS total 73 | FROM ${table} 74 | WHERE main_chain_index>=? AND main_chain_index<=? AND address=?`, 75 | fromMCI, toMCI, address, 76 | )).total; 77 | 78 | return total; 79 | } 80 | -------------------------------------------------------------------------------- /src/core/message.ts: -------------------------------------------------------------------------------- 1 | import * as objectHash from '../common/object_hash'; 2 | 3 | export type Input = IssueInput | TransferInput | HeaderInput | WitnessInput; 4 | 5 | export type TransferInput = { 6 | type: 'transfer', 7 | unit: Base64, 8 | messageIndex: number, 9 | outputIndex: number, 10 | }; 11 | 12 | export type IssueInput = { 13 | type: 'issue', 14 | amount: number, 15 | serialNumber: number, 16 | address?: string, 17 | }; 18 | 19 | export type HeaderInput = { 20 | type: 'headers_commission', 21 | fromMCI: number, 22 | toMCI: number, 23 | }; 24 | 25 | export type WitnessInput = { 26 | type: 'witness_commission', 27 | fromMCI: number, 28 | toMCI: number, 29 | }; 30 | 31 | export class Output { 32 | constructor( 33 | readonly address: Address, 34 | readonly amount: number, 35 | ) { 36 | } 37 | } 38 | 39 | export type Payload = { 40 | inputs: Input[], 41 | outputs: Output[], 42 | }; 43 | 44 | export class Message { 45 | readonly payloadHash: string; 46 | readonly payload: Payload; 47 | 48 | constructor( 49 | readonly app: string, 50 | readonly payloadLocation: string, 51 | inputs: Input[], 52 | outputs: Output[], 53 | ) { 54 | this.payload = {inputs, outputs}; 55 | this.payloadHash = objectHash.getObjHashB64(this.payload); 56 | } 57 | 58 | static newPaymentMessage(inputs: Input[], outputs: Output[]): Message { 59 | return new Message('payment', 'inline', inputs, outputs); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/core/payload_commission.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import * as conf from '../common/conf'; 3 | import * as mcOutputs from '../core/mc_outputs'; 4 | 5 | export async function calcWitnessEarnings(type: string, fromMCI: number, toMCI: number, address: Address): Promise { 6 | const countRows = await sqlstore.get( 7 | `SELECT COUNT(*) AS count FROM units 8 | WHERE is_on_main_chain=1 AND is_stable=1 AND main_chain_index>=? AND main_chain_index<=?`, 9 | [toMCI, toMCI + conf.COUNT_MC_BALLS_FOR_PAID_WITNESSING + 1], 10 | ); 11 | 12 | if (countRows.count !== conf.COUNT_MC_BALLS_FOR_PAID_WITNESSING + 2) { 13 | throw new Error('not enough stable MC units after to_main_chain_index'); 14 | } 15 | return mcOutputs.calcEarnings(type, fromMCI, toMCI, address); 16 | } 17 | 18 | 19 | export function getMaxSpendableMciForLastBallMci(lastBallMCI: number) { 20 | return lastBallMCI - 1 - conf.COUNT_MC_BALLS_FOR_PAID_WITNESSING; 21 | } 22 | 23 | export async function updatePaidWitnesses() { 24 | return null; 25 | } 26 | -------------------------------------------------------------------------------- /src/core/signer.test.ts: -------------------------------------------------------------------------------- 1 | import Wallets from '../wallets/Wallets'; 2 | import MyAddresses from '../models/MyAddresses'; 3 | import XPubKeys from '../models/XPubKeys'; 4 | 5 | test('test signer', async() => { 6 | const wallet = await Wallets.readOrCreate(''); 7 | const address = await MyAddresses.issueOrSelectNextAddress(wallet.wallet); 8 | const defs = await wallet.signer.readDefinitions(address); 9 | console.log(defs); 10 | 11 | console.log(await XPubKeys.all()); 12 | console.log(wallet.key); 13 | console.log(wallet.key.deriveDeviceAddress()); 14 | 15 | const signingPaths = await wallet.signer.readSigningPaths(address, []); 16 | console.log(signingPaths); 17 | }); 18 | -------------------------------------------------------------------------------- /src/core/signer.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import * as objectHash from '../common/object_hash'; 3 | import Unit from './unit'; 4 | import * as conf from '../common/conf'; 5 | import Signature from '../common/Signature'; 6 | 7 | type Definition = string; 8 | 9 | export class Signer { 10 | constructor(readonly xPrivKey) { 11 | } 12 | 13 | signWithLocalPrivateKey( 14 | wallet: string, 15 | account: number, 16 | isChange: number, 17 | addressIndex: number, 18 | buffer: Buffer, 19 | ) { 20 | const derived = this.xPrivKey 21 | .derive(44, true) 22 | .derive(0, true) 23 | .derive(account, true) 24 | .derive(isChange) 25 | .derive(addressIndex) 26 | .privateKey; 27 | 28 | const privKeyBuf = derived.bn.toBuffer({size: 32}); // https://github.com/bitpay/bitcore-lib/issues/47 29 | return Signature.sign(buffer, privKeyBuf); 30 | } 31 | 32 | async readDefinitions(address: Address): Promise { 33 | const row = await sqlstore.get(` 34 | SELECT definition FROM my_addresses WHERE address=?`, 35 | address, 36 | ); 37 | return JSON.parse(row.definition); 38 | } 39 | 40 | async readSigningPaths(address: Address, signingDeviceAddresses: DeviceAddress[]): Promise> { 41 | const signingPathTypes = await readFullSigningPaths(address, signingDeviceAddresses); 42 | const signingLengths = new Map(); 43 | for (const key in signingPathTypes) { 44 | if (signingPathTypes[key] === 'key') { 45 | signingLengths.set(key, conf.SIG_LENGTH); 46 | } else { 47 | signingLengths.set(key, 1); 48 | } 49 | } 50 | return signingLengths; 51 | } 52 | 53 | async sign(unit: Unit, address: Address, path: any): Promise { 54 | const bufToSign = objectHash.getUnitHashToSign(unit); 55 | const addressObj = await findAddress(address, path); 56 | return this.signWithLocalPrivateKey( 57 | addressObj.wallet, 0, addressObj.is_change, addressObj.address_index, bufToSign); 58 | } 59 | } 60 | 61 | async function findAddress(address: Address, signingPath: any) { 62 | const rows = await sqlstore.all(` 63 | SELECT wallet, account, is_change, address_index, full_approval_date, device_address 64 | FROM my_addresses JOIN wallets USING(wallet) JOIN wallet_signing_paths USING(wallet) 65 | WHERE address=? AND signing_path=?`, 66 | address, signingPath, 67 | ); 68 | 69 | const row = rows[0]; 70 | return { 71 | address: address, 72 | wallet: row.wallet, 73 | account: row.key, 74 | is_change: row.is_change, 75 | address_index: row.address_index, 76 | }; 77 | } 78 | 79 | // returns a map of signing_path => (key|merkle) 80 | async function readFullSigningPaths(address: Address, signingDeviceAddresses: Address[]): Promise> { 81 | const signingPaths = new Map(); 82 | 83 | async function goDeeper(memberAddress: Address, pathPrefix: string) { 84 | let sql = `SELECT signing_path FROM my_addresses JOIN wallet_signing_paths USING(wallet) WHERE address=?`; 85 | const params: any[] = [memberAddress]; 86 | if (signingDeviceAddresses && signingDeviceAddresses.length > 0) { 87 | sql += ` AND device_address IN(?)`; 88 | params.push(signingDeviceAddresses); 89 | } 90 | const rows = await sqlstore.all(sql, params); 91 | if (rows.length > 0) { 92 | await Promise.all(rows.map(async (row) => { 93 | if (row.address === '') { // merkle 94 | return signingPaths[pathPrefix + row.signingPath.substr(1)] = 'merkle'; 95 | } else { 96 | return await goDeeper(row.address, pathPrefix + row.signingPath.substr(1)); 97 | } 98 | })); 99 | } else { 100 | signingPaths[pathPrefix] = 'key'; 101 | } 102 | } 103 | 104 | await goDeeper(address, 'r'); 105 | return signingPaths; 106 | } 107 | -------------------------------------------------------------------------------- /src/core/unit.ts: -------------------------------------------------------------------------------- 1 | import Author from './author'; 2 | import {Message} from './message'; 3 | import * as objectHash from '../common/object_hash'; 4 | import * as objectLength from '../common/object_length'; 5 | import * as conf from '../common/conf'; 6 | import {isGenesisUnit} from './genesis'; 7 | import {isNonemptyArray, isStringOfLength} from '../common/validation_utils'; 8 | 9 | interface UnitValid { 10 | kind: 'ok'; 11 | } 12 | 13 | interface UnitError { 14 | kind: 'unit_error'; 15 | msg: string; 16 | } 17 | 18 | class Result { 19 | static ok(): UnitValid { 20 | return {kind: 'ok'}; 21 | } 22 | 23 | static unitError(msg: string): UnitError { 24 | return { 25 | kind: 'unit_error', 26 | msg, 27 | }; 28 | } 29 | } 30 | 31 | export type ValidateResult = UnitValid | UnitError; 32 | 33 | export default class Unit { 34 | readonly version = conf.version; 35 | readonly alt = conf.alt; 36 | unit: Base64; 37 | headersCommission: number; 38 | payloadCommission: number; 39 | mainChainIndex?: number; 40 | timestamp?: number; 41 | isStable?: boolean; 42 | ball: Base64; 43 | contentHash: Base64; 44 | 45 | constructor( 46 | readonly parentUnits: Base64[], 47 | readonly lastBall: Base64, 48 | readonly lastBallUnit: Base64, 49 | readonly witnessListUnit: Base64, 50 | readonly authors: Author[], 51 | readonly witnesses: Address[], 52 | readonly messages: Message[], 53 | ) { 54 | this.headersCommission = this.calcHeadersCommission(); 55 | this.payloadCommission = this.calcPayloadCommission(); 56 | } 57 | 58 | calcHeadersCommission(): number { 59 | return objectLength.getHeadersSize(this); 60 | } 61 | 62 | calcPayloadCommission(): number { 63 | return objectLength.getTotalPayloadSize(this); 64 | } 65 | 66 | calcUnit(): Base64 { 67 | return objectHash.getUnitHash(this); 68 | } 69 | 70 | calcBall(): Base64 { 71 | return objectHash.getBallHash(this.unit, null, null, null); 72 | } 73 | 74 | static validateUnit(unit: Unit): ValidateResult { 75 | if (!isStringOfLength(unit.unit, conf.HASH_LENGTH)) 76 | return Result.unitError(`wrong unit length: ${unit.unit}`); 77 | 78 | if (objectHash.getUnitHash(unit) !== unit.unit) 79 | return Result.unitError('wrong unit hash'); 80 | 81 | if (isGenesisUnit(unit)) { 82 | return Result.ok(); 83 | } 84 | 85 | if (!isNonemptyArray(unit.parentUnits)) 86 | return Result.unitError('missing or empty parent units array'); 87 | if (!isStringOfLength(unit.lastBall, conf.HASH_LENGTH)) 88 | return Result.unitError('wrong length of last ball'); 89 | if (!isStringOfLength(unit.lastBallUnit, conf.HASH_LENGTH)) 90 | return Result.unitError('wrong length of last ball unit'); 91 | 92 | return Result.ok(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/core/validation.ts: -------------------------------------------------------------------------------- 1 | import Unit from './unit'; 2 | import { 3 | hasFieldsExcept, isNonnegativeInteger, isPositiveInteger, isStringOfLength, 4 | isValidAddressAnyCase, 5 | } from '../common/validation_utils'; 6 | import {Message, Payload} from './message'; 7 | import * as conf from '../common/conf'; 8 | import sqlstore from '../storage/sqlstore'; 9 | 10 | 11 | async function validateParents(unit: Unit, lbMCI: number) { 12 | 13 | // avoid merging the obvious nonserials 14 | async function checkNoSameAddressInDifferentParents() { 15 | if (unit.parentUnits.length === 1) 16 | return checkLastBallDidNotRetreat(); 17 | const rows = await sqlstore.all(` 18 | SELECT address, COUNT(*) AS c 19 | FROM unit_authors WHERE unit IN(?) 20 | GROUP BY address HAVING c>1`, 21 | unit.parentUnits, 22 | ); 23 | if (rows.length > 0) 24 | throw Error('some addresses found more than once in parents, e.g. ' + rows[0].address); 25 | return checkLastBallDidNotRetreat(); 26 | } 27 | 28 | async function checkLastBallDidNotRetreat() { 29 | const row = await sqlstore.get(` 30 | SELECT MAX(lb_units.main_chain_index) AS max_parent_last_ball_mci 31 | FROM units JOIN units AS lb_units ON units.last_ball_unit=lb_units.unit 32 | WHERE units.unit IN(?)`, 33 | unit.parentUnits, 34 | ); 35 | 36 | const max_parent_last_ball_mci = row.max_parent_last_ball_mci; 37 | if (max_parent_last_ball_mci > lbMCI) { 38 | throw Error(`last ball mci must not retreat, parents: ${unit.parentUnits.join(', ')}`); 39 | } 40 | } 41 | 42 | if (unit.parentUnits.length > conf.MAX_PARENTS_PER_UNIT) // anti-spam 43 | throw Error('too many parents: ' + unit.parentUnits.length); 44 | // after this point, we can trust parent list as it either agrees with parents_hash or agrees with hash tree 45 | // hence, there are no more joint errors, except unordered parents or skiplist units 46 | const lastBall = unit.lastBall; 47 | const lastBallUnit = unit.lastBallUnit; 48 | let prev = ''; 49 | const missingParentUnits = []; 50 | const prevParentUnitProps = []; 51 | // objValidationState.max_parent_limci = 0; 52 | const join = unit.ball ? 'LEFT JOIN balls USING(unit) LEFT JOIN hash_tree_balls ON units.unit=hash_tree_balls.unit' : ''; 53 | const field = unit.ball ? ', IFNULL(balls.ball, hash_tree_balls.ball) AS ball' : ''; 54 | 55 | for (const parent of unit.parentUnits) { 56 | if (parent <= prev) { 57 | throw Error('parent units not ordered'); 58 | } 59 | prev = parent; 60 | 61 | const row = await sqlstore.get(`SELECT units.* ${field} FROM units ${join} WHERE units.unit=?`, parent); 62 | if (row === undefined) { 63 | missingParentUnits.push(parent); 64 | } 65 | if (unit.ball && row.ball === null) { 66 | throw Error('no ball corresponding to parent unit'); 67 | } 68 | } 69 | 70 | if (missingParentUnits.length > 0) { 71 | const rows = await sqlstore.all('SELECT error FROM known_bad_joints WHERE unit IN(?)', missingParentUnits); 72 | if (rows.length > 0) { 73 | throw Error('some of the unit parents are known bad'); 74 | } else { 75 | throw Error(`unresolved parent units: ${missingParentUnits.join(',')}`); 76 | } 77 | } 78 | 79 | const rows = await sqlstore.all(` 80 | SELECT is_stable, is_on_main_chain, main_chain_index, ball, 81 | (SELECT MAX(main_chain_index) FROM units) AS max_known_mci 82 | FROM units LEFT JOIN balls USING(unit) WHERE unit=?`, 83 | lastBallUnit, 84 | ); 85 | if (rows.length !== 1) { 86 | throw Error(`last ball unit ${lastBallUnit} not found`); 87 | } 88 | 89 | const objLastBallUnitProps = rows[0]; 90 | 91 | // it can be unstable and have a received (not self-derived) ball 92 | //if (objLastBallUnitProps.ball !== null && objLastBallUnitProps.is_stable === 0) 93 | // throw "last ball "+last_ball+" is unstable"; 94 | if (objLastBallUnitProps.ball === null && objLastBallUnitProps.is_stable === 1) 95 | throw Error(`last ball unit ${lastBallUnit} is stable but has no ball`); 96 | if (objLastBallUnitProps.is_on_main_chain !== 1) 97 | throw Error(`last ball ${lastBall} is not on MC`); 98 | if (objLastBallUnitProps.ball && objLastBallUnitProps.ball !== lastBall) 99 | throw Error(`last_ball ${lastBall} and ${lastBallUnit} do not match`); 100 | if (objLastBallUnitProps.is_stable === 1) { 101 | // if it were not stable, we wouldn't have had the ball at all 102 | if (objLastBallUnitProps.ball !== lastBall) 103 | throw Error(`stable: lastBall ${lastBall} and lastBallUnit ${lastBallUnit} do not match`); 104 | } 105 | } 106 | 107 | async function validatePayment(message: Message, unit: Unit) { 108 | if (hasFieldsExcept(message.payload, ['inputs', 'outputs'])) { 109 | throw Error('unknown fields in payment message'); 110 | } 111 | return validatePaymentInputsAndOutputs(message.payload, 0, unit); 112 | } 113 | 114 | async function validatePaymentInputsAndOutputs(payload: Payload, messageIndex: number, unit: Unit) { 115 | if (payload.inputs.length > conf.MAX_INPUTS_PER_PAYMENT_MESSAGE) 116 | throw Error('too many inputs'); 117 | if (payload.outputs.length > conf.MAX_OUTPUTS_PER_PAYMENT_MESSAGE) 118 | throw Error('too many outputs'); 119 | 120 | const denomination = 1; 121 | 122 | const authorAddresses = unit.authors.map(author => author.address); 123 | let inputAddresses = []; // used for non-transferrable assets only 124 | const outputAddresses = []; 125 | let totalInput = 0; 126 | let totalOutput = 0; 127 | let prevAddress = ''; // if public, outputs must be sorted by address 128 | let prevAmount = 0; 129 | for (let i = 0; i < payload.outputs.length; i++) { 130 | const output = payload.outputs[i]; 131 | if (hasFieldsExcept(output, ['address', 'amount', 'blinding', 'output_hash'])) 132 | throw Error('unknown fields in payment output'); 133 | if (!isPositiveInteger(output.amount)) 134 | throw Error('amount must be positive integer, found ' + output.amount); 135 | if (!isValidAddressAnyCase(output.address)) 136 | throw Error('output address ' + output.address + ' invalid'); 137 | 138 | if (prevAddress > output.address) 139 | throw Error('output addresses not sorted'); 140 | else if (prevAddress === output.address && prevAmount > output.amount) 141 | throw Error('output amounts for same address not sorted'); 142 | prevAddress = output.address; 143 | prevAmount = output.amount; 144 | if (output.address && outputAddresses.indexOf(output.address) === -1) 145 | outputAddresses.push(output.address); 146 | totalOutput += output.amount; 147 | } 148 | 149 | let isIssue = false; 150 | 151 | for (let inputIndex = 0; inputIndex < payload.inputs.length; inputIndex++) { 152 | const input = payload.inputs[inputIndex]; 153 | const type = input.type || 'transfer'; 154 | 155 | let doubleSpendWhere; 156 | let doubleSpendVars = []; 157 | 158 | switch (type) { 159 | // case 'issue': 160 | // if (inputIndex !== 0) 161 | // throw Error('issue must come first'); 162 | // if (hasFieldsExcept(input, ['type', 'address', 'amount', 'serial_number'])) 163 | // throw Error('unknown fields in issue input'); 164 | // if (!isPositiveInteger(input.amount)) 165 | // throw Error('amount must be positive'); 166 | // if (!isPositiveInteger(input.serial_number)) 167 | // throw Error('serial_number must be positive'); 168 | // if (input.serial_number !== 1) 169 | // throw Error('for capped asset serial_number must be 1'); 170 | // if (isIssue) 171 | // throw Error('only one issue per message allowd'); 172 | // isIssue = true; 173 | // 174 | // let address = null; 175 | // if (authorAddresses.length === 1) { 176 | // if ('address' in input) 177 | // throw Error('when single-authored, must not put address in issue input'); 178 | // address = authorAddresses[0]; 179 | // } else { 180 | // if (typeof input.address !== 'string') 181 | // throw Error('when multi-authored, must put address in issue input'); 182 | // if (authorAddresses.indexOf(input.address) === -1) 183 | // throw Error('issue input address ' + input.address + ' is not an author'); 184 | // address = input.address; 185 | // } 186 | // 187 | // inputAddresses = [address]; 188 | // if (!genesis.isGenesisUnit(unit.unit)) 189 | // throw Error('only genesis can issue base asset'); 190 | // if (input.amount !== conf.TOTAL) 191 | // throw Error('issue must be equal to cap'); 192 | // totalInput += input.amount; 193 | // 194 | // break; 195 | 196 | case 'transfer': 197 | if (hasFieldsExcept(input, ['type', 'unit', 'message_index', 'output_index'])) 198 | throw Error('unknown fields in payment input'); 199 | if (!isStringOfLength(input.unit, conf.HASH_LENGTH)) 200 | throw Error('wrong unit length in payment input'); 201 | if (!isNonnegativeInteger(input.messageIndex)) 202 | throw Error('no message_index in payment input'); 203 | if (!isNonnegativeInteger(input.outputIndex)) 204 | throw Error('no output_index in payment input'); 205 | 206 | doubleSpendWhere = 'type=? AND src_unit=? AND src_message_index=? AND src_output_index=?'; 207 | doubleSpendVars = [type, input.unit, input.messageIndex, input.outputIndex]; 208 | 209 | const rows = await sqlstore.all(` 210 | SELECT amount, is_stable, sequence, address, main_chain_index, denomination, asset 211 | FROM outputs 212 | JOIN units USING(unit) 213 | WHERE outputs.unit=? AND message_index=? AND output_index=?`, 214 | input.unit, input.messageIndex, input.outputIndex, 215 | ); 216 | if (rows.length > 1) 217 | throw Error('more than 1 src output'); 218 | if (rows.length === 0) 219 | throw Error('input unit ' + input.unit + ' not found'); 220 | const srcOutput = rows[0]; 221 | if (typeof srcOutput.amount !== 'number') 222 | throw Error('src output amount is not a number'); 223 | if (srcOutput.sequence !== 'good') // it is also stable or private 224 | throw Error('input unit ' + input.unit + ' is not serial'); 225 | const onwerAddress = srcOutput.address; 226 | if (authorAddresses.indexOf(onwerAddress) === -1) 227 | throw Error('output owner is not among authors'); 228 | if (denomination !== srcOutput.denomination) 229 | throw Error('denomination mismatch'); 230 | if (inputAddresses.indexOf(onwerAddress) === -1) 231 | inputAddresses.push(onwerAddress); 232 | totalInput += srcOutput.amount; 233 | } 234 | 235 | } 236 | 237 | if (totalInput !== totalOutput + unit.headersCommission + unit.payloadCommission) { 238 | throw Error('inputs and outputs do not balance'); 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /src/events/eventbus.test.ts: -------------------------------------------------------------------------------- 1 | import {TypedEventEmitter} from './eventbus'; 2 | 3 | class Foo { 4 | constructor(readonly str: string) {} 5 | } 6 | 7 | class Bar { 8 | constructor(readonly num: number) {} 9 | } 10 | 11 | type Routes = { 12 | foo: Foo; 13 | bar: Bar; 14 | }; 15 | 16 | test('test event bus', () => { 17 | const bus = new TypedEventEmitter(); 18 | bus.on('foo', x => console.log(`get foo ${x.str}`)); 19 | bus.once('bar', x => console.log(`get bar ${x.num}`)); 20 | bus.emit('foo', new Foo('hello')); 21 | bus.emit('bar', new Bar(1)); 22 | bus.emit('bar', new Bar(2)); 23 | }); 24 | -------------------------------------------------------------------------------- /src/events/eventbus.ts: -------------------------------------------------------------------------------- 1 | export interface Listener { 2 | (arg: T): any; 3 | } 4 | 5 | interface ITypedEventEmitter { 6 | addListener(event: K, listener: Listener): this; 7 | 8 | on(event: K, listener: Listener): this; 9 | 10 | once(event: K, listener: Listener): this; 11 | 12 | removeListener(event: K, listener: Listener): this; 13 | 14 | removeAllListeners(event?: K): this; 15 | 16 | setMaxListeners(n: number): this; 17 | 18 | getMaxListeners(): number; 19 | 20 | listeners(event: K): Listener[]; 21 | 22 | emit(event: K, arg: T[K]): boolean; 23 | 24 | listenerCount(type: K): number; 25 | 26 | eventNames(): (string | symbol)[]; 27 | } 28 | 29 | export class TypedEventEmitter implements ITypedEventEmitter { 30 | private _listeners: Map = new Map(); 31 | private _onceListeners: Map = new Map(); 32 | private _maxListeners: number; 33 | 34 | 35 | addListener(event: K, listener: Listener): this { 36 | if (!this._listeners.has(event)) { 37 | this._listeners.set(event, []); 38 | } 39 | this._listeners.get(event).push(listener); 40 | return this; 41 | } 42 | 43 | 44 | on(event: K, listener: Listener): this { 45 | return this.addListener(event, listener); 46 | } 47 | 48 | 49 | once(event: K, listener: Listener): this { 50 | if (!this._onceListeners.has(event)) { 51 | this._onceListeners.set(event, []); 52 | } 53 | this._onceListeners.get(event).push(listener); 54 | return this; 55 | } 56 | 57 | removeListener(event: K, listener: Listener): this { 58 | if (this._listeners.has(event)) { 59 | const listeners = this._listeners.get(event); 60 | const callbackIndex = listeners.indexOf(listener); 61 | if (callbackIndex > -1) listeners.splice(callbackIndex, 1); 62 | } 63 | return this; 64 | } 65 | 66 | removeAllListeners(event?: K): this { 67 | if (event === undefined) { 68 | this._listeners.clear(); 69 | this._onceListeners.clear(); 70 | } else { 71 | this._listeners.set(event, []); 72 | this._onceListeners.set(event, []); 73 | } 74 | return this; 75 | } 76 | 77 | setMaxListeners(n: number): this { 78 | this._maxListeners = n; 79 | return this; 80 | } 81 | 82 | getMaxListeners(): number { 83 | return this._maxListeners; 84 | } 85 | 86 | listeners(event: K): Listener[] { 87 | return this._listeners.get(event); 88 | } 89 | 90 | emit(event: K, arg: T[K]): boolean { 91 | const listeners = this._listeners.get(event); 92 | if (listeners !== undefined) { 93 | listeners.forEach((listener) => listener(arg)); 94 | } 95 | 96 | const onceListeners = this._onceListeners.get(event); 97 | if (onceListeners !== undefined) { 98 | onceListeners.forEach((listener) => listener(arg)); 99 | this._onceListeners.set(event, []); 100 | } 101 | 102 | return true; 103 | } 104 | 105 | listenerCount(type: K): number { 106 | return this._listeners.get(type).length; 107 | } 108 | 109 | eventNames(): (string | symbol)[] { 110 | return [...this._listeners.keys()]; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/events/events.ts: -------------------------------------------------------------------------------- 1 | export enum PEER_EVENTS { 2 | NEW_UNIT, 3 | } 4 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import * as Path from 'path'; 4 | import {CLI, Shim} from 'clime'; 5 | 6 | const cli = new CLI('itc', Path.join(__dirname, 'commands')); 7 | 8 | // Clime in its core provides an object-based command-line infrastructure. 9 | // To have it work as a common CLI, a shim needs to be applied: 10 | const shim = new Shim(cli); 11 | shim.execute(process.argv); 12 | -------------------------------------------------------------------------------- /src/models/Authors.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import Author from '../core/author'; 3 | import Unit from '../core/unit'; 4 | import * as objectHash from '../common/object_hash'; 5 | import * as genesis from '../core/genesis'; 6 | import {Authentifiers} from '../core/authentifiers'; 7 | 8 | export class Authors { 9 | static async read(unit: Base64): Promise { 10 | const rows = await sqlstore.all( 11 | 'SELECT address, definition_chash FROM unit_authors WHERE unit=? ORDER BY address', 12 | unit, 13 | ); 14 | 15 | return Promise.all(rows.map(async (row) => { 16 | const rows = await sqlstore.all( 17 | 'SELECT path, authentifier FROM authentifiers WHERE unit=? AND address=?', 18 | unit, row.address, 19 | ); 20 | 21 | // const authentifiers = auths.map(auth => { 22 | // return new Authentifier(auth.path, auth.authentifier); 23 | // }); 24 | 25 | const authentifiers: Authentifiers = {}; 26 | for (const row of rows) { 27 | authentifiers[row.path] = row.authentifier; 28 | } 29 | 30 | return new Author(row.address, authentifiers); 31 | })); 32 | } 33 | 34 | static async save(unit: Unit): Promise { 35 | const isGenesis = genesis.isGenesisUnit(unit); 36 | const authorAddresses = []; 37 | for (let i = 0; i < unit.authors.length; i++) { 38 | const author = unit.authors[i]; 39 | authorAddresses.push(author.address); 40 | const definition = author.definition; 41 | let definitionChash = null; 42 | if (definition) { 43 | definitionChash = objectHash.getChash160(definition); 44 | await sqlstore.run( 45 | `INSERT INTO definitions (definition_chash, definition, has_references) VALUES (?,?,?)`, 46 | definitionChash, JSON.stringify(definition), 0, 47 | ); 48 | // actually inserts only when the address is first used. 49 | // if we change keys and later send a unit signed by new keys, the address is not inserted. 50 | // Its definition_chash was updated before when we posted change-definition message. 51 | if (definitionChash === author.address) { 52 | await sqlstore.run(`INSERT INTO addresses (address) VALUES(?)`, author.address); 53 | } 54 | } else if (unit.contentHash) { 55 | await sqlstore.run(`INSERT INTO addresses (address) VALUES(?)`, author.address); 56 | } 57 | 58 | await sqlstore.run(`INSERT INTO unit_authors (unit, address, definition_chash) VALUES(?,?,?)`, 59 | unit.unit, author.address, definitionChash); 60 | if (isGenesis) 61 | await sqlstore.run('UPDATE unit_authors SET _mci=0 WHERE unit=?', unit.unit); 62 | if (!unit.contentHash) { 63 | for (const path in author.authentifiers) { 64 | await sqlstore.run(` 65 | INSERT INTO authentifiers (unit, address, path, authentifier) VALUES(?,?,?,?)`, 66 | unit.unit, author.address, path, author.authentifiers[path], 67 | ); 68 | } 69 | } 70 | } 71 | } 72 | } 73 | 74 | 75 | -------------------------------------------------------------------------------- /src/models/Balls.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import Unit from '../core/unit'; 3 | 4 | export default class Balls { 5 | static async read(unit: Base64): Promise { 6 | const row = await sqlstore.get('SELECT ball FROM balls WHERE unit=?', [unit]); 7 | return row.ball; 8 | } 9 | 10 | static async save(unit: Unit) { 11 | if (unit.ball) { 12 | await sqlstore.run(`INSERT INTO balls (ball, unit) VALUES (?,?)`, [unit.ball, unit.unit]); 13 | await sqlstore.run(`DELETE FROM hash_tree_balls WHERE ball=? AND unit=?`, [unit.ball, unit.unit]); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/models/MainChain.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | 3 | export const minRetrievableMCI = null; 4 | export default class MainChain { 5 | static minRetrievableMCI() { 6 | return minRetrievableMCI; 7 | } 8 | 9 | static async readLastMCI(): Promise { 10 | const rows = await sqlstore.all(`SELECT MAX(main_chain_index) AS mci FROM units`); 11 | let lastMCI = rows[0].mci; 12 | if (lastMCI === null) // empty database 13 | lastMCI = 0; 14 | return lastMCI; 15 | } 16 | 17 | static async findLastBallMciOfMci(mci: number): Promise { 18 | if (mci === 0) 19 | throw Error('findLastBallMciOfMci called with mci=0'); 20 | const rows = await sqlstore.all(` 21 | SELECT lb_units.main_chain_index, lb_units.is_on_main_chain 22 | FROM units JOIN units AS lb_units ON units.last_ball_unit=lb_units.unit 23 | WHERE units.is_on_main_chain=1 AND units.main_chain_index=?`, 24 | mci, 25 | ); 26 | 27 | if (rows.length !== 1) 28 | throw Error('last ball\'s mci count ' + rows.length + ' !== 1, mci = ' + mci); 29 | if (rows[0].is_on_main_chain !== 1) 30 | throw Error('lb is not on mc?'); 31 | return rows[0].main_chain_index; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/models/Messages.ts: -------------------------------------------------------------------------------- 1 | import Unit from '../core/unit'; 2 | import sqlstore from '../storage/sqlstore'; 3 | import {Output, TransferInput} from '../core/message'; 4 | import logger from '../common/log'; 5 | 6 | export class Inputs { 7 | static newTransferInput(unit: Base64, messageIndex: number, outputIndex: number): TransferInput { 8 | return { 9 | 'type': 'transfer', 10 | 'unit': unit, 11 | 'messageIndex': messageIndex, 12 | 'outputIndex': outputIndex, 13 | }; 14 | } 15 | 16 | static async read(unit: Base64, messageIndex: number): Promise { 17 | const rows = await sqlstore.all(` 18 | SELECT type, denomination, assets.fixed_denominations, 19 | src_unit AS unit, src_message_index AS message_index, src_output_index AS output_index, 20 | from_main_chain_index, to_main_chain_index, serial_number, amount, address, asset 21 | FROM inputs 22 | LEFT JOIN assets ON asset=assets.unit 23 | WHERE inputs.unit=? AND inputs.message_index=? 24 | ORDER BY input_index`, 25 | unit, messageIndex, 26 | ); 27 | 28 | const inputs = []; 29 | for (let i = 0; i < rows.length; i++) { 30 | const input = rows[i]; 31 | inputs.push(Inputs.newTransferInput(input.unit, input.message_index, input.output_index)); 32 | } 33 | return inputs; 34 | } 35 | 36 | static async save(unit: Unit) { 37 | for (let i = 0; i < unit.messages.length; i++) { 38 | const message = unit.messages[i]; 39 | for (let j = 0; j < message.payload.inputs.length; j++) { 40 | const input = message.payload.inputs[j]; 41 | let type; 42 | let srcUnit = null; 43 | let srcMessageIndex = null; 44 | let srcOutputIndex = null; 45 | let fromMCI = null; 46 | let toMCI = null; 47 | let amount = null; 48 | let serialNumber = null; 49 | const address = unit.authors[0].address; 50 | switch (input.type) { 51 | case 'issue': 52 | type = 'issue'; 53 | amount = input.amount; 54 | serialNumber = input.serialNumber; 55 | break; 56 | case 'headers_commission': 57 | type = 'headers_commission'; 58 | fromMCI = input.fromMCI; 59 | toMCI = input.toMCI; 60 | break; 61 | case 'witness_commission': 62 | type = 'witness_commission'; 63 | fromMCI = input.fromMCI; 64 | toMCI = input.toMCI; 65 | break; 66 | case 'transfer': 67 | type = 'transfer'; 68 | srcUnit = input.unit; 69 | srcMessageIndex = input.messageIndex; 70 | srcOutputIndex = input.outputIndex; 71 | } 72 | const denomination = 1; 73 | const isUnique = 1; 74 | const asset = null; 75 | 76 | 77 | await sqlstore.run(` 78 | INSERT INTO inputs 79 | (unit, message_index, input_index, type, 80 | src_unit, src_message_index, src_output_index, 81 | from_main_chain_index, to_main_chain_index, 82 | denomination, amount, serial_number, 83 | asset, is_unique, address) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, 84 | unit.unit, i, j, type, 85 | srcUnit, srcMessageIndex, srcOutputIndex, 86 | fromMCI, toMCI, 87 | denomination, amount, serialNumber, 88 | asset, isUnique, address, 89 | ); 90 | } 91 | } 92 | } 93 | } 94 | 95 | export class Outputs { 96 | static async all() { 97 | return sqlstore.all('SELECT * FROM outputs'); 98 | } 99 | 100 | static async read(unit: Base64, messageIndex: number): Promise { 101 | const rows = await sqlstore.all( // we don't select blinding because it's absent on public payments 102 | `SELECT address, amount, asset, denomination 103 | FROM outputs WHERE unit=? AND message_index=? ORDER BY output_index`, 104 | [unit, messageIndex], 105 | ); 106 | 107 | const outputs = []; 108 | for (const output of rows) { 109 | outputs.push(new Output(output.address, output.amount)); 110 | } 111 | return outputs; 112 | } 113 | 114 | static async save(unit: Unit) { 115 | for (let i = 0; i < unit.messages.length; i++) { 116 | const message = unit.messages[i]; 117 | for (let j = 0; j < message.payload.outputs.length; j++) { 118 | const output = message.payload.outputs[j]; 119 | await sqlstore.run(` 120 | INSERT INTO outputs 121 | (unit, message_index, output_index, address, amount, asset, denomination, is_serial) 122 | VALUES(?,?,?,?,?,?,?,1)`, 123 | unit.unit, i, j, output.address, output.amount, null, 1, 124 | ); 125 | } 126 | } 127 | } 128 | } 129 | 130 | export default class Messages { 131 | static async read(unit: Base64) { 132 | const rows = await sqlstore.all(` 133 | SELECT app, payload_hash, payload_location, payload, payload_uri, payload_uri_hash, message_index 134 | FROM messages WHERE unit=? ORDER BY message_index`, 135 | unit, 136 | ); 137 | 138 | const messages = []; 139 | for (const row of rows) { 140 | const inputs = await Inputs.read(row.unit, row.messageIndex); 141 | const outputs = await Outputs.read(row.unit, row.messageIndex); 142 | const message = row; 143 | message.payload = { 144 | inputs: inputs, 145 | outputs: outputs, 146 | }; 147 | messages.push(message); 148 | } 149 | 150 | return messages; 151 | } 152 | 153 | static async save(unit: Unit) { 154 | for (let i = 0; i < unit.messages.length; i++) { 155 | const message = unit.messages[i]; 156 | const payload = JSON.stringify(message.payload); 157 | 158 | await sqlstore.run(` 159 | INSERT INTO messages 160 | (unit, message_index, app, payload_hash, payload_location, payload, payload_uri, payload_uri_hash) 161 | VALUES(?,?,?,?,?,?,?,?)`, 162 | unit.unit, i, message.app, message.payloadHash, message.payloadLocation, payload, null, null, 163 | ); 164 | } 165 | 166 | // save inputs 167 | await Inputs.save(unit); 168 | // save outputs 169 | await Outputs.save(unit); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/models/MyAddresses.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import XPubKeys from './XPubKeys'; 3 | import * as objectHash from '../common/object_hash'; 4 | import HDKeys from '../common/HDKeys'; 5 | import Definition from '../core/definition'; 6 | 7 | export type MyAddress = { 8 | address: Address, 9 | wallet: Base64, 10 | is_change: number, 11 | address_index: number, 12 | definition: string, 13 | }; 14 | 15 | export default class MyAddresses { 16 | static async read(): Promise { 17 | return sqlstore.all('select * from my_addresses'); 18 | } 19 | 20 | static async deriveAddress(wallet: Base64, isChange: number, addressIndex: number) { 21 | const row = await sqlstore.get(` 22 | SELECT definition_template, full_approval_date FROM wallets WHERE wallet=?`, 23 | wallet, 24 | ); 25 | 26 | if (!row) { 27 | throw Error('wallet not found: ' + wallet + ', is_change=' + isChange + ', index=' + addressIndex); 28 | } 29 | 30 | let definitions = JSON.parse(row.definition_template); 31 | const pk = await XPubKeys.findByWallet(wallet); 32 | const params: any = {}; 33 | params[`pubkey@${pk.device_address}`] = HDKeys.derivePubKey(pk.extended_pubkey, isChange, addressIndex); 34 | const address = objectHash.getChash160(definitions); 35 | definitions = Definition.replaceTemplate(definitions, params); 36 | return [address, definitions]; 37 | } 38 | 39 | static async readNextAddressIndex(wallet: Base64, isChange: number) { 40 | const row = await sqlstore.get(` 41 | SELECT MAX(address_index) AS last_used_index 42 | FROM my_addresses WHERE wallet=? AND is_change=?`, 43 | wallet, isChange, 44 | ); 45 | const index = row.last_used_index; 46 | if (row === null) { 47 | return 0; 48 | } else { 49 | return index + 1; 50 | } 51 | } 52 | 53 | static async readLastUsedAddressIndex(wallet: Base64, isChange: number) { 54 | const row = await sqlstore.get(` 55 | SELECT MAX(address_index) AS last_used_index 56 | FROM my_addresses JOIN outputs USING(address) WHERE wallet=? AND is_change=?`, 57 | wallet, isChange, 58 | ); 59 | 60 | return row.last_used_index; 61 | } 62 | 63 | static async issueOrSelectNextAddress(wallet: Base64, isChange: number = 0): Promise
{ 64 | const addressIndex = await MyAddresses.readNextAddressIndex(wallet, isChange); 65 | return this.issueAddress(wallet, isChange, addressIndex); 66 | } 67 | 68 | static async issueOrSelectNextChangeAddress(wallet: Base64) { 69 | return this.issueOrSelectNextAddress(wallet, 1); 70 | } 71 | 72 | static async issueAddress(wallet: Base64, isChange: number, addressIndex: number): Promise
{ 73 | const [address, definitions] = await this.deriveAddress(wallet, isChange, addressIndex); 74 | await this.save(address, wallet, isChange, addressIndex, definitions); 75 | return address; 76 | } 77 | 78 | static async save(address: Address, wallet: Base64, isChange: number, addressIndex: number, definition: any[]) { 79 | await sqlstore.run(` 80 | INSERT INTO my_addresses (address, wallet, is_change, address_index, definition) VALUES (?,?,?,?,?)`, 81 | address, wallet, isChange, addressIndex, JSON.stringify(definition), 82 | ); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/models/MyWitnesses.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import * as conf from '../common/conf'; 3 | import {isValidAddress} from '../common/validation_utils'; 4 | 5 | export class MyWitnesses { 6 | static async readWitnesses(): Promise { 7 | const rows = await sqlstore.all(`SELECT address FROM my_witnesses ORDER BY address`); 8 | const witnesses = rows.map((row) => row.address); 9 | 10 | if (witnesses.length !== conf.COUNT_WITNESSES) 11 | throw Error(`wrong number of my witnesses: ${witnesses.length}`); 12 | return witnesses; 13 | } 14 | 15 | static async replaceWitnesses(oldWitness: Address, newWitness: Address): Promise { 16 | if (!isValidAddress(newWitness)) 17 | throw new Error('new witness address is invalid'); 18 | 19 | const witnesses = await this.readWitnesses(); 20 | if (witnesses.indexOf(oldWitness) === -1) { 21 | throw new Error('old witness not known'); 22 | } 23 | if (witnesses.indexOf(newWitness) >= 0) { 24 | throw new Error('new witness already present'); 25 | } 26 | return await sqlstore.run('UPDATE my_witnesses SET address=? WHERE address=?', [newWitness, oldWitness]); 27 | } 28 | 29 | static async insertWitnesses(witnesses: Address[]): Promise { 30 | if (witnesses.length !== conf.COUNT_WITNESSES) { 31 | throw new Error('attempting to insert wrong number of witnesses: ' + witnesses.length); 32 | } 33 | const placeholders = Array.apply(null, Array(witnesses.length)).map(() => '(?)').join(','); 34 | return await sqlstore.run('INSERT INTO my_witnesses (address) VALUES ' + placeholders, witnesses); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/models/Parents.ts: -------------------------------------------------------------------------------- 1 | import Unit from '../core/unit'; 2 | import sqlstore from '../storage/sqlstore'; 3 | 4 | export default class Parents { 5 | static async read(unit: Base64): Promise { 6 | const rows = await sqlstore.all(` 7 | SELECT parent_unit 8 | FROM parenthoods 9 | WHERE child_unit=? 10 | ORDER BY parent_unit`, 11 | [unit], 12 | ); 13 | 14 | return rows.map(row => row.parent_unit); 15 | } 16 | 17 | static async save(unit: Unit): Promise { 18 | if (unit.parentUnits) { 19 | for (const parent of unit.parentUnits) { 20 | await sqlstore.run(`INSERT INTO parenthoods (child_unit, parent_unit) VALUES (?,?)`, [unit.unit, parent]); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/models/Units.ts: -------------------------------------------------------------------------------- 1 | import Unit from '../core/unit'; 2 | import sqlstore from '../storage/sqlstore'; 3 | import Parents from './Parents'; 4 | import Witnesses from './Witnesses'; 5 | import {Authors} from './Authors'; 6 | import Messages from './Messages'; 7 | import {isGenesisUnit} from '../core/genesis'; 8 | 9 | type StaticUnitProps = { 10 | level: number, 11 | witnessed_level: number, 12 | best_parent_unit: Base64, 13 | witness_list_unit: Base64, 14 | }; 15 | 16 | const cachedUnits = new Map(); 17 | const stableUnits = new Map(); 18 | 19 | 20 | type UnitStatus = 'unknown' | 'known'; 21 | 22 | export default class Units { 23 | static async readStaticUnitProps(unit: Base64): Promise { 24 | const props = cachedUnits.get(unit); 25 | if (props) 26 | return props; 27 | 28 | const row: StaticUnitProps = await sqlstore.get(` 29 | SELECT level, witnessed_level, best_parent_unit, witness_list_unit FROM units where unit=?`, unit); 30 | cachedUnits.set(unit, row); 31 | return row; 32 | } 33 | 34 | static async readUnitProps(unit: Base64): Promise { 35 | if (stableUnits.has(unit)) 36 | return stableUnits.get(unit); 37 | 38 | const rows = await sqlstore.all(` 39 | SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free, is_stable, witnessed_level 40 | FROM units WHERE unit=?`, 41 | unit, 42 | ); 43 | const props = rows[0]; 44 | if (props.is_stable) { 45 | stableUnits.set(unit, props); 46 | } 47 | 48 | return props; 49 | } 50 | 51 | static async readPropsOfUnits(earlierUnit: string, laterUnits: any[]) { 52 | const rows = await sqlstore.all(` 53 | SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free FROM units WHERE unit IN(?, ?)`, 54 | earlierUnit, laterUnits, 55 | ); 56 | let earlierUnitProps; 57 | const laterUnitProps = []; 58 | for (let i = 0; i < rows.length; i++) { 59 | if (rows[i].unit === earlierUnit) 60 | earlierUnitProps = rows[i]; 61 | else 62 | laterUnitProps.push(rows[i]); 63 | } 64 | return [earlierUnitProps, laterUnitProps]; 65 | } 66 | 67 | static async read(unit: Base64): Promise { 68 | const row = await sqlstore.get(` 69 | SELECT units.unit, version, alt, witness_list_unit, last_ball_unit, balls.ball AS last_ball, is_stable, 70 | content_hash, headers_commission, payload_commission, main_chain_index, 71 | FROM units LEFT JOIN balls ON last_ball_unit=balls.unit WHERE units.unit=?`, 72 | unit, 73 | ); 74 | 75 | if (!row) { 76 | return null; 77 | } 78 | 79 | const parents = await Parents.read(unit); 80 | let witnesses = []; 81 | if (!row.witness_list_unit) { 82 | witnesses = await Witnesses.readWitnessList(unit); 83 | } 84 | const authors = await Authors.read(unit); 85 | const messages = await Messages.read(unit); 86 | return new Unit( 87 | parents, 88 | row.last_ball, 89 | row.last_ball_unit, 90 | row.witness_list_unit, 91 | authors, 92 | witnesses, 93 | messages, 94 | ); 95 | } 96 | 97 | static async save(unit: Unit, sequence: string) { 98 | const fields = [ 99 | 'unit', 'version', 'alt', 'witness_list_unit', 'last_ball_unit', 'headers_commission', 100 | 'payload_commission', 'sequence', 'content_hash', 101 | ]; 102 | const values = '?,?,?,?,?,?,?,?,?'; 103 | const params = [unit.unit, unit.version, unit.alt, unit.witnessListUnit, unit.lastBallUnit, unit.headersCommission, 104 | unit.payloadCommission, sequence, 105 | ]; 106 | 107 | await sqlstore.run(`INSERT INTO units (${fields}) VALUES (${values})`, ...params); 108 | 109 | if (isGenesisUnit(unit)) { 110 | await sqlstore.run(` 111 | UPDATE units SET is_on_main_chain=1, main_chain_index=0, is_stable=1, level=0, witnessed_level=0 112 | WHERE unit=?`, unit.unit); 113 | } else { 114 | await sqlstore.run(`UPDATE units SET is_free=0 WHERE unit IN(?)`, unit.parentUnits); 115 | } 116 | 117 | // save balls 118 | if (unit.ball) { 119 | await sqlstore.run('INSERT INTO balls (ball, unit) VALUES(?,?)', unit.ball, unit.unit); 120 | } 121 | // save parenthoods 122 | if (unit.parentUnits) { 123 | for (const parent of unit.parentUnits) { 124 | await sqlstore.run('INSERT INTO parenthoods (child_unit, parent_unit) VALUES(?,?)', unit.unit, parent); 125 | } 126 | } 127 | await Messages.save(unit); 128 | await Witnesses.save(unit); 129 | await Authors.save(unit); 130 | } 131 | 132 | static async checkUnitStatus(unit: Base64): Promise { 133 | const rows = await sqlstore.all('SELECT 1 FROM units WHERE unit=?', unit); 134 | if (rows.length > 0) 135 | return 'known'; 136 | 137 | return 'unknown'; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/models/Witnesses.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import Unit from '../core/unit'; 3 | import * as objectHash from '../common/object_hash'; 4 | import * as conf from '../common/conf'; 5 | import Base64 = require('crypto-js/enc-base64'); 6 | 7 | const cachedWitnesses = new Map(); 8 | 9 | export default class Witnesses { 10 | static async findWitnessListUnit(witnesses: Address[], lbMCI: number): Promise { 11 | const rows = await sqlstore.all(` 12 | SELECT witness_list_hashes.witness_list_unit 13 | FROM witness_list_hashes CROSS JOIN units ON witness_list_hashes.witness_list_unit=unit 14 | WHERE witness_list_hash=? AND sequence='good' AND is_stable=1 AND main_chain_index<=?`, 15 | objectHash.getObjHashB64(witnesses), lbMCI, 16 | ); 17 | return rows[0].witness_list_unit; 18 | } 19 | 20 | static async readWitnesses(unit: Base64): Promise { 21 | const witnesses = cachedWitnesses.get(unit); 22 | if (witnesses) 23 | return witnesses; 24 | const rows = await sqlstore.all('SELECT witness_list_unit FROM units WHERE unit=?', unit); 25 | if (rows.length === 0) 26 | throw Error('unit ' + unit + ' not found'); 27 | 28 | const witnessListUnit = rows[0].witness_list_unit; 29 | return Witnesses.readWitnessList(witnessListUnit ? witnessListUnit : unit); 30 | } 31 | 32 | static async readWitnessList(unit: Base64): Promise { 33 | let witnesses = cachedWitnesses.get(unit); 34 | if (witnesses) 35 | return witnesses; 36 | const rows = await sqlstore.all(` 37 | SELECT address FROM unit_witnesses WHERE unit=? ORDER BY address`, 38 | unit, 39 | ); 40 | if (rows.length === 0) 41 | throw Error('witness list of unit ' + unit + ' not found'); 42 | if (rows.length > 0 && rows.length !== conf.COUNT_WITNESSES) 43 | throw Error('wrong number of witnesses in unit ' + unit); 44 | witnesses = rows.map(row => row.address); 45 | cachedWitnesses[unit] = witnesses; 46 | return witnesses; 47 | } 48 | 49 | static async save(unit: Unit): Promise { 50 | for (const witness of unit.witnesses) { 51 | await sqlstore.run(`INSERT INTO unit_witnesses (unit, address) VALUES (?,?)`, unit.unit, witness); 52 | } 53 | await sqlstore.run('INSERT OR IGNORE INTO witness_list_hashes (witness_list_unit, witness_list_hash) VALUES (?,?)', 54 | unit.unit, objectHash.getObjHashB64(unit.witnesses)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/models/XPubKeys.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | 3 | export type XPubKey = { 4 | extended_pubkey: PubKey, 5 | device_address: DeviceAddress, 6 | }; 7 | 8 | export default class XPubKeys { 9 | static async all() { 10 | return sqlstore.all(`select * from extended_pubkeys`); 11 | } 12 | 13 | static async findByWallet(wallet: Base64): Promise { 14 | return sqlstore.get(`select extended_pubkey, device_address from extended_pubkeys where wallet=?`, wallet); 15 | } 16 | 17 | static async findByDeviceAddress(devAdd: DeviceAddress) { 18 | return sqlstore.get(`select * from extended_pubkeys where device_address=?`, devAdd); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/models/myaddresses.test.ts: -------------------------------------------------------------------------------- 1 | import MyAddresses from './MyAddresses'; 2 | import Wallets from '../wallets/Wallets'; 3 | 4 | test('address derivation', async () => { 5 | const wallet = await Wallets.readOrCreate(''); 6 | console.log(await MyAddresses.read()); 7 | const address = await MyAddresses.issueOrSelectNextAddress(wallet.wallet); 8 | console.log(address); 9 | console.log(await MyAddresses.read()); 10 | }); 11 | -------------------------------------------------------------------------------- /src/models/xpubkey.test.ts: -------------------------------------------------------------------------------- 1 | import Wallets from '../wallets/Wallets'; 2 | import XPubKeys from './XPubKeys'; 3 | 4 | test('test x pubkey', async() => { 5 | const wallet = await Wallets.readOrCreate(''); 6 | const pk = await XPubKeys.findByWallet(wallet.wallet); 7 | console.log(pk); 8 | }); 9 | -------------------------------------------------------------------------------- /src/network/Peer.ts: -------------------------------------------------------------------------------- 1 | import WebSocketServer from './WebSocketServer'; 2 | import WebSocketClient from './WebSocketClient'; 3 | import * as objectHash from '../common/object_hash'; 4 | import logger from '../common/log'; 5 | import {PEER_CONF} from '../common/conf'; 6 | import Unit from '../core/unit'; 7 | import Units from '../models/Units'; 8 | import Witnesses from '../models/Witnesses'; 9 | import {MyWitnesses} from '../models/MyWitnesses'; 10 | 11 | type Endpoint = string | WebSocketClient; // url or an established connection 12 | 13 | const MY_ADDR = 'MY_ADDR'; 14 | const PING = 'PING'; 15 | const GET_PEERS = 'GET_PEERS'; 16 | const BROADCAST_UNIT = 'BROADCAST_UNIT'; 17 | const GET_WITNESSES = 'GET_WITNESSES'; 18 | 19 | export class Peer { 20 | clients: Map; 21 | server: WebSocketServer; 22 | seeds: string[]; 23 | listenPort: number; 24 | listenAddr: string; 25 | 26 | constructor() { 27 | this.clients = new Map(); 28 | this.server = new WebSocketServer(); 29 | this.server.use(async (ctx) => { 30 | const messages = JSON.parse(ctx.data); 31 | const type = messages[0]; 32 | const content = messages[1]; 33 | 34 | switch (type) { 35 | case 0: 36 | return this.onData(ctx.ws, content.subject, content.body); 37 | case 1: 38 | return this.onRequest(ctx.ws, content.id, content.data.command, content.data.params); 39 | } 40 | }); 41 | this.seeds = PEER_CONF.POOL_SEEDS; 42 | } 43 | 44 | listen(port?: number) { 45 | port = port || PEER_CONF.LISTEN_PORT; 46 | this.listenPort = port; 47 | this.listenAddr = `ws://localhost:${this.listenPort}`; 48 | logger.info({port}, 'start peer server'); 49 | this.server.start({port: port}); 50 | } 51 | 52 | close() { 53 | this.server.close(); 54 | this.clients.forEach(x => x.close()); 55 | } 56 | 57 | async connect(ws?: string | WebSocketClient) { 58 | if (ws) { 59 | return this.sendRequest(ws, 'get_peers', null); 60 | } else { 61 | for (const seed of this.seeds) { 62 | await this.sendRequest(seed, 'get_peers', null); 63 | } 64 | } 65 | } 66 | 67 | async getOrCreateClient(ws: string | WebSocketClient): Promise { 68 | if (typeof ws === 'string') { 69 | const url = ws.toLowerCase(); 70 | if (this.clients.has(url)) { 71 | return this.clients.get(url); 72 | } 73 | const client = new WebSocketClient(url); 74 | await client.open(); 75 | client.onData(data => this.onData(client, data.subject, data.body)); 76 | client.onRequest((id, data) => this.onRequest(client, id, data.command, data.parmas)); 77 | this.clients.set(url, client); 78 | return client; 79 | } else { 80 | return ws; 81 | } 82 | } 83 | 84 | async broadcastData(subject: string, body: any) { 85 | await this.broadcastInboundData(subject, body); 86 | this.clients.forEach(async (client) => { 87 | await client.sendData({subject, body}); 88 | }); 89 | } 90 | 91 | async broadcastInboundData(subject: string, body: any) { 92 | return this.server.broadcast({subject, body}); 93 | } 94 | 95 | async forwardData(ws: WebSocketClient, subject: string, body: any) { 96 | this.clients.forEach(async (client) => { 97 | if (client !== ws) { 98 | await client.sendData({subject, body}); 99 | } 100 | }); 101 | 102 | this.server.clients.forEach(async (client) => { 103 | if (client !== ws) { 104 | await client.sendData({subject, body}); 105 | } 106 | }); 107 | } 108 | 109 | async sendData(ws: string | WebSocketClient, subject: string, body?: any) { 110 | const client = await this.getOrCreateClient(ws); 111 | return client.sendData({subject, body}); 112 | } 113 | 114 | async sendRequest(ws: string | WebSocketClient, command: string, params?: any) { 115 | const client = await this.getOrCreateClient(ws); 116 | const request: any = {command: command}; 117 | if (params) 118 | request.params = params; 119 | const id = objectHash.getObjHashB64(request); 120 | logger.info({request, id}, 'sending request'); 121 | return client.sendRequest(request, id); 122 | } 123 | 124 | async sendResponse(ws: Endpoint, id: string, data: any) { 125 | const client = await this.getOrCreateClient(ws); 126 | return client.sendResponse(id, data); 127 | } 128 | 129 | async getWitnesses(ws: string | WebSocketClient) { 130 | return this.sendRequest(ws, 'get_witnesses', null); 131 | } 132 | 133 | 134 | async onData(ws: WebSocketClient, subject: string, body: any) { 135 | switch (subject) { 136 | case PING: 137 | return this.handlePing(ws); 138 | case MY_ADDR: 139 | return this.handleMyAddr(ws, body); 140 | case BROADCAST_UNIT: 141 | return this.handleBroadcastUnit(ws, body); 142 | default: 143 | return logger.warn(`unknown subject ${subject}`); 144 | } 145 | } 146 | 147 | async onRequest(ws: WebSocketClient, id: string, command: string, params: any) { 148 | switch (command) { 149 | case GET_PEERS: 150 | return this.handleGetPeers(ws, id); 151 | default: 152 | return logger.warn(`unknown command ${command}`); 153 | } 154 | } 155 | 156 | async sendGetPeers(ws: Endpoint) { 157 | logger.info(`${this.listenAddr} sending ${GET_PEERS}`); 158 | const peers = await this.sendRequest(ws, GET_PEERS); 159 | logger.info({peers}, `${this.listenAddr} received ${GET_PEERS}`); 160 | for (const peer of peers) { 161 | if (peer !== this.listenAddr) { 162 | await this.sendPing(peer); 163 | } 164 | } 165 | } 166 | 167 | async handleGetPeers(ws: WebSocketClient, id: string) { 168 | logger.info(`${this.listenAddr} handle ${GET_PEERS}`); 169 | const outboundPeers = [...this.clients.values()].map(x => x.url); 170 | return this.sendResponse(ws, id, outboundPeers); 171 | } 172 | 173 | async broadcastUnit(unit: Unit) { 174 | logger.info(`${this.listenAddr} sending ${BROADCAST_UNIT}`); 175 | return this.broadcastData(BROADCAST_UNIT, unit); 176 | } 177 | 178 | async handleBroadcastUnit(ws: WebSocketClient, unit: Unit) { 179 | logger.info(unit, `${this.listenAddr} handle ${BROADCAST_UNIT}`); 180 | const status = await Units.checkUnitStatus(unit.unit); 181 | switch (status) { 182 | case 'known': 183 | return logger.info(unit.unit, 'known unit, ignore'); 184 | case 'unknown': 185 | const result = Unit.validateUnit(unit); 186 | switch (result.kind) { 187 | case 'ok': 188 | logger.info('OK'); 189 | await Units.save(unit, 'good'); 190 | return this.forwardData(ws, BROADCAST_UNIT, unit); 191 | case 'unit_error': 192 | return logger.warn(result, 'validate unit error'); 193 | default: 194 | const _exhaustiveCheck: never = result; 195 | return _exhaustiveCheck; 196 | } 197 | } 198 | } 199 | 200 | async sendMyAddr(ws: Endpoint) { 201 | if (this.listenAddr) { 202 | logger.info(`${this.listenAddr} sending ${MY_ADDR}`); 203 | return this.sendData(ws, MY_ADDR, this.listenAddr); 204 | } else { 205 | return logger.info('this peer is not listening'); 206 | } 207 | } 208 | 209 | async handleMyAddr(ws: WebSocketClient, addr: string) { 210 | logger.info({addr}, `${this.listenAddr} handle ${MY_ADDR}`); 211 | if (this.clients.has(addr)) { 212 | return; 213 | } else { 214 | logger.info('try to connect the received addr'); 215 | return this.sendPing(addr); 216 | } 217 | } 218 | 219 | async sendPing(ws: Endpoint) { 220 | logger.info(`${this.listenAddr} sending ${PING}`); 221 | return this.sendData(ws, PING); 222 | } 223 | 224 | async handlePing(ws: Endpoint) { 225 | return logger.info(`${this.listenAddr} handle ${PING}`); 226 | } 227 | 228 | async sendGetWitnesses(ws: Endpoint) { 229 | logger.info(`${this.listenAddr} sending ${GET_WITNESSES}`); 230 | const witnesses = await this.sendRequest(ws, GET_WITNESSES); 231 | logger.info(witnesses, 'sendGetWitnesses'); 232 | return MyWitnesses.insertWitnesses(witnesses); 233 | } 234 | 235 | async handleGetWitnesses(ws: WebSocketClient, id: string) { 236 | const witnesses = await MyWitnesses.readWitnesses(); 237 | return this.sendResponse(ws, id, witnesses); 238 | } 239 | } 240 | 241 | const network = new Peer(); 242 | export default network; 243 | -------------------------------------------------------------------------------- /src/network/SocketAddr.ts: -------------------------------------------------------------------------------- 1 | import * as assert from 'assert'; 2 | import * as net from 'net'; 3 | 4 | export class SocketAddress { 5 | readonly isIPv4; 6 | 7 | constructor(readonly port: number, readonly ipv4?: string, readonly ipv6?: string) { 8 | assert(ipv4 || ipv6); 9 | this.isIPv4 = Boolean(ipv4); 10 | } 11 | 12 | static fromHostPort(host: string, port: number) { 13 | if (net.isIPv4(host)) { 14 | return new SocketAddress(port, host); 15 | } else { 16 | return new SocketAddress(port, null, host); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/network/WebSocketClient.ts: -------------------------------------------------------------------------------- 1 | import * as WebSocket from 'ws'; 2 | import ControlledPromise from '../common/ControlledPromise'; 3 | import logger from '../common/log'; 4 | import * as uuid from 'uuid/v4'; 5 | import * as http from 'http'; 6 | import timeoutPromise from '../common/timeoutPromise'; 7 | import {SocketAddress} from './SocketAddr'; 8 | 9 | const defaultOptions = { 10 | timeout: 60 * 1000, 11 | connectionTimeout: 0, 12 | }; 13 | 14 | const STATE = { 15 | CONNECTING: 0, 16 | OPEN: 1, 17 | CLOSING: 2, 18 | CLOSED: 3, 19 | }; 20 | 21 | enum MessageType { 22 | data, // just sending data 23 | request, // send and await response 24 | response, // must contains requestId 25 | } 26 | 27 | interface IPromise { 28 | resolve; 29 | reject; 30 | } 31 | 32 | class Request { 33 | private _promises: Map; 34 | 35 | constructor() { 36 | this._promises = new Map(); 37 | } 38 | 39 | async create(id: string, fn: (...params: any[]) => Promise, timeout: number): Promise { 40 | return timeoutPromise(new Promise(async (resolve, reject) => { 41 | await fn(); 42 | this._promises.set(id, {resolve, reject}); 43 | }), timeout); 44 | } 45 | 46 | resolve(id: string, data: any) { 47 | if (this._promises.has(id)) { 48 | this._promises.get(id).resolve(data); 49 | } 50 | } 51 | 52 | rejectAll(error?: any) { 53 | this._promises.forEach(promise => promise.reject(error)); 54 | } 55 | } 56 | 57 | export default class WebSocketClient { 58 | private _url: string; 59 | private _ws: WebSocket; 60 | private _options = defaultOptions; 61 | private _opening: ControlledPromise; 62 | private _closing: ControlledPromise; 63 | private _request: Request; 64 | 65 | socketAddress: SocketAddress; 66 | 67 | private _handleData = (data: any) => { 68 | // do nothing 69 | }; 70 | 71 | private _handleRequest = (id: string, data: any) => { 72 | // donothing 73 | }; 74 | 75 | constructor(ws: string | WebSocket, req?: http.IncomingMessage) { 76 | if (typeof ws === 'string') { 77 | this._url = ws; 78 | } else { 79 | this._ws = ws; 80 | } 81 | 82 | if (req) { 83 | this.socketAddress = SocketAddress.fromHostPort(req.connection.remoteAddress, req.connection.remotePort); 84 | } 85 | 86 | this._opening = new ControlledPromise(); 87 | this._closing = new ControlledPromise(); 88 | this._request = new Request(); 89 | } 90 | 91 | onData(fn: (data: any) => Promise) { 92 | this._handleData = fn; 93 | } 94 | 95 | onRequest(fn: (id: string, data: any) => Promise) { 96 | this._handleRequest = fn; 97 | } 98 | 99 | get url(): string { 100 | return this._url; 101 | } 102 | 103 | get ws(): WebSocket { 104 | return this._ws; 105 | } 106 | 107 | get isOpening(): boolean { 108 | return Boolean(this._ws && this._ws.readyState === STATE.CONNECTING); 109 | } 110 | 111 | get isOpened(): boolean { 112 | return Boolean(this._ws && this._ws.readyState === STATE.OPEN); 113 | } 114 | 115 | get isClosing(): boolean { 116 | return Boolean(this._ws && this._ws.readyState === STATE.CLOSING); 117 | } 118 | 119 | get isClosed(): boolean { 120 | return Boolean(!this._ws || this._ws.readyState === STATE.CLOSED); 121 | } 122 | 123 | async open(): Promise { 124 | if (this.isClosing) { 125 | return Promise.reject(new Error(`Can't open WebSocket while closing.`)); 126 | } 127 | if (this.isOpened) { 128 | return this._opening.promise; 129 | } 130 | return this._opening.call(() => { 131 | const timeout = this._options.connectionTimeout || this._options.timeout; 132 | this._opening.timeout(timeout, `Can't open WebSocket within allowed timeout: ${timeout} ms.`); 133 | this._opening.promise.catch(e => this._cleanup(e)); 134 | this._createWS(); 135 | }); 136 | } 137 | 138 | private async _send(data: any): Promise { 139 | if (this.isOpened) { 140 | this._ws.send(data); 141 | } else { 142 | throw new Error(`Can't send data because WebSocket is not opened.`); 143 | } 144 | } 145 | 146 | async sendData(data: any): Promise { 147 | const message = JSON.stringify([MessageType.data, data]); 148 | return this._send(message); 149 | } 150 | 151 | async sendRequest(data: any, id?: string, timeout?: number): Promise { 152 | id = id || `${uuid()}`; 153 | timeout = timeout || this._options.timeout; 154 | return this._request.create(id, async () => { 155 | const message = JSON.stringify([MessageType.request, {id: id, data: data}]); 156 | return this._send(message); 157 | }, timeout); 158 | } 159 | 160 | async sendResponse(id: string, data: any): Promise { 161 | const message = JSON.stringify([MessageType.response, {id: id, data: data}]); 162 | return this._send(message); 163 | } 164 | 165 | close() { 166 | if (this.isClosed) { 167 | return Promise.resolve(this._closing.value); 168 | } 169 | return this._closing.call(() => { 170 | const {timeout} = this._options; 171 | this._closing.timeout(timeout, `Can't close WebSocket within allowed timeout: ${timeout} ms.`); 172 | this._ws.close(); 173 | }); 174 | } 175 | 176 | _createWS() { 177 | this._ws = new WebSocket(this._url); 178 | this._ws.on('open', e => this._handleOpen(e)); 179 | this._ws.on('message', e => this._handleMessage(e)); 180 | this._ws.on('error', e => this._handleError(e)); 181 | this._ws.on('close', e => this._handleClose(e)); 182 | } 183 | 184 | _handleOpen(event) { 185 | this._opening.resolve(event); 186 | } 187 | 188 | async _handleMessage(event) { 189 | const messages = JSON.parse(event); 190 | const type = messages[0]; 191 | const content = messages[1]; 192 | switch (type) { 193 | case MessageType.data: 194 | return this._handleData(content); 195 | case MessageType.request: 196 | return this._handleRequest(content.id, content.data); 197 | case MessageType.response: 198 | return this._handleResponse(content.id, content.data); 199 | } 200 | } 201 | 202 | async _handleResponse(id: string, data: any) { 203 | return this._request.resolve(id, data); 204 | } 205 | 206 | _handleError(err: Error) { 207 | logger.error(`error on ${this._url}, ${err}`); 208 | } 209 | 210 | _handleClose(event) { 211 | this._closing.resolve(event); 212 | const error = new Error(`WebSocket closed with reason: ${event.reason} (${event.code}).`); 213 | if (this._opening.isPending) { 214 | this._opening.reject(error); 215 | } 216 | this._cleanup(error); 217 | } 218 | 219 | _cleanupWS() { 220 | this._ws = null; 221 | } 222 | 223 | _cleanup(error?: any) { 224 | this._cleanupWS(); 225 | this._request.rejectAll(error); 226 | } 227 | } 228 | 229 | -------------------------------------------------------------------------------- /src/network/WebSocketServer.ts: -------------------------------------------------------------------------------- 1 | import * as WebSocket from 'ws'; 2 | import {ServerOptions} from 'ws'; 3 | import * as Koa from 'koa'; 4 | import logger from '../common/log'; 5 | import * as co from 'co'; 6 | import * as http from 'http'; 7 | import WebSocketClient from './WebSocketClient'; 8 | import compose = require('koa-compose'); 9 | import {SocketAddress} from './SocketAddr'; 10 | 11 | export interface Context extends Koa.Context { 12 | ws: WebSocketClient; 13 | data: any; 14 | } 15 | 16 | export default class WebSocketServer extends Koa { 17 | clients: Set = new Set(); 18 | wss: WebSocket.Server; 19 | 20 | constructor() { 21 | super(); 22 | } 23 | 24 | close() { 25 | this.wss.close(); 26 | this.clients.forEach(x => x.close()); 27 | } 28 | 29 | async broadcast(data: any) { 30 | for (const client of this.clients) { 31 | await client.sendData(data); 32 | } 33 | } 34 | 35 | use(fn: (ctx: Context, next: any) => Promise): this { 36 | return super.use(fn); 37 | } 38 | 39 | onConnection(ws: WebSocket, req: http.IncomingMessage) { 40 | const client = new WebSocketClient(ws, req); 41 | this.clients.add(client); 42 | const ctx = this.createContext(req, null); 43 | ctx.ws = client; 44 | ctx.socketAddr = SocketAddress.fromHostPort(req.connection.remoteAddress, req.connection.remotePort); 45 | 46 | const fn = co.wrap(compose(this.middleware)); 47 | ws.on('message', data => { 48 | ctx.data = data; 49 | fn(ctx).catch(function (err) { 50 | logger.error(err); 51 | }); 52 | }); 53 | 54 | ws.on('close', () => { 55 | logger.info('on close'); 56 | this.clients.delete(client); 57 | }); 58 | 59 | ws.on('error', err => { 60 | logger.error(err); 61 | }); 62 | } 63 | 64 | start(wsOptions?: ServerOptions) { 65 | this.wss = new WebSocket.Server(wsOptions); 66 | this.wss.on('connection', this.onConnection.bind(this)); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/network/peer.test.ts: -------------------------------------------------------------------------------- 1 | import {Peer} from './Peer'; 2 | import {sleep} from '../common/utils'; 3 | 4 | test('test peers', async () => { 5 | const peer = new Peer(); 6 | peer.listen(4000); 7 | const peer2 = new Peer(); 8 | peer2.listen(5000); 9 | const peer3 = new Peer(); 10 | peer3.listen(6000); 11 | 12 | 13 | // peer2 connect to peer1 14 | await peer2.sendMyAddr('ws://localhost:3000'); 15 | 16 | // peer3 request peer1's peers 17 | await sleep(1000); 18 | await peer3.sendGetPeers('ws://localhost:3000'); 19 | 20 | peer.close(); 21 | peer2.close(); 22 | peer3.close(); 23 | }); 24 | -------------------------------------------------------------------------------- /src/network/pool.test.ts: -------------------------------------------------------------------------------- 1 | import {Peer} from './Peer'; 2 | 3 | test('test pool', async () => { 4 | const poolPort = 4000; 5 | const pool = new Peer(); 6 | pool.listen(poolPort); 7 | 8 | const peer = new Peer(); 9 | try { 10 | const resp = await peer.sendRequest(`ws://localhost:${poolPort}`, 'get_peers', ''); 11 | console.log(resp); 12 | } catch (e) { 13 | console.log(e); 14 | } 15 | 16 | pool.close(); 17 | }); 18 | -------------------------------------------------------------------------------- /src/network/websocket.test.ts: -------------------------------------------------------------------------------- 1 | import WebSocketServer from './WebSocketServer'; 2 | import WebSocketClient from './WebSocketClient'; 3 | 4 | test('test web socket client/server', async () => { 5 | const port = 3000; 6 | const server = new WebSocketServer(); 7 | server.use(async (ctx) => { 8 | const messages = JSON.parse(ctx.data); 9 | const type = messages[0]; 10 | const content = messages[1]; 11 | 12 | switch (type) { 13 | case 0: 14 | console.log('server on type 0'); 15 | return await ctx.ws.sendData(`received ${content}j`); 16 | case 1: 17 | console.log('server on type 1'); 18 | console.log('content', content); 19 | // return await ctx.ws.sendResponse(content.id, `received req ${content.data}`); 20 | } 21 | }); 22 | 23 | server.start({port: port}); 24 | const client1 = new WebSocketClient(`ws://localhost:${port}`); 25 | const client2 = new WebSocketClient(`ws://localhost:${port}`); 26 | 27 | await client1.open(); 28 | client1.onData(async (data) => { 29 | console.log(`client: on message ${data}`); 30 | }); 31 | 32 | await client1.sendData(`hello`); 33 | await client2.open(); 34 | 35 | await server.broadcast('broadcast'); 36 | const response = await client2.sendRequest('hello req'); 37 | console.log('response: ', response); 38 | server.close(); 39 | }); 40 | -------------------------------------------------------------------------------- /src/storage/kvstore.test.ts: -------------------------------------------------------------------------------- 1 | import kvstore from './kvstore'; 2 | 3 | test('test kvstore', async () => { 4 | await kvstore.put('testKey', 'testValue'); 5 | const value1 = await kvstore.get('testKey'); 6 | expect(value1.toString()).toBe('testValue'); 7 | 8 | let value; 9 | try { 10 | value = await kvstore.get('ne'); 11 | console.log(value.toString); 12 | } catch (e) { 13 | console.log(e.toString()); 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /src/storage/kvstore.ts: -------------------------------------------------------------------------------- 1 | import leveldown from 'leveldown'; 2 | import * as levelup from 'levelup'; 3 | 4 | export interface IKVStore { 5 | put(key: any, value: any): Promise; 6 | 7 | get(key: any): Promise; 8 | 9 | del(key: any): Promise; 10 | } 11 | 12 | class LevelDBStore implements IKVStore { 13 | private _db: any; 14 | 15 | constructor() { 16 | this._db = levelup(leveldown('./leveldb')); 17 | } 18 | 19 | async put(key: any, value: any): Promise { 20 | return this._db.put(key, value); 21 | } 22 | 23 | async get(key: any): Promise { 24 | return this._db.get(key); 25 | } 26 | 27 | async del(key: any): Promise { 28 | return this._db.del(key); 29 | } 30 | } 31 | 32 | export const kvstore: IKVStore = new LevelDBStore(); 33 | export default kvstore; 34 | -------------------------------------------------------------------------------- /src/storage/sqlstore.test.ts: -------------------------------------------------------------------------------- 1 | import {default as sqlstore} from './sqlstore'; 2 | 3 | test('test sqlstore', async () => { 4 | const row = await sqlstore.get(`SELECT 1`); 5 | console.log(row['1']); 6 | }); 7 | -------------------------------------------------------------------------------- /src/storage/sqlstore.ts: -------------------------------------------------------------------------------- 1 | import * as sqlite from 'sqlite'; 2 | import {Database} from 'sqlite'; 3 | 4 | export interface ISqlStore { 5 | get(sql: string, ...params: any[]): Promise; 6 | 7 | all(sql: string, ...params: any[]): Promise; 8 | 9 | run(sql: string, ...params: any[]): Promise; 10 | 11 | exec(sql: string): Promise; 12 | } 13 | 14 | export class SqliteStore implements ISqlStore { 15 | private _db: Database; 16 | 17 | async getOrOpen() { 18 | if (!this._db) 19 | return this.open(); 20 | } 21 | 22 | async open(filename: string = ':memory:') { 23 | this._db = await sqlite.open(filename); 24 | await this._db.migrate({force: 'last', migrationsPath: './migrations'}); 25 | } 26 | 27 | async get(sql: string, ...params): Promise { 28 | await this.getOrOpen(); 29 | return this._db.get(sql, params); 30 | } 31 | 32 | async all(sql: string, ...params): Promise { 33 | await this.getOrOpen(); 34 | return this._db.all(sql, params); 35 | } 36 | 37 | async run(sql: string, ...params): Promise { 38 | await this.getOrOpen(); 39 | return this._db.run(sql, params); 40 | } 41 | 42 | async exec(sql: string): Promise { 43 | await this.getOrOpen(); 44 | return this._db.exec(sql); 45 | } 46 | 47 | escape(xs: any[]): string { 48 | return xs.map(SqliteStore.escape).join(','); 49 | } 50 | 51 | static getUnixTimestamp(date: string) { 52 | return `strftime('%s', ${date})`; 53 | } 54 | 55 | static escape(str) { 56 | if (typeof str === 'string') 57 | return "'" + str.replace(/'/g, "''") + "'"; 58 | else if (Array.isArray(str)) 59 | return str.map(function (member) { 60 | return SqliteStore.escape(member); 61 | }).join(','); 62 | else 63 | throw Error('escape: unknown type ' + (typeof str)); 64 | } 65 | } 66 | 67 | const sqlstore = new SqliteStore(); 68 | export default sqlstore; 69 | -------------------------------------------------------------------------------- /src/typings/type.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'secp256k1'; 2 | declare module 'bitcore-lib'; 3 | declare module 'bitcore-mnemonic'; 4 | declare module 'websocket-as-promised'; 5 | -------------------------------------------------------------------------------- /src/wallets/Wallets.ts: -------------------------------------------------------------------------------- 1 | import {Device} from '../core/device'; 2 | import * as _ from 'lodash'; 3 | import sqlstore from '../storage/sqlstore'; 4 | import HDKey from '../common/HDKey'; 5 | import * as Bitcore from 'bitcore-lib'; 6 | import Wallet from './wallet'; 7 | import HDKeys from '../common/HDKeys'; 8 | import XPubKeys from '../models/XPubKeys'; 9 | import logger from '../common/log'; 10 | 11 | export default class Wallets { 12 | static async readWalletIds(): Promise { 13 | const rows = await sqlstore.all('SELECT wallet FROM wallets'); 14 | return rows.map(row => row.wallet); 15 | } 16 | 17 | static async read() { 18 | return sqlstore.all('select * from wallets'); 19 | } 20 | 21 | static async create(key: HDKey): Promise { 22 | const device = new Device(key.devicePrivKey); 23 | const derived = key.xPrivKey.derive(0, 0); 24 | const xPubKeyStr = new Bitcore.HDPublicKey(derived).toString(); 25 | const wallet = key.walletId; 26 | const definition = ['sig', {pubkey: '$pubkey@' + device.deviceAddress()}]; 27 | const deviceAddressesBySigningPaths = getDeviceAddressesBySigningPaths(definition); 28 | const deviceAddresses = _.uniq([...deviceAddressesBySigningPaths.values()]); 29 | 30 | await sqlstore.run( 31 | 'INSERT INTO wallets (wallet, account, definition_template) VALUES (?,?,?)', 32 | wallet, 0, JSON.stringify(definition), 33 | ); 34 | 35 | await Promise.all(deviceAddresses.map(async (address) => { 36 | return sqlstore.run(` 37 | INSERT INTO extended_pubkeys (wallet, device_address, extended_pubkey, approval_date) 38 | VALUES (?,?,?,datetime('now'))`, 39 | wallet, address, xPubKeyStr, 40 | ); 41 | })); 42 | 43 | for (const [path, address] of deviceAddressesBySigningPaths) { 44 | await sqlstore.run(` 45 | INSERT INTO wallet_signing_paths (wallet, signing_path, device_address) VALUES (?,?,?)`, 46 | wallet, path, address, 47 | ); 48 | } 49 | 50 | return new Wallet(wallet, key); 51 | } 52 | 53 | static async readWalletAddresses(wallet: string): Promise { 54 | const rows = await sqlstore.all( 55 | `SELECT address FROM my_addresses where wallet=?`, 56 | wallet, 57 | ); 58 | return rows.map(row => row.address); 59 | } 60 | 61 | static async readOrCreate(passphrase?: string, keyPath?: string): Promise { 62 | const key = await HDKeys.readOrCreate(passphrase, keyPath); 63 | const device = new Device(key.devicePrivKey); 64 | const wallet = key.walletId; 65 | if (await Wallets.isWalletExists(wallet)) { 66 | logger.info(`wallet ${wallet} exists`); 67 | const pk = await XPubKeys.findByDeviceAddress(device.deviceAddress()); 68 | logger.info(`pk: ${pk}`); 69 | if (!pk) { 70 | throw Error('incorrect passphrase'); 71 | } 72 | return new Wallet(wallet, key); 73 | } else { 74 | logger.info(`wallet ${wallet} does not exist, will create`); 75 | return Wallets.create(key); 76 | } 77 | } 78 | 79 | static async isWalletExists(wallet?: string) { 80 | if (!wallet) { 81 | const rows = await sqlstore.all('SELECT wallet FROM wallets'); 82 | return rows.length > 0; 83 | } else { 84 | const rows = await sqlstore.all('SELECT wallet FROM wallets where wallet=?', wallet); 85 | return rows.length > 0; 86 | } 87 | } 88 | } 89 | 90 | function getDeviceAddressesBySigningPaths(definitionTemplates: any[]): Map { 91 | function evaluate(arr: any[], path: string) { 92 | const op = arr[0]; 93 | const args = arr[1]; 94 | if (!args) 95 | return; 96 | const prefix = '$pubkey@'; 97 | switch (op) { 98 | case 'sig': 99 | if (!args.pubkey || args.pubkey.substr(0, prefix.length) !== prefix) 100 | return; 101 | const deviceAddress = args.pubkey.substr(prefix.length); 102 | deviceAddressesBySigningPaths.set(path, deviceAddress); 103 | break; 104 | } 105 | } 106 | 107 | const deviceAddressesBySigningPaths = new Map(); 108 | evaluate(definitionTemplates, 'r'); 109 | return deviceAddressesBySigningPaths; 110 | } 111 | -------------------------------------------------------------------------------- /src/wallets/wallet.ts: -------------------------------------------------------------------------------- 1 | import sqlstore from '../storage/sqlstore'; 2 | import {Signer} from '../core/signer'; 3 | import * as balances from '../core/balances'; 4 | import {Balances} from '../core/balances'; 5 | import Wallets from './Wallets'; 6 | import HDKey from '../common/HDKey'; 7 | import logger from '../common/log'; 8 | import * as composer from '../core/composer'; 9 | import network from '../network/Peer'; 10 | import Units from '../models/Units'; 11 | 12 | const TYPICAL_FEE = 1000; 13 | const MAX_FEE = 20000; 14 | 15 | export default class Wallet { 16 | signer: Signer; 17 | 18 | constructor( 19 | readonly wallet: Base64, 20 | readonly key: HDKey, 21 | ) { 22 | this.signer = new Signer(this.key.xPrivKey); 23 | } 24 | 25 | async address(): Promise
{ 26 | const addresses = await Wallets.readWalletAddresses(this.wallet); 27 | return addresses[0]; 28 | } 29 | 30 | async readBalance(address?: Address): Promise { 31 | if (!address) { 32 | address = await this.address(); 33 | } 34 | return balances.readBalance(address); 35 | } 36 | 37 | async sendPayment(to: Address, amount: number, witnesses: Address[]) { 38 | const changeAddress = await this.address(); 39 | const [fundedAddresses, signingAddresses] = await readFundedAndSigningAddresses( 40 | this.wallet, amount + TYPICAL_FEE, []); 41 | 42 | logger.info({fundedAddresses, signingAddresses}, 'send payment'); 43 | 44 | const outputs = [{ 45 | address: to, 46 | amount: amount, 47 | }]; 48 | 49 | const unit = await composer.composeUnit( 50 | witnesses, 51 | signingAddresses, 52 | fundedAddresses, 53 | changeAddress, 54 | outputs, 55 | this.signer, 56 | ); 57 | 58 | for (const author of unit.authors) { 59 | author.authentifiers['r'] = await this.signer.sign(unit, author.address, 'r'); 60 | } 61 | 62 | unit.unit = unit.calcUnit(); 63 | unit.ball = unit.calcBall(); 64 | 65 | logger.info(unit, 'sendPayment'); 66 | 67 | // save 68 | await Units.save(unit, 'good'); 69 | 70 | // broadcast 71 | await network.broadcastUnit(unit); 72 | } 73 | } 74 | 75 | async function readFundedAndSigningAddresses( 76 | walletId: Base64, 77 | estimatedAmount: number, 78 | signingAddresses: Address[], 79 | ): Promise<[Address[], Address[]]> { 80 | const fundedAddresses = await readFundedAddresses(walletId, estimatedAmount); 81 | logger.info(fundedAddresses, 'readFundedAddresses'); 82 | return [fundedAddresses, signingAddresses]; 83 | } 84 | 85 | async function readFundedAddresses(wallet: Base64, estimatedAmount: number): Promise { 86 | // find my paying utxo addresses 87 | // sort by |amount - estimatedAmount| 88 | const orderBy = `(SUM(amount) > ${estimatedAmount}) DESC, ABS(SUM(amount)-${estimatedAmount}) ASC`; 89 | const utxo = await sqlstore.all(` 90 | SELECT address, SUM(amount) AS total 91 | FROM outputs JOIN my_addresses USING(address) 92 | CROSS JOIN units USING(unit) 93 | WHERE wallet=? AND is_stable=1 AND sequence='good' AND is_spent=0 AND asset IS NULL 94 | AND NOT EXISTS ( 95 | SELECT * FROM unit_authors JOIN units USING(unit) 96 | WHERE is_stable=0 AND unit_authors.address=outputs.address AND definition_chash IS NOT NULL 97 | ) 98 | GROUP BY address ORDER BY ${orderBy}`, 99 | wallet, 100 | ); 101 | 102 | const fundedAddresses = []; 103 | let accumulatedAmount = 0; 104 | for (let i = 0; i < utxo.length; i++) { 105 | fundedAddresses.push(utxo[i].address); 106 | accumulatedAmount += utxo[i].total; 107 | if (accumulatedAmount > estimatedAmount + MAX_FEE) { 108 | break; 109 | } 110 | } 111 | return fundedAddresses; 112 | } 113 | -------------------------------------------------------------------------------- /src/wallets/wallet_general.ts: -------------------------------------------------------------------------------- 1 | import device from '../core/device'; 2 | 3 | async function sendPrivatePayments(to: Address, chains: any[], forwarded: boolean) { 4 | const body: any = {chains}; 5 | if (forwarded) { 6 | body.forwarded = forwarded; 7 | } 8 | return device.sendMessageToDevice(to, 'private_payments', body); 9 | } 10 | -------------------------------------------------------------------------------- /src/wallets/wallets.test.ts: -------------------------------------------------------------------------------- 1 | import Wallets from './Wallets'; 2 | import * as rimraf from 'rimraf'; 3 | import HDKeys from '../common/HDKeys'; 4 | import MyAddresses from '../models/MyAddresses'; 5 | import XPubKeys from '../models/XPubKeys'; 6 | 7 | jest.setTimeout(60 * 1000); 8 | 9 | test('gen wallets', async () => { 10 | const tmpDir = './tmp'; 11 | rimraf.sync(tmpDir); 12 | 13 | for (let i = 0; i < 2; i++) { 14 | const keyPath = `${tmpDir}/key_${i}.json`; 15 | const key = await HDKeys.readOrCreate('', keyPath); 16 | const wallet = await Wallets.create(key); 17 | await MyAddresses.issueOrSelectNextAddress(wallet.wallet); 18 | } 19 | 20 | console.log(await Wallets.read()); 21 | console.log(await MyAddresses.read()); 22 | console.log(await XPubKeys.all()); 23 | }); 24 | -------------------------------------------------------------------------------- /src/witness/witness.ts: -------------------------------------------------------------------------------- 1 | import {Output} from '../core/message'; 2 | import sqlstore from '../storage/sqlstore'; 3 | import {composeUnit} from '../core/composer'; 4 | import logger from '../common/log'; 5 | import network from '../network/Peer'; 6 | import Wallet from '../wallets/wallet'; 7 | import MainChain from '../models/MainChain'; 8 | import Wallets from '../wallets/Wallets'; 9 | import MyAddresses from '../models/MyAddresses'; 10 | 11 | const WITNESSING_COST = 600; // size of typical witnessing unit 12 | const THRESHOLD_DISTANCE = 50; 13 | const MIN_AVAILABLE_WITNESSINGS = 100; 14 | 15 | async function determineIfThereAreMyUnitsWithoutMci(address: Address) { 16 | const rows = await sqlstore.all( 17 | 'SELECT 1 FROM units JOIN unit_authors USING(unit) WHERE address=? AND main_chain_index IS NULL LIMIT 1', 18 | address, 19 | ); 20 | return rows.length > 0; 21 | } 22 | 23 | 24 | async function readNumberOfWitnessingsAvailable(address: Address, available: number): Promise { 25 | if (available > MIN_AVAILABLE_WITNESSINGS) 26 | return available; 27 | let rows = await sqlstore.all(` 28 | SELECT COUNT(*) AS count_big_outputs FROM outputs JOIN units USING(unit) 29 | WHERE address=? AND is_stable=1 AND amount>=? AND asset IS NULL AND is_spent=0`, 30 | address, WITNESSING_COST, 31 | ); 32 | const numBigOutputs = rows[0].count_big_outputs; 33 | logger.info(`count of big outputs(>= ${WITNESSING_COST}) ${numBigOutputs}`); 34 | 35 | rows = await sqlstore.all(` 36 | SELECT SUM(amount) AS total FROM outputs JOIN units USING(unit) 37 | WHERE address=? AND is_stable=1 AND amount acc + cur.total, 0); 47 | const paid = Math.round(total / WITNESSING_COST); 48 | return numBigOutputs + paid; 49 | } 50 | 51 | 52 | export class Witness { 53 | available = 0; // available count of witnessings 54 | forcedWitnessingTimer; 55 | isWitnessingUnderWay = false; 56 | 57 | constructor(readonly wallet: Wallet) { 58 | } 59 | 60 | async start() { 61 | const address = await this.wallet.address(); 62 | await this.checkAndWitness(this.wallet, address); 63 | } 64 | 65 | // make sure we never run out of spendable (stable) outputs. 66 | // Keep the number above a threshold, and if it drops below, produce more outputs than consume. 67 | async createOptimalOutputs(address: Address): Promise { 68 | const outputs = []; 69 | this.available = await readNumberOfWitnessingsAvailable(address, this.available); 70 | if (this.available > MIN_AVAILABLE_WITNESSINGS) 71 | return outputs; 72 | // try to split the biggest output in two 73 | const row = await sqlstore.get(` 74 | SELECT amount FROM outputs JOIN units USING(unit) 75 | WHERE address=? AND is_stable=1 AND amount>=? AND asset IS NULL AND is_spent=0 76 | ORDER BY amount DESC LIMIT 1`, 77 | address, 2 * WITNESSING_COST, 78 | ); 79 | if (!row) { 80 | logger.info(`only ${this.available} spendable outputs left`); 81 | } else { 82 | const amount = row.amount; 83 | logger.info(`only ${this.available} spendable outputs left, will split an output of ${amount}`); 84 | outputs.push({amount: Math.round(amount / 2), address: address}); 85 | } 86 | return outputs; 87 | } 88 | 89 | async checkAndWitness(wallet: Wallet, address: Address) { 90 | clearTimeout(this.forcedWitnessingTimer); 91 | if (this.isWitnessingUnderWay) 92 | return logger.info('witnessing under way'); 93 | this.isWitnessingUnderWay = true; 94 | // abort if there are my units without an mci 95 | const myUnitsWithoutMci = await determineIfThereAreMyUnitsWithoutMci(address); 96 | if (myUnitsWithoutMci) { 97 | this.isWitnessingUnderWay = false; 98 | } else { 99 | const maxMCI = 0; 100 | const rows = await sqlstore.all(` 101 | SELECT main_chain_index AS max_my_mci 102 | FROM units JOIN unit_authors USING(unit) 103 | WHERE +address=? ORDER BY unit_authors.rowid 104 | DESC LIMIT 1`, 105 | address, 106 | ); 107 | const myMaxMCI = (rows.length > 0) ? rows[0].max_my_mci : -1000; 108 | const distance = maxMCI - myMaxMCI; 109 | logger.info('distance=' + distance); 110 | if (distance > THRESHOLD_DISTANCE) { 111 | logger.info('distance above threshold, will witness'); 112 | try { 113 | await this.witness(wallet, address); 114 | } catch (e) { 115 | logger.warn(e, 'wait and try again'); 116 | setTimeout(async () => this.checkAndWitness(wallet, address), 60000); 117 | } finally { 118 | this.isWitnessingUnderWay = false; 119 | } 120 | } else { 121 | return this.checkForUnconfirmedUnits(THRESHOLD_DISTANCE - distance); 122 | } 123 | } 124 | } 125 | 126 | async witness(wallet: Wallet, address: Address) { 127 | const outputs = await this.createOptimalOutputs(address); 128 | logger.info(outputs, 'witness outputs'); 129 | const payingAddresses = [address]; 130 | const unit = await composeUnit([], [], payingAddresses, address, outputs, wallet.signer); 131 | logger.info(unit, 'witness unit'); 132 | return network.broadcastUnit(unit); 133 | } 134 | 135 | async checkForUnconfirmedUnits(distance2threshold: number) { 136 | const rows = await sqlstore.all(` 137 | SELECT 1 FROM units CROSS JOIN unit_authors USING(unit) LEFT JOIN my_witnesses USING(address) 138 | WHERE (main_chain_index>? OR main_chain_index IS NULL AND sequence='good') 139 | AND my_witnesses.address IS NULL 140 | AND NOT ( 141 | (SELECT COUNT(*) FROM messages WHERE messages.unit=units.unit)=1 142 | AND (SELECT COUNT(*) FROM unit_authors WHERE unit_authors.unit=units.unit)=1 143 | AND (SELECT COUNT(DISTINCT address) FROM outputs WHERE outputs.unit=units.unit)=1 144 | AND (SELECT address FROM outputs WHERE outputs.unit=units.unit LIMIT 1)=unit_authors.address 145 | ) 146 | LIMIT 1`, 147 | MainChain.minRetrievableMCI(), 148 | ); 149 | 150 | if (rows.length === 0) 151 | return; 152 | 153 | const timeout = Math.round((distance2threshold + Math.random()) * 10000); 154 | logger.info('scheduling unconditional witnessing in ' + timeout + ' ms unless a new unit arrives'); 155 | this.forcedWitnessingTimer = setTimeout(this.witnessBeforeThreshold, timeout); 156 | } 157 | 158 | async witnessBeforeThreshold(wallet: Wallet, address: Address) { 159 | if (this.isWitnessingUnderWay) 160 | return; 161 | this.isWitnessingUnderWay = true; 162 | const myUnitsWithoutMci = await determineIfThereAreMyUnitsWithoutMci(address); 163 | if (myUnitsWithoutMci) { 164 | this.isWitnessingUnderWay = false; 165 | return; 166 | } 167 | logger.info('will witness before threshold'); 168 | await this.witness(wallet, address); 169 | this.isWitnessingUnderWay = false; 170 | } 171 | } 172 | 173 | async function main() { 174 | const wallet = await Wallets.readOrCreate(''); 175 | logger.info(wallet, 'witness wallet'); 176 | const address = await MyAddresses.issueOrSelectNextAddress(wallet.wallet, 0); 177 | logger.info({address}, 'witness address'); 178 | 179 | const witness = new Witness(wallet); 180 | await witness.start(); 181 | } 182 | 183 | (async () => { 184 | await main(); 185 | })(); 186 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "downlevelIteration": true, 4 | "experimentalDecorators": true, 5 | "outDir": "dist", 6 | "target": "es6", 7 | "lib": [ 8 | "es2015" 9 | ], 10 | "moduleResolution": "node", 11 | "module": "commonjs", 12 | "sourceMap": true, 13 | "alwaysStrict": true, 14 | "noImplicitReturns": true, 15 | "noImplicitThis": true 16 | }, 17 | "include": [ 18 | "src/**/*.ts" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "eofline": true, 4 | "indent": [ 5 | true, 6 | "spaces" 7 | ], 8 | "max-line-length": [ 9 | true, 10 | 140 11 | ], 12 | "no-string-throw": true, 13 | "prefer-const": true, 14 | "typeof-compare": true, 15 | "no-var-keyword": true, 16 | "quotemark": [ 17 | true, 18 | "single", 19 | "avoid-escape" 20 | ], 21 | "semicolon": [ 22 | true 23 | ], 24 | "switch-default": false, 25 | "trailing-comma": [ 26 | true, 27 | { 28 | "multiline": "always", 29 | "singleline": "never" 30 | } 31 | ], 32 | "triple-equals": [ 33 | true 34 | ], 35 | "variable-name": [ 36 | true, 37 | "check-format", 38 | "allow-leading-underscore", 39 | "ban-keywords", 40 | "allow-pascal-case" 41 | ], 42 | "whitespace": [ 43 | true, 44 | "check-branch", 45 | "check-decl", 46 | "check-operator", 47 | "check-separator", 48 | "check-type" 49 | ] 50 | } 51 | } 52 | --------------------------------------------------------------------------------