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

hyperledger-identus / sdk-ts / 14056150531

25 Mar 2025 09:30AM UTC coverage: 75.378% (-0.2%) from 75.595%
14056150531

Pull #402

github

web-flow
Merge cc184c4a1 into d265d5a35
Pull Request #402: fix: update signature encoding to follow JWS specification

1444 of 2108 branches covered (68.5%)

Branch coverage included in aggregate %.

19 of 38 new or added lines in 2 files covered. (50.0%)

3445 of 4378 relevant lines covered (78.69%)

53.78 hits per line

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

48.15
/src/pollux/utils/jwt/DER.ts
1
/**
2
 * Fix around normalising DER signatures into their raw representation
3
 * @param derSignature Uint8Array
4
 * @returns Uint8Array
5
 */
6
export function normaliseDER(derSignature: Uint8Array): Uint8Array {
7
    // Ensure the DER signature starts with the correct sequence header
8
    if (derSignature[0] !== 0x30) {
27!
NEW
9
        return derSignature;
×
10
    }
11
    // Get the length of the sequence
12
    let seqLength = derSignature[1];
27✔
13
    let offset = 2;
27✔
14
    if (seqLength & 0x80) {
27!
NEW
15
        const lengthBytes = seqLength & 0x7f;
×
NEW
16
        seqLength = 0;
×
NEW
17
        for (let i = 0; i < lengthBytes; i++) {
×
NEW
18
            seqLength = (seqLength << 8) | derSignature[offset++];
×
19
        }
20
    }
21

22
    if (derSignature[offset++] !== 0x02) {
27!
NEW
23
        throw new Error('Invalid DER signature: expected integer for r');
×
24
    }
25

26
    const rLength = derSignature[offset++];
27✔
27
    let r = Buffer.from(derSignature.slice(offset, offset + rLength));
27✔
28
    offset += rLength;
27✔
29

30
    // Extract s value
31
    if (derSignature[offset++] !== 0x02) {
27!
NEW
32
        throw new Error('Invalid DER signature: expected integer for s');
×
33
    }
34
    const sLength = derSignature[offset++];
27✔
35
    let s = Buffer.from(derSignature.slice(offset, offset + sLength));
27✔
36

37
    // Normalize r and s to 32 bytes
38
    if (r.length > 32) {
27✔
39
        r = r.slice(-32); // truncate if r is longer than 32 bytes
12✔
40
    } else if (r.length < 32) {
15!
NEW
41
        const paddedR = Uint8Array.from(Buffer.alloc(32));
×
NEW
42
        r.copy(paddedR, 32 - r.length);
×
NEW
43
        r = Buffer.from(paddedR); // left pad with zeros if r is shorter than 32 bytes
×
44
    }
45

46
    if (s.length > 32) {
27!
NEW
47
        s = s.slice(-32); // truncate if s is longer than 32 bytes
×
48
    } else if (s.length < 32) {
27!
NEW
49
        const paddedS = Uint8Array.from(Buffer.alloc(32));
×
NEW
50
        s.copy(paddedS, 32 - s.length);
×
NEW
51
        s = Buffer.from(paddedS); // left pad with zeros if s is shorter than 32 bytes
×
52
    }
53

54
    // Concatenate r and s to form the raw signature
55
    return Uint8Array.from([...r, ...s]);
27✔
56
}
57
/**
58
 * Remove leading zeros from a buffer
59
 * @param buffer Buffer
60
 * @returns Buffer
61
 */
62
function removeLeadingZeros(buffer: Buffer): Buffer {
NEW
63
    const arr = Array.from(buffer)
×
NEW
64
    let i = 0;
×
NEW
65
    while (i < arr.length - 1 && arr[i] === 0) {
×
NEW
66
        i++;
×
67
    }
NEW
68
    return Buffer.from(arr.slice(i));
×
69
}
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

© 2025 Coveralls, Inc