All files index.js

20.3% Statements 27/133
11.43% Branches 8/70
12.9% Functions 4/31
20.45% Lines 27/132

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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537                                            1x 6x   6x   6x   6x     6x     6x     6x                                                                                                                           6x 6x 6x 6x 6x   6x 6x 6x                                                                               6x   6x     6x 6x 6x   6x 6x   6x 6x   6x                     6x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
import bsv from 'bsv';
import Message from 'bsv/message';
import ECIES from 'bsv/ecies';
import 'node-fetch';
 
import { Utils } from './utils';
import { BAP_ID } from './id';
import {
  ENCRYPTION_PATH,
  BAP_SERVER,
  BAP_BITCOM_ADDRESS,
  BAP_BITCOM_ADDRESS_HEX,
  AIP_BITCOM_ADDRESS,
} from './constants';
 
/**
 * BAP class
 *
 * Creates an instance of the BAP class and uses the given HDPrivateKey for all BAP operations.
 *
 * @param HDPrivateKey
 */
export const BAP = class {
  #HDPrivateKey = null;
 
  #ids = {};
 
  #BAP_SERVER = BAP_SERVER;
 
  #BAP_TOKEN = '';
 
  constructor(HDPrivateKey, token = false) {
    Iif (!HDPrivateKey) {
      throw new Error('No HDPrivateKey given');
    } else {
      this.#HDPrivateKey = bsv.HDPrivateKey(HDPrivateKey);
    }
 
    Iif (token) {
      this.#BAP_TOKEN = token;
    }
  }
 
  /**
   * Get the public key of the given childPath, or of the current HDPrivateKey of childPath is empty
   *
   * @param childPath Full derivation path for this child
   * @returns {*}
   */
  getPublicKey(childPath = false) {
    if (childPath) {
      return this.#HDPrivateKey.deriveChild(childPath).publicKey.toString();
    }
 
    return this.#HDPrivateKey.publicKey.toString();
  }
 
  /**
   * Get the public key of the given childPath, or of the current HDPrivateKey of childPath is empty
   *
   * @param childPath Full derivation path for this child
   * @returns {*}
   */
  getHdPublicKey(childPath = false) {
    if (childPath) {
      return this.#HDPrivateKey.deriveChild(childPath).hdPublicKey.toString();
    }
 
    return this.#HDPrivateKey.hdPublicKey.toString();
  }
 
  set BAP_SERVER(bapServer) {
    this.#BAP_SERVER = bapServer;
    Object.keys(this.#ids).forEach((key) => {
      this.#ids[key].BAP_SERVER = bapServer;
    });
  }
 
  get BAP_SERVER() {
    return this.#BAP_SERVER;
  }
 
  set BAP_TOKEN(token) {
    this.#BAP_TOKEN = token;
    Object.keys(this.#ids).forEach((key) => {
      this.#ids[key].BAP_TOKEN = token;
    });
  }
 
  get BAP_TOKEN() {
    return this.#BAP_TOKEN;
  }
 
  /**
   * Increment that last part of the given path
   *
   * @param path
   * @returns {*}
   */
  incrementPath(path) {
    const pathValues = path.split('/');
    const lastPart = pathValues[pathValues.length - 1];
    let hardened = false;
    Eif (lastPart.match('\'')) {
      hardened = true;
    }
    const nextPath = (Number(lastPart.replace(/[^0-9]/g, '')) + 1).toString();
    pathValues[pathValues.length - 1] = nextPath + (hardened ? '\'' : '');
    return pathValues.join('/');
  }
 
  /**
   * This function verifies that the given bapId matches the given root address
   * This is used as a data integrity check
   *
   * @param bapId BAP_ID instance
   */
  checkIdBelongs(bapId) {
    const derivedChild = this.#HDPrivateKey.deriveChild(bapId.rootPath);
    const checkRootAddress = derivedChild.publicKey.toAddress().toString();
    if (checkRootAddress !== bapId.rootAddress) {
      throw new Error('ID does not belong to this private key');
    }
 
    return true;
  }
 
  /**
   * Returns a list of all the identity keys that are stored in this instance
   *
   * @returns {string[]}
   */
  listIds() {
    return Object.keys(this.#ids);
  }
 
  /**
   * Create a new Id and link it to this BAP instance
   *
   * This function uses the length of the #ids of this class to determine the next valid path.
   * If not all ids related to this HDPrivateKey have been loaded, determine the path externally
   * and pass it to newId when creating a new ID.
   *
   * @param path
   * @param identityAttributes
   * @returns {*}
   */
  newId(path = null, identityAttributes = {}) {
    Eif (!path) {
      // get next usable path for this key
      path = this.getNextValidPath();
    }
 
    const newIdentity = new BAP_ID(this.#HDPrivateKey, identityAttributes);
    newIdentity.BAP_SERVER = this.#BAP_SERVER;
    newIdentity.BAP_TOKEN = this.#BAP_TOKEN;
 
    newIdentity.rootPath = path;
    newIdentity.currentPath = this.incrementPath(path);
 
    const idKey = newIdentity.getIdentityKey();
    this.#ids[idKey] = newIdentity;
 
    return this.#ids[idKey];
  }
 
  /**
   * Get the next valid path for the used HDPrivateKey and loaded #ids
   *
   * @returns {string}
   */
  getNextValidPath() {
    // prefer hardened paths
    // TODO check whether this is taken
    return `/0'/${Object.keys(this.#ids).length}'/0'`;
  }
 
  /**
   * Get a certain Id
   *
   * @param identityKey
   * @returns {null}
   */
  getId(identityKey) {
    return this.#ids[identityKey] || null;
  }
 
  /**
   * This function is used when manipulating ID's, adding or removing attributes etc
   * First create an id through this class and then use getId to get it. Then you can add/edit or
   * increment the signing path and then re-set it with this function.
   *
   * Note: when you getId() from this class, you will be working on the same object as this class
   * has and any changes made will be propagated to the id in this class. When you call exportIds
   * your new changes will also be included, without having to setId().
   *
   * @param bapId
   */
  setId(bapId) {
    if (bapId instanceof BAP_ID) {
      this.checkIdBelongs(bapId);
 
      this.#ids[bapId.getIdentityKey()] = bapId;
    } else {
      throw new Error('id is not an instance of BAP_ID');
    }
  }
 
  /**
   * This function is used to import IDs and attributes from some external storage
   *
   * The ID information should NOT be stored together with the HD private key !
   *
   * @param ids Array of ids that have been exported
   * @param encrypted Whether the data should be treated as being encrypted (default true)
   */
  importIds(ids, encrypted = true) {
    if (encrypted) {
      // we first need to decrypt the ids array using ECIES
      const ecies = ECIES();
      const derivedChild = this.#HDPrivateKey.deriveChild(ENCRYPTION_PATH);
      ecies.privateKey(derivedChild.privateKey);
      const decrypted = ecies.decrypt(Buffer.from(ids, Utils.isHex(ids) ? 'hex' : 'base64')).toString();
      ids = JSON.parse(decrypted);
    }
 
    ids.forEach((id) => {
      if (!id.identityKey || !id.identityAttributes || !id.rootAddress) {
        throw new Error('ID cannot be imported as it is not complete');
      }
      const importId = new BAP_ID(this.#HDPrivateKey);
      importId.BAP_SERVER = this.#BAP_SERVER;
      importId.BAP_TOKEN = this.#BAP_TOKEN;
      importId.import(id);
 
      this.checkIdBelongs(importId);
 
      this.#ids[importId.getIdentityKey()] = importId;
    });
  }
 
  /**
   * Export all the IDs of this instance for external storage
   *
   * By default this function will encrypt the data, using a derivative child of the main HD key
   *
   * @param encrypted Whether the data should be encrypted (default true)
   * @returns {[]|*}
   */
  exportIds(encrypted = true) {
    const ids = [];
 
    Object.keys(this.#ids)
      .forEach((key) => {
        ids.push(this.#ids[key].export());
      });
 
    if (encrypted) {
      const ecies = ECIES();
      const derivedChild = this.#HDPrivateKey.deriveChild(ENCRYPTION_PATH);
      ecies.publicKey(derivedChild.publicKey);
      return ecies.encrypt(JSON.stringify(ids)).toString('base64');
    }
 
    return ids;
  }
 
  /**
   * Sign an attestation for a user
   *
   * @param attestationHash The computed attestation hash for the user - this should be calculated with the BAP_ID class for an identity for the user
   * @param identityKey The identity key we are using for the signing
   * @param counter
   * @param dataString Optional data string that will be appended to the BAP attestation
   * @returns {string[]}
   */
  signAttestationWithAIP(attestationHash, identityKey, counter = 0, dataString = '') {
    const id = this.getId(identityKey);
    if (!id || !(id instanceof BAP_ID)) {
      throw new Error('Could not find identity to attest with');
    }
 
    const attestationBuffer = this.getAttestationBuffer(attestationHash, counter, dataString);
    const { address, signature } = id.signMessage(attestationBuffer);
 
    return this.createAttestationTransaction(
      attestationHash,
      counter,
      address,
      signature,
      dataString,
    );
  }
 
  /**
   * Verify an AIP signed attestation for a user
   *
   * [
   *   '0x6a',
   *   '0x31424150537561506e66476e53424d33474c56397968785564596534764762644d54',
   *   '0x415454455354',
   *   '0x33656166366361396334313936356538353831366439336439643034333136393032376633396661623034386333633031333663343364663635376462383761',
   *   '0x30',
   *   '0x7c',
   *   '0x313550636948473232534e4c514a584d6f5355615756693757537163376843667661',
   *   '0x424954434f494e5f4543445341',
   *   '0x31477531796d52567a595557634638776f6f506a7a4a4c764d383550795a64655876',
   *   '0x20ef60c5555001ddb1039bb0f215e46571fcb39ee46f48b089d1c08b0304dbcb3366d8fdf8bafd82be24b5ac42dcd6a5e96c90705dd42e3ad918b1b47ac3ce6ac2'
   * ]
   *
   * @param tx Array of hex values for the OP_RETURN values
   * @returns {{}}
   */
  verifyAttestationWithAIP(tx) {
    if (
      !Array.isArray(tx)
      || tx[0] !== '0x6a'
      || tx[1] !== BAP_BITCOM_ADDRESS_HEX
    ) {
      throw new Error('Not a valid BAP transaction');
    }
 
    const dataOffset = tx[7] === '0x44415441' ? 5 : 0; // DATA
    const attestation = {
      type: Utils.hexDecode(tx[2]),
      hash: Utils.hexDecode(tx[3]),
      sequence: Utils.hexDecode(tx[4]),
      signingProtocol: Utils.hexDecode(tx[7 + dataOffset]),
      signingAddress: Utils.hexDecode(tx[8 + dataOffset]),
      signature: Utils.hexDecode(tx[9 + dataOffset], 'base64'),
    };
 
    if (dataOffset && tx[3] === tx[8]) {
      // valid data addition
      attestation.data = Utils.hexDecode(tx[9]);
    }
 
    try {
      const signatureBufferStatements = [];
      for (let i = 0; i < 6 + dataOffset; i++) {
        signatureBufferStatements.push(Buffer.from(tx[i].replace('0x', ''), 'hex'));
      }
      const attestationBuffer = Buffer.concat([
        ...signatureBufferStatements,
      ]);
      attestation.verified = this.verifySignature(
        attestationBuffer,
        attestation.signingAddress,
        attestation.signature,
      )
    } catch (e) {
      attestation.verified = false;
    }
 
    return attestation;
  }
 
  /**
   * For BAP attestations we use all fields for the attestation
   *
   * @param attestationHash
   * @param counter
   * @param address
   * @param signature
   * @param dataString Optional data string that will be appended to the BAP attestation
   * @returns {[string]}
   */
  createAttestationTransaction(attestationHash, counter, address, signature, dataString = '') {
    const transaction = ['0x6a', Utils.hexEncode(BAP_BITCOM_ADDRESS)];
    transaction.push(Utils.hexEncode('ATTEST'));
    transaction.push(Utils.hexEncode(attestationHash));
    transaction.push(Utils.hexEncode(`${counter}`));
    transaction.push('0x7c'); // |
    if (dataString && typeof dataString === 'string') {
      // data should be a string, either encrypted or stringified JSON if applicable
      transaction.push(Utils.hexEncode(BAP_BITCOM_ADDRESS));
      transaction.push(Utils.hexEncode('DATA'));
      transaction.push(Utils.hexEncode(attestationHash));
      transaction.push(Utils.hexEncode(dataString));
      transaction.push('0x7c'); // |
    }
    transaction.push(Utils.hexEncode(AIP_BITCOM_ADDRESS));
    transaction.push(Utils.hexEncode('BITCOIN_ECDSA'));
    transaction.push(Utils.hexEncode(address));
    transaction.push('0x' + Buffer.from(signature, 'base64').toString('hex'));
 
    return transaction;
  }
 
  /**
   * This is a re-creation of how the bitcoinfiles-sdk creates a hash to sign for AIP
   *
   * @param attestationHash
   * @param counter
   * @param dataString Optional data string
   * @returns {Buffer}
   */
  getAttestationBuffer(attestationHash, counter = 0, dataString = '') {
    // re-create how AIP creates the buffer to sign
    let dataStringBuffer = Buffer.from('');
    if (dataString) {
      dataStringBuffer = Buffer.concat([
        Buffer.from(BAP_BITCOM_ADDRESS),
        Buffer.from('DATA'),
        Buffer.from(attestationHash),
        Buffer.from(dataString),
        Buffer.from('7c', 'hex'),
      ]);
    }
    return Buffer.concat([
      Buffer.from('6a', 'hex'), // OP_RETURN
      Buffer.from(BAP_BITCOM_ADDRESS),
      Buffer.from('ATTEST'),
      Buffer.from(attestationHash),
      Buffer.from(`${counter}`),
      Buffer.from('7c', 'hex'),
      dataStringBuffer,
    ]);
  }
 
  /**
   * Verify that the identity challenge is signed by the address
   *
   * @param message Buffer or utf-8 string
   * @param address Bitcoin address of signee
   * @param signature Signature base64 string
   */
  verifySignature(message, address, signature) {
    // check the signature against the challenge
    const messageBuffer = Buffer.isBuffer(message) ? message : Buffer.from(message);
    return Message.verify(
      messageBuffer,
      address,
      signature,
    );
  }
 
  /**
   * Check whether the given transaction (BAP OP_RETURN) is valid, is signed and that the
   * identity signing is also valid at the time of signing
   *
   * @param idKey
   * @param address
   * @param challenge
   * @param signature
   * @returns {Promise<boolean|*>}
   */
  async verifyChallengeSignature(idKey, address, challenge, signature) {
    // first we test locally before sending to server
    if (this.verifySignature(challenge, address, signature)) {
      const result = await this.getApiData('/attestation/valid', {
        idKey,
        challenge,
        signature,
      });
    }
 
    return false;
  }
 
  /**
   * Check whether the given transaction (BAP OP_RETURN) is valid, is signed and that the
   * identity signing is also valid at the time of signing
   *
   * @param tx
   * @returns {Promise<boolean|*>}
   */
  async isValidAttestationTransaction(tx) {
    // first we test locally before sending to server
    if (this.verifyAttestationWithAIP(tx)) {
      return this.getApiData('/attestation/valid', {
        tx,
      });
    }
 
    return false;
  }
 
  /**
   * Get all signing keys for the given idKey
   *
   * @param address
   * @returns {Promise<*>}
   */
  async getIdentityFromAddress(address) {
    return this.getApiData('/identity/from-address', {
      address,
    });
  }
 
  /**
   * Get all signing keys for the given idKey
   *
   * @param idKey
   * @returns {Promise<*>}
   */
  async getIdentity(idKey) {
    return this.getApiData('/identity', {
      idKey,
    });
  }
 
  /**
   * Get all attestations for the given attestation hash
   *
   * @param attestationHash
   */
  async getAttestationsForHash(attestationHash) {
    // get all BAP ATTEST records for the given attestationHash
    return this.getApiData('/attestations', {
      hash: attestationHash,
    });
  }
 
  /**
   * Helper function to get attestation from a BAP API server
   *
   * @param apiUrl
   * @param apiData
   * @returns {Promise<any>}
   */
  async getApiData(apiUrl, apiData) {
    const url = `${this.#BAP_SERVER}${apiUrl}`;
    const response = await fetch(url, {
      method: 'post',
      headers: {
        'Content-type': 'application/json; charset=utf-8',
        token: this.#BAP_TOKEN,
        format: 'json',
      },
      body: JSON.stringify(apiData),
    });
 
    return response.json();
  }
};