• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

handshake-org / hsd / 11895505322

18 Nov 2024 03:21PM UTC coverage: 71.265% (+1.2%) from 70.033%
11895505322

Pull #888

github

web-flow
Merge a914b01ee into 1b331eedb
Pull Request #888: Wallet TX Count and time indexing

8052 of 13150 branches covered (61.23%)

Branch coverage included in aggregate %.

826 of 885 new or added lines in 16 files covered. (93.33%)

2063 existing lines in 60 files now uncovered.

25720 of 34239 relevant lines covered (75.12%)

34512.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.27
/lib/script/sigcache.js
1
/*!
2
 * sigcache.js - signature cache for hsd
3
 * Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
4
 * https://github.com/handshake-org/hsd
5
 */
6

7
'use strict';
8

9
const assert = require('bsert');
1✔
10
const {BufferMap} = require('buffer-map');
1✔
11
const secp256k1 = require('bcrypto/lib/secp256k1');
1✔
12

13
/** @typedef {import('../types').Hash} Hash */
14

15
/**
16
 * Signature cache.
17
 * @alias module:script.SigCache
18
 * @property {Number} size
19
 * @property {Hash[]} keys
20
 * @property {Object} valid
21
 */
22

23
class SigCache {
24
  /**
25
   * Create a signature cache.
26
   * @constructor
27
   * @param {Number} [size=10000]
28
   */
29

30
  constructor(size) {
UNCOV
31
    if (size == null)
×
32
      size = 10000;
×
33

34
    assert((size >>> 0) === size);
×
35

36
    this.size = size;
×
UNCOV
37
    this.keys = [];
×
UNCOV
38
    this.valid = new BufferMap();
×
39
  }
40

41
  /**
42
   * Resize the sigcache.
43
   * @param {Number} size
44
   */
45

46
  resize(size) {
47
    assert((size >>> 0) === size);
×
48

49
    this.size = size;
×
UNCOV
50
    this.keys.length = 0;
×
UNCOV
51
    this.valid.clear();
×
52
  }
53

54
  /**
55
   * Add item to the sigcache.
56
   * Potentially evict a random member.
57
   * @param {Hash} hash - Sig hash.
58
   * @param {Buffer} sig
59
   * @param {Buffer} key
60
   */
61

62
  add(hash, sig, key) {
UNCOV
63
    if (this.size === 0)
×
64
      return;
×
65

66
    this.valid.set(hash, new SigCacheEntry(sig, key));
×
67

68
    if (this.keys.length >= this.size) {
×
69
      const i = Math.floor(Math.random() * this.keys.length);
×
70
      const k = this.keys[i];
×
UNCOV
71
      this.valid.delete(k);
×
72
      this.keys[i] = hash;
×
73
    } else {
UNCOV
74
      this.keys.push(hash);
×
75
    }
76
  }
77

78
  /**
79
   * Test whether the sig exists.
80
   * @param {Hash} hash - Sig hash.
81
   * @param {Buffer} sig
82
   * @param {Buffer} key
83
   * @returns {Boolean}
84
   */
85

86
  has(hash, sig, key) {
87
    const entry = this.valid.get(hash);
×
88

UNCOV
89
    if (!entry)
×
90
      return false;
×
91

UNCOV
92
    return entry.equals(sig, key);
×
93
  }
94

95
  /**
96
   * Verify a signature, testing
97
   * it against the cache first.
98
   * @param {Hash} hash
99
   * @param {Buffer} sig
100
   * @param {Buffer} key
101
   * @returns {Boolean}
102
   */
103

104
  verify(hash, sig, key) {
UNCOV
105
    if (this.size === 0)
×
106
      return secp256k1.verify(hash, sig, key);
×
107

UNCOV
108
    if (this.has(hash, sig, key))
×
109
      return true;
×
110

111
    const result = secp256k1.verify(hash, sig, key);
×
112

UNCOV
113
    if (!result)
×
114
      return false;
×
115

116
    this.add(hash, sig, key);
×
117

UNCOV
118
    return true;
×
119
  }
120
}
121

122
/**
123
 * Signature Cache Entry
124
 * @ignore
125
 * @property {Buffer} sig
126
 * @property {Buffer} key
127
 */
128

129
class SigCacheEntry {
130
  /**
131
   * Create a cache entry.
132
   * @constructor
133
   * @param {Buffer} sig
134
   * @param {Buffer} key
135
   */
136

137
  constructor(sig, key) {
UNCOV
138
    this.sig = Buffer.from(sig);
×
UNCOV
139
    this.key = Buffer.from(key);
×
140
  }
141

142
  /**
143
   * Compare an entry to a sig and key.
144
   * @param {Buffer} sig
145
   * @param {Buffer} key
146
   * @returns {Boolean}
147
   */
148

149
  equals(sig, key) {
UNCOV
150
    return this.sig.equals(sig) && this.key.equals(key);
×
151
  }
152
}
153

154
/*
155
 * Expose
156
 */
157

158
module.exports = SigCache;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc