All files / lib export.js

0% Statements 0/249
0% Branches 0/6
0% Functions 0/33
0% Lines 0/245

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
/*!
 * export.js - Export and import serializations for
 * Wallet, Account and Cosigner.
 * Copyright (c) 2019, The Bcoin Developers (MIT License).
 */
 
'use strict';
 
const assert = require('bsert');
const Struct = require('bufio/lib/struct');
const wcommon = require('bcoin/lib/wallet/common');
const HDPublicKey = require('bcoin/lib/hd/public');
const Account = require('bcoin/lib/wallet/account');
const MasterKey = require('bcoin/lib/wallet/masterkey');
const Cosigner = require('./primitives/cosigner');
const common = require('./utils/common');
 
const NULL_TOKEN = Buffer.alloc(32, 0);
const NULL_KEY = Buffer.alloc(33, 0);
 
/**
 * Cosigner dump object.
 * Wraps Cosigner primitive.
 */
 
class CosignerDetails extends Struct {
  constructor() {
    super();
 
    this.cosigner = new Cosigner();
  }
 
  fromCosigner(cosigner) {
    this.cosigner = cosigner.clone();
 
    return this;
  }
 
  getSize() {
    return this.cosigner.getSize();
  }
 
  write(bw, network) {
    return this.cosigner.write(bw, network);
  }
 
  read(br, network) {
    this.cosigner.read(br, network);
 
    return this;
  }
 
  getJSON(network) {
    return this.cosigner.getJSON(true, network);
  }
 
  fromJSON(json, network) {
    this.cosigner.fromJSON(json, true, network);
 
    return this;
  }
 
  static fromCosigner(cosigner) {
    return new this().fromCosigner(cosigner);
  }
}
 
class AccountDetails extends Struct {
  constructor() {
    super();
 
    this.accountIndex = 0;
    this.name = 'default';
    this.witness = false;
    this.lookahead = 10;
    this.type = Account.types.MULTISIG;
    this.initialized = true;
 
    this.m = 1;
    this.n = 1;
 
    // These wont be actually restored, as they can be easily
    // restored when rescanning. (will be `lookahead` when importing)
    this.receiveDepth = 0;
    this.changeDepth = 0;
    this.nestedDepth = 0;
 
    // These will be recovered from cosigners array.
    this.accountKey = null;
    this.keys = [];
  }
 
  /**
   * @param {Account} account
   * @returns {AccountDetails}
   */
 
  fromAccount(account) {
    this.accountIndex = account.accountIndex;
    this.name = account.name;
    this.witness = account.witness;
    this.receiveDepth = account.receiveDepth;
    this.changeDepth = account.changeDepth;
    this.nestedDepth = account.nestedDepth;
    this.lookahead = account.lookahead;
    this.type = account.type;
 
    this.m = account.m;
    this.n = account.n;
 
    // These are not utilized in bmultisig,
    // it is here, for future compatibility
    // for bcoin export/import formats.
    this.accountKey = common.cloneHDPublicKey(account.accountKey);
    this.keys = [];
 
    for (const key of account.keys)
      this.keys.push(common.cloneHDPublicKey(key));
 
    return this;
  }
 
  getSize() {
    let size = 0;
 
    // standard account serialization from bcoin
    size += 1; // flags
    size += 1; // type
    size += 2; // m and n
    size += 4; // receiveDepth
    size += 4; // changeDepth
    size += 4; // nestedDepth
    size += 1; // lookahead
    size += 74; // 1 + 4 + 4 + 32 + 33 AccountKey
    size += 1; // keys length
    size += 74 * this.keys.length;
 
    // export
    size += 4; // accountIndex
    size += 1; // account name length
    size += this.name.length; // we only accept ASCII
 
    return size;
  }
 
  write(bw) {
    let flags = 0;
 
    if (this.initialized)
      flags |= 1;
 
    if (this.witness)
      flags |= 2;
 
    bw.writeU8(flags);
    bw.writeU8(this.type);
    bw.writeU8(this.m);
    bw.writeU8(this.n);
    bw.writeU32(this.receiveDepth);
    bw.writeU32(this.changeDepth);
    bw.writeU32(this.nestedDepth);
    bw.writeU8(this.lookahead);
    writeKey(this.accountKey, bw);
    bw.writeU8(this.keys.length);
 
    for (const key of this.keys)
      writeKey(key, bw);
 
    bw.writeU32(this.accountIndex);
    bw.writeU8(this.name.length);
    bw.writeString(this.name, 'ascii');
 
    return bw;
  }
 
  read(br) {
    const flags = br.readU8();
 
    this.initialized = (flags & 1) !== 0;
    this.witness = (flags & 2) !== 0;
    // default type.
    this.type = br.readU8();
    this.m = br.readU8();
    this.n = br.readU8();
    this.receiveDepth = br.readU32();
    this.changeDepth = br.readU32();
    this.nestedDepth = br.readU32();
    this.lookahead = br.readU8();
    this.accountKey = readKey(br);
 
    const keys = br.readU8();
    for (let i = 0; i < keys; i++) {
      const key = readKey(br);
      this.keys.push(key);
    }
 
    this.accountIndex = br.readU32();
    const nameLength = br.readU8();
    this.name = br.readBytes(nameLength).toString('ascii');
 
    return this;
  }
 
  getJSON(network) {
    return {
      name: this.name,
      witness: this.witness,
      initialized: this.initialized,
      watchOnly: true, // not part of the serialization
      type: Account.typesByVal[Account.types.MULTISIG].toLowerCase(),
      m: this.m,
      n: this.n,
      accountIndex: this.accountIndex,
      receiveDepth: this.receiveDepth,
      changeDepth: this.changeDepth,
      nestedDepth: this.nestedDepth,
      lookahead: this.lookahead,
      accountKey: this.accountKey.toBase58(network),
      keys: this.keys.map(key => key.toBase58(network))
    };
  }
 
  fromJSON(json, network) {
    assert(json);
    assert(typeof json.name === 'string');
    assert(wcommon.isName(json.name));
    assert(typeof json.witness === 'boolean');
    assert(typeof json.initialized === 'boolean');
    assert((json.receiveDepth >>> 0) === json.receiveDepth);
    assert((json.changeDepth >>> 0) === json.changeDepth);
    assert((json.nestedDepth >>> 0) === json.nestedDepth);
    assert((json.lookahead >>> 0) === json.lookahead);
    assert(json.lookahead <= Account.MAX_LOOKAHEAD);
    assert((json.m & 0xff) === json.m);
    assert((json.n & 0xff) === json.n);
 
    this.name = json.name;
    this.witness = json.witness;
    this.receiveDepth = json.receiveDepth;
    this.changeDepth = json.changeDepth;
    this.nestedDepth = json.nestedDepth;
    this.lookahead = json.lookahead;
    this.initialized = json.initialized;
 
    this.m = json.m;
    this.n = json.n;
 
    // we recover accountKey and keys from cosigners..
    this.accountKey = HDPublicKey.fromBase58(json.accountKey, network);
    this.keys = json.keys.map(k => HDPublicKey.fromBase58(k, network));
 
    return this;
  }
 
  static fromJSON(json, accountKey, keys) {
    return new this().fromJSON(json, accountKey, keys);
  }
 
  static fromAccount(account) {
    return new this().fromAccount(account);
  }
}
 
class WalletDetails extends Struct {
  constructor() {
    super();
 
    this.watchOnly = true;
    this.tokenDepth = 0;
    this.master = new MasterKey();
    this.token = NULL_TOKEN;
    this.joinPubKey = NULL_KEY;
 
    // not used, for information.
    this.timestamp = now();
 
    // This will be recovered based on accounts array
    // and wont actually be initialized.
    // NOTE: in bmultisig this will always be 1.
    this.accountDepth = 1;
 
    this.accounts = [];
    this.cosigners = [];
  }
 
  fromWallet(wallet, accounts, cosigners) {
    this.master = cloneMasterKey(wallet.master);
    this.accounts = accounts;
    this.cosigners = cosigners;
 
    wallet.joinPubKey.copy(this.joinPubKey, 0);
 
    return this;
  }
 
  getSize() {
    let size = 0;
 
    size += 1; // flags
    size += 4; // accountDepth
    size += 32; // token
    size += 4; // token depth
    size += this.master.getSize();
 
    size += 5; // timestamp
    size += 33; // joinPubKey
 
    size += 4; // accounts length
 
    for (const account of this.accounts)
      size += account.getSize();
 
    size += 1; // cosigners length
    for (const cosigner of this.cosigners)
      size += cosigner.getSize();
 
    return size;
  }
 
  write(bw, network) {
    let flags = 0;
 
    if (this.watchOnly)
      flags |= 1;
 
    bw.writeU8(flags);
    bw.writeU32(this.accountDepth);
    bw.writeBytes(this.token);
    bw.writeU32(this.tokenDepth);
    this.master.toWriter(bw);
 
    bw.writeU40(this.timestamp);
    bw.writeBytes(this.joinPubKey);
 
    bw.writeU32(this.accounts.length);
    for (const account of this.accounts)
      account.toWriter(bw);
 
    bw.writeU8(this.cosigners.length);
    for (const cosigner of this.cosigners)
      cosigner.toWriter(bw, network);
 
    return bw;
  }
 
  read(br, network) {
    const flags = br.readU8();
 
    this.watchOnly = (flags & 1) !== 0;
    this.accountDepth = br.readU32();
    this.token = br.readBytes(32);
    this.tokenDepth = br.readU32();
    this.master.fromReader(br);
 
    this.timestamp = br.readU40();
    this.joinPubKey = br.readBytes(33);
 
    const accounts = br.readU32();
 
    for (let i = 0; i < accounts; i++) {
      const account = new AccountDetails();
 
      account.fromReader(br);
      this.accounts.push(account);
    }
 
    const cosigners = br.readU8();
 
    for (let i = 0; i < cosigners; i++) {
      const cosigner = new CosignerDetails();
 
      cosigner.fromReader(br, network);
      this.cosigners.push(cosigner);
    }
 
    return this;
  }
 
  getJSON(network) {
    return {
      watchOnly: this.watchOnly,
      accountDepth: this.accountDepth,
      tokenDepth: this.tokenDepth,
      token: this.token.toString('hex'),
      master: this.master.toRaw().toString('hex'),
      joinPubKey: this.joinPubKey.toString('hex'),
      timestamp: this.timestamp,
      accounts: this.accounts.map(a => a.getJSON(network)),
      cosigners: this.cosigners.map(c => c.getJSON(network))
    };
  }
 
  fromJSON(json, network) {
    assert(json);
    assert((json.tokenDepth >>> 0) === json.tokenDepth);
    assert(typeof json.token === 'string');
    assert(typeof json.joinPubKey === 'string');
    assert(typeof json.timestamp === 'number');
    assert(json.timestamp >= 0);
    assert(Array.isArray(json.accounts));
 
    const token = Buffer.from(json.token, 'hex');
    const joinPubKey = Buffer.from(json.joinPubKey, 'hex');
    const rawMasterKey = Buffer.from(json.master, 'hex');
 
    assert(token.length === 32);
    assert(joinPubKey.length === 33);
 
    this.token = token;
    this.joinPubKey = joinPubKey;
    this.timestamp = json.timestamp;
    this.accountDepth = json.accounts.length;
    this.master = MasterKey.fromRaw(rawMasterKey);
 
    assert(json.accounts.length === 1);
 
    for (const cosignerJSON of json.cosigners) {
      const cosignerDetails = CosignerDetails.fromJSON(cosignerJSON, network);
      this.cosigners.push(cosignerDetails);
    }
 
    const accountDetails = AccountDetails.fromJSON(json.accounts[0], network);
    this.accounts.push(accountDetails);
 
    return this;
  }
 
  static fromWallet(wallet, accounts, cosigners) {
    return new this().fromWallet(wallet, accounts, cosigners);
  }
}
 
/*
 * helpers
 */
 
function cloneMasterKey(key) {
  return MasterKey.fromRaw(key.toRaw());
}
 
function now() {
  return Math.floor(Date.now() / 1000);
}
 
function writeKey(key, bw) {
  bw.writeU8(key.depth);
  bw.writeU32BE(key.parentFingerPrint);
  bw.writeU32BE(key.childIndex);
  bw.writeBytes(key.chainCode);
  bw.writeBytes(key.publicKey);
}
 
function readKey(br) {
  const key = new HDPublicKey();
  key.depth = br.readU8();
  key.parentFingerPrint = br.readU32BE();
  key.childIndex = br.readU32BE();
  key.chainCode = br.readBytes(32);
  key.publicKey = br.readBytes(33);
  return key;
}
 
exports.WalletDetails = WalletDetails;
exports.AccountDetails = AccountDetails;
exports.CosignerDetails = CosignerDetails;