All files / src/transactions signer.ts

81.35% Statements 48/59
59.09% Branches 13/22
90% Functions 9/10
82.75% Lines 48/58

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      10x   10x 10x 10x   10x               39x 39x 39x 39x 39x       39x 39x 9x   4x     1x     8x 1x 1x 1x               1x                   5x     5x 5x 5x 5x 5x 5x 5x 5x 5x       41x     41x     41x     41x 16x 16x     11x     1x       40x       5x     5x 5x     5x       7x   7x     7x 7x       1x                
import { StacksTransaction } from './transaction';
 
import { StacksPrivateKey, StacksPublicKey } from './keys';
import { isSingleSig, nextVerification, SpendingConditionOpts } from './authorization';
 
import { AuthType, PubKeyEncoding } from './common/constants';
import { cloneDeep, SigningError } from 'micro-stacks/common';
import { StacksMessageType } from 'micro-stacks/clarity';
 
export class TransactionSigner {
  transaction: StacksTransaction;
  sigHash: string;
  originDone: boolean;
  checkOversign: boolean;
  checkOverlap: boolean;
 
  constructor(transaction: StacksTransaction) {
    this.transaction = transaction;
    this.sigHash = transaction.signBegin();
    this.originDone = false;
    this.checkOversign = true;
    this.checkOverlap = true;
 
    // If multi-sig spending condition exists, iterate over
    // auth fields and reconstruct sigHash
    const spendingCondition = transaction.auth.spendingCondition;
    if (spendingCondition && !isSingleSig(spendingCondition)) {
      if (
        spendingCondition.fields.filter(
          field => field.contents.type === StacksMessageType.MessageSignature
        ).length >= spendingCondition.signaturesRequired
      ) {
        throw new Error('SpendingCondition has more signatures than are expected');
      }
 
      spendingCondition.fields.forEach(field => {
        if (field.contents.type === StacksMessageType.MessageSignature) {
          const signature = field.contents;
          const nextVerify = nextVerification(
            this.sigHash,
            transaction.auth.authType,
            spendingCondition.fee,
            spendingCondition.nonce,
            PubKeyEncoding.Compressed, // always compressed for multisig
            signature
          );
          this.sigHash = nextVerify.nextSigHash;
        }
      });
    }
  }
 
  static createSponsorSigner(
    transaction: StacksTransaction,
    spendingCondition: SpendingConditionOpts
  ) {
    Iif (transaction.auth.authType != AuthType.Sponsored) {
      throw new SigningError('Cannot add sponsor to non-sponsored transaction');
    }
    const tx: StacksTransaction = cloneDeep(transaction);
    tx.setSponsor(spendingCondition);
    const originSigHash = tx.verifyOrigin();
    const signer = new this(tx);
    signer.originDone = true;
    signer.sigHash = originSigHash;
    signer.checkOversign = true;
    signer.checkOverlap = true;
    return signer;
  }
 
  async signOrigin(privateKey: StacksPrivateKey) {
    Iif (this.checkOverlap && this.originDone)
      throw new SigningError('Cannot sign origin after sponsor key');
 
    Iif (this.transaction.auth === undefined)
      throw new SigningError('"transaction.auth" is undefined');
 
    Iif (this.transaction.auth.spendingCondition === undefined)
      throw new SigningError('"transaction.auth.spendingCondition" is undefined');
 
    if (!isSingleSig(this.transaction.auth.spendingCondition)) {
      const spendingCondition = this.transaction.auth.spendingCondition;
      if (
        this.checkOversign &&
        spendingCondition.fields.filter(
          field => field.contents.type === StacksMessageType.MessageSignature
        ).length >= spendingCondition.signaturesRequired
      ) {
        throw new Error('Origin would have too many signatures');
      }
    }
 
    this.sigHash = await this.transaction.signNextOrigin(this.sigHash, privateKey);
  }
 
  appendOrigin(publicKey: StacksPublicKey) {
    Iif (this.checkOverlap && this.originDone)
      throw Error('Cannot append public key to origin after sponsor key');
 
    Iif (this.transaction.auth === undefined) throw new Error('"transaction.auth" is undefined');
    Iif (this.transaction.auth.spendingCondition === undefined)
      throw new Error('"transaction.auth.spendingCondition" is undefined');
 
    this.transaction.appendPubkey(publicKey);
  }
 
  async signSponsor(privateKey: StacksPrivateKey) {
    Iif (this.transaction.auth === undefined)
      throw new SigningError('"transaction.auth" is undefined');
    Iif (this.transaction.auth.authType !== AuthType.Sponsored)
      throw new SigningError('"transaction.auth.authType" is not AuthType.Sponsored');
 
    this.sigHash = await this.transaction.signNextSponsor(this.sigHash, privateKey);
    this.originDone = true;
  }
 
  getTxInComplete(): StacksTransaction {
    return cloneDeep(this.transaction);
  }
 
  resume(transaction: StacksTransaction) {
    this.transaction = cloneDeep(transaction);
    this.sigHash = transaction.signBegin();
  }
}