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

JCCDex / jcc_wallet / 20839478914

09 Jan 2026 02:48AM UTC coverage: 97.253% (-0.007%) from 97.26%
20839478914

Pull #97

github

web-flow
Merge 07fed5249 into 8bb949f3f
Pull Request #97: unify to return compressed public key

258 of 264 branches covered (97.73%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

521 of 537 relevant lines covered (97.02%)

149.29 hits per line

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

97.67
/src/hd/tron.plugin.ts
1
import { IHDPlugin, IKeyPair } from "../types";
2
import {
3
  pkToAddress,
4
  computeAddress,
5
  getPubKeyFromPriKey,
6
  isAddressValid,
7
  getBase58CheckAddress
8
} from "../minify-tron/crypto";
9
import { hexStr2byteArray } from "../minify-tron/code";
10
import { hashMessage, signMessage, verifyMessage } from "../minify-tron/message";
11
import { secp256k1 } from "@noble/curves/secp256k1.js";
12

13
export interface ITronPlugin extends IHDPlugin {
14
  checkPrivateKey(privateKey: string): string;
15
  getKeyPairFromPrivateKey(privateKey: string): IKeyPair | null;
16
}
17

18
export const plugin: ITronPlugin = {
18✔
19
  checkPrivateKey(privateKey: string): string {
20
    // check and cut swtc keypair lib add prefix 00
21
    return privateKey.length === 66 ? privateKey.substring(2) : privateKey;
20✔
22
  },
23
  address(key: IKeyPair): string {
24
    if (key.privateKey) {
10✔
25
      const privateKey = plugin.checkPrivateKey(key.privateKey);
6✔
26
      const wallet = pkToAddress(privateKey);
6✔
27
      return wallet as string;
6✔
28
    }
29
    if (key.publicKey) {
4✔
30
      // TODO: length of ethereum publick key of keypaire is 128, but swtc lib keypair is 64
31
      // so, if you want get address from public key, get it from private first
32
      const pubBytes = hexStr2byteArray(key.publicKey);
2✔
33
      const comCddressBytes = computeAddress(pubBytes);
2✔
34
      return getBase58CheckAddress(comCddressBytes) as string;
2✔
35
    }
36
    return null;
2✔
37
  },
38

39
  isValidAddress(address: string): boolean {
40
    return isAddressValid(address);
8✔
41
  },
42

43
  isValidSecret(secret: string): boolean {
44
    try {
8✔
45
      const comPriKeyBytes = hexStr2byteArray(plugin.checkPrivateKey(secret));
8✔
46
      const pubBytes = getPubKeyFromPriKey(comPriKeyBytes);
4✔
47
      const comCddressBytes = computeAddress(pubBytes);
4✔
48
      const address = getBase58CheckAddress(comCddressBytes);
4✔
49
      return plugin.isValidAddress(address) as boolean;
4✔
50
    } catch {
51
      return false;
4✔
52
    }
53
  },
54
  hash(message: string): string {
55
    return hashMessage(message) as string;
2✔
56
  },
57
  /**
58
   *
59
   * @param message message content, let message is "\x19Ethereum Signed Message:\n" + message.length + message, match web3.accounts.sign function
60
   * @param privateKey private key
61
   * @returns signature string
62
   */
63
  sign(message: string, privateKey: string): string {
64
    const key = plugin.checkPrivateKey(privateKey).toLowerCase();
2✔
65

66
    return signMessage(message, key) as string;
2✔
67
  },
68
  verify(message: string, signature: string, address: string): boolean {
69
    return plugin.recover(message, signature) === address;
8✔
70
  },
71
  recover(message: string, signature: string): string {
72
    return verifyMessage(message, signature);
10✔
73
  },
74
  getKeyPairFromPrivateKey(privateKey: string): IKeyPair | null {
75
    try {
4✔
76
      const key = plugin.checkPrivateKey(privateKey);
4✔
77
      if (!plugin.isValidSecret(key)) {
4✔
78
        return null;
2✔
79
      }
80
      const publicKey = secp256k1.ProjectivePoint.fromPrivateKey(Buffer.from(key, "hex")).toHex(true);
2✔
81
      return {
2✔
82
        privateKey: key,
83
        publicKey
84
      };
85
    } catch {
UNCOV
86
      return null;
×
87
    }
88
  }
89
};
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