Skip to content

Commit b61b822

Browse files
authored
feat: rewrite in ts (#41)
* Convert to typescript * Baseline * Use tsnode * Fix types * Add a few more tests * Add a couple more tests * Add a few more tests * Fix test * Fix the wallet * Fix equality tests * Fix export * Add test clean up * Attempt to add coverage * Add that ugly comment * Add a few more tests * General clean up * Add last chunk of tests * Redefined imports * Return the filter * Changed property name * Fill the remaining jsdocs * Extracted verifier interface for verification * Fix testnet prefix * PR changes * Fix mnemonic test
1 parent 717e4e8 commit b61b822

40 files changed

Lines changed: 2104 additions & 1348 deletions

.github/workflows/CI.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

.github/workflows/publish.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ jobs:
2424
registry-url: 'https://registry.npmjs.org'
2525
- name: Install dependencies
2626
run: npm ci
27-
- name: Run node tests
28-
run: npm run test:node
27+
- name: Build package
28+
run: npm run build
2929
- name: Install bare
3030
run: npm i -g bare
31-
- name: Run bare tests
32-
run: npm run test:bare
31+
- name: Run tests
32+
run: npm run test:ci
3333
- name: Publish to npm
34-
run: npm publish --access public
34+
run: npm publish --access public

.github/workflows/test.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: WALLET-CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
permissions:
17+
contents: read
18+
pull-requests: write
19+
20+
jobs:
21+
tests:
22+
runs-on: ${{ matrix.os }}
23+
24+
strategy:
25+
matrix:
26+
node-version: [ 20 ]
27+
os: [ ubuntu-latest, macos-latest, windows-latest ]
28+
29+
steps:
30+
- uses: actions/checkout@v4
31+
- name: Use Node.js ${{ matrix.node-version }}
32+
uses: actions/setup-node@v3
33+
with:
34+
node-version: ${{ matrix.node-version }}
35+
cache: 'npm'
36+
- name: Install dependencies
37+
run: npm ci
38+
- name: Build package
39+
run: npm run build
40+
- name: Install bare
41+
run: npm i -g bare
42+
- name: Run tests
43+
run: npm run test:ci
44+
45+
coverage:
46+
runs-on: ubuntu-latest
47+
48+
steps:
49+
- uses: actions/checkout@v4
50+
- name: Use Node.js 20
51+
uses: actions/setup-node@v3
52+
with:
53+
node-version: 20
54+
cache: 'npm'
55+
- name: Install dependencies
56+
run: npm ci
57+
- name: Run coverage
58+
run: npm run coverage
59+
- name: Read coverage totals
60+
id: coverage_totals
61+
run: node scripts/ci/export-coverage-totals.mjs
62+
- name: Add coverage summary to GitHub
63+
env:
64+
LINES_PCT: ${{ steps.coverage_totals.outputs.lines_pct }}
65+
LINES_COV: ${{ steps.coverage_totals.outputs.lines_cov }}
66+
LINES_TOTAL: ${{ steps.coverage_totals.outputs.lines_total }}
67+
STATEMENTS_PCT: ${{ steps.coverage_totals.outputs.statements_pct }}
68+
STATEMENTS_COV: ${{ steps.coverage_totals.outputs.statements_cov }}
69+
STATEMENTS_TOTAL: ${{ steps.coverage_totals.outputs.statements_total }}
70+
FUNCTIONS_PCT: ${{ steps.coverage_totals.outputs.functions_pct }}
71+
FUNCTIONS_COV: ${{ steps.coverage_totals.outputs.functions_cov }}
72+
FUNCTIONS_TOTAL: ${{ steps.coverage_totals.outputs.functions_total }}
73+
BRANCHES_PCT: ${{ steps.coverage_totals.outputs.branches_pct }}
74+
BRANCHES_COV: ${{ steps.coverage_totals.outputs.branches_cov }}
75+
BRANCHES_TOTAL: ${{ steps.coverage_totals.outputs.branches_total }}
76+
run: bash scripts/ci/write-coverage-summary.sh
77+
- name: Comment coverage on PR
78+
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false
79+
uses: actions/github-script@v7
80+
env:
81+
LINES_PCT: ${{ steps.coverage_totals.outputs.lines_pct }}
82+
LINES_COV: ${{ steps.coverage_totals.outputs.lines_cov }}
83+
LINES_TOTAL: ${{ steps.coverage_totals.outputs.lines_total }}
84+
STATEMENTS_PCT: ${{ steps.coverage_totals.outputs.statements_pct }}
85+
STATEMENTS_COV: ${{ steps.coverage_totals.outputs.statements_cov }}
86+
STATEMENTS_TOTAL: ${{ steps.coverage_totals.outputs.statements_total }}
87+
FUNCTIONS_PCT: ${{ steps.coverage_totals.outputs.functions_pct }}
88+
FUNCTIONS_COV: ${{ steps.coverage_totals.outputs.functions_cov }}
89+
FUNCTIONS_TOTAL: ${{ steps.coverage_totals.outputs.functions_total }}
90+
BRANCHES_PCT: ${{ steps.coverage_totals.outputs.branches_pct }}
91+
BRANCHES_COV: ${{ steps.coverage_totals.outputs.branches_cov }}
92+
BRANCHES_TOTAL: ${{ steps.coverage_totals.outputs.branches_total }}
93+
with:
94+
script: |
95+
const path = require('node:path');
96+
const commentCoverage = require(path.join(process.env.GITHUB_WORKSPACE, 'scripts/ci/comment-coverage.cjs'));
97+
await commentCoverage({ github, context });
98+
- name: Upload coverage artifact
99+
uses: actions/upload-artifact@v4
100+
with:
101+
name: coverage-report
102+
path: |
103+
coverage/index.html
104+
coverage/lcov.info
105+
coverage/lcov-report

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ web_modules/
4848

4949
# TypeScript cache
5050
*.tsbuildinfo
51+
.test-build
5152

5253
# Optional npm cache directory
5354
.npm
@@ -132,3 +133,4 @@ dist
132133

133134
# idea
134135
.idea
136+
.vscode

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Trac Wallet
22

3-
This project provides a `Wallet` class that can generate and store ED25519 keys, sign messages, and verify signatures. It also includes functionality to export keys to a JSON file.
3+
This project provides a `WalletProvider` class that can generate wallets that store ED25519 keys, sign messages, and verify signatures. It also includes functionality to export keys to an encrypted file.
44

55
## Installation
66

@@ -17,21 +17,19 @@ npm install
1717
You can create a new wallet with a randomly generated mnemonic phrase:
1818

1919
```javascript
20-
import { Wallet } from './index.js';
20+
import { WalletProvider } from 'trac-wallet';
2121

22-
const wallet = new Wallet();
23-
const mnemonic = wallet.generateMnemonic() // creates a randomly generated mnemonic phrase containing 12 words
24-
wallet.generateKeypair(mnemonic) // Generates a keypair using the provided mnemonic and stores it internally
22+
const provider = new WalletProvider({ networkPrefix: 'trac' });
23+
const wallet = await provider.generate('optional-seed');
2524

26-
console.log(mnemonic);
2725
console.log(wallet.publicKey.toString('hex')); // Prints the public key
2826
```
2927

3028
You can also create a wallet with a specific mnemonic phrase:
3129

3230
```javascript
3331
const mnemonic = 'session attitude weekend sign collect mobile return vacuum pool afraid wagon client';
34-
const wallet = new Wallet(mnemonic);
32+
const wallet = await provider.fromMnemonic({ mnemonic });
3533
console.log(wallet.publicKey.toString('hex')); // Prints the public key
3634
```
3735

@@ -42,7 +40,7 @@ You can sign a message with the wallet's secret key and verify the signature wit
4240
```javascript
4341
const message = 'Hello, world!';
4442
const signature = wallet.signMessage(message);
45-
const isValid = wallet.verifySignature(message, signature, wallet.publicKey);
43+
const isValid = wallet.verify(message, signature);
4644
console.log(isValid); // Prints true if the signature is valid
4745
```
4846

@@ -52,7 +50,7 @@ You can export the wallet's keys to a JSON file:
5250

5351
```javascript
5452
const filePath = './wallet.json';
55-
wallet.exportToFile(filePath);
53+
exportToFile(wallet, filePath);
5654
```
5755

5856
## Running Tests
@@ -61,4 +59,4 @@ To run the tests, use the following command:
6159

6260
```bash
6361
npm run test
64-
```
62+
```

constants.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)