Skip to content

Commit df2bd42

Browse files
committed
Renamed wallet to address everywhere
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent 22e5e92 commit df2bd42

File tree

14 files changed

+381
-263
lines changed

14 files changed

+381
-263
lines changed

.github/workflows/build-lint-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
- name: Install dependencies
2525
run: npm ci
2626

27+
- name: Push Prisma schema to DB
28+
run: npm run db:push
29+
2730
- name: Build (TypeScript)
2831
run: npm run build
2932

package-lock.json

Lines changed: 113 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"scripts": {
88
"build": "tsc",
99
"test": "jest --runInBand",
10-
"lint": "eslint . --ext .ts"
10+
"lint": "eslint . --ext .ts",
11+
"db:push": "prisma db push"
1112
},
1213
"keywords": [],
1314
"author": "",
@@ -31,13 +32,15 @@
3132
"dependencies": {
3233
"@dignetwork/datalayer-driver": "^0.1.35",
3334
"@dignetwork/proof-of-storage-continuity": "^0.1.0",
35+
"@prisma/client": "^6.11.1",
3436
"async-mutex": "^0.5.0",
3537
"better-sqlite3": "^11.10.0",
3638
"bip39": "^3.1.0",
3739
"chia-bls": "^1.0.3",
3840
"fs-extra": "^11.3.0",
3941
"nconf": "^0.13.0",
4042
"observable-fns": "^0.6.1",
43+
"prisma": "^6.11.1",
4144
"threads": "^1.7.0",
4245
"uuid": "^11.1.0",
4346
"zod": "^3.25.55"

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ model Block {
1717
created_at DateTime @default(now())
1818
}
1919

20-
model Wallet {
20+
model Address {
2121
address String @id
2222
namespace String @default("default")
2323
synced_to_height Int
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { PrismaClient } from '@prisma/client';
2+
3+
const prisma = new PrismaClient();
4+
5+
export interface AddressRow {
6+
address: string;
7+
namespace: string;
8+
synced_to_height: number;
9+
synced_to_hash: string;
10+
name?: string;
11+
}
12+
13+
export interface IAddressRepository {
14+
addAddress(address: string, name: string, namespace?: string): Promise<void>;
15+
updateAddressSync(address: string, synced_to_height: number, synced_to_hash: string): Promise<void>;
16+
removeAddress(address: string): Promise<void>;
17+
removeAddressByName(name: string): Promise<void>;
18+
getAddresses(): Promise<AddressRow[]>;
19+
}
20+
21+
export class AddressRepository implements IAddressRepository {
22+
async addAddress(address: string, name: string, namespace: string = 'default', synchedToHeight: number = 0, synchedToHash: string = '') {
23+
// Prevent duplicate names
24+
const exists = await prisma.address.findUnique({ where: { name } });
25+
if (exists) throw new Error('Address with this name already exists');
26+
await prisma.address.create({
27+
data: {
28+
address,
29+
namespace,
30+
name,
31+
synced_to_height: synchedToHeight,
32+
synced_to_hash: synchedToHash,
33+
},
34+
});
35+
}
36+
37+
async updateAddressSync(address: string, synced_to_height: number, synced_to_hash: string) {
38+
await prisma.address.update({
39+
where: { address },
40+
data: { synced_to_height, synced_to_hash },
41+
});
42+
}
43+
44+
async removeAddress(address: string) {
45+
await prisma.address.deleteMany({ where: { address } });
46+
}
47+
48+
async removeAddressByName(name: string) {
49+
await prisma.address.deleteMany({ where: { name } });
50+
}
51+
52+
async getAddresses(): Promise<AddressRow[]> {
53+
return prisma.address.findMany();
54+
}
55+
}

src/application/repositories/BlockRepository.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ export class BlockRepository implements IBlockRepository {
2929
return toBlock(block);
3030
}
3131

32-
// Test-only helper for inserting blocks
3332
async addBlock(hash: Buffer, blockHeight: number) {
34-
// @ts-ignore
3533
return await prisma.block.create({ data: { hash, blockHeight } });
3634
}
3735
}

src/application/repositories/Interfaces/IWalletRepository.ts

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

src/application/repositories/WalletRepository.ts

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

src/application/services/WalletService.ts renamed to src/application/services/AddressService.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { EncryptedData } from '../types/EncryptedData';
55
import { Wallet } from '../types/Wallet';
66
import type { IBlockchainService } from '../../infrastructure/BlockchainServices/IBlockChainService';
77
import { ChiaBlockchainService } from '../../infrastructure/BlockchainServices/ChiaBlockchainService';
8-
import { AddressRow, WalletRepository } from '../repositories/WalletRepository';
8+
import { AddressRow, AddressRepository } from '../repositories/AddressRepository';
99

1010
const KEYRING_FILE = 'keyring.json';
1111

12-
export class WalletService {
12+
export class AddressService {
1313
private blockchain: IBlockchainService;
14-
private static walletRepo: WalletRepository = new WalletRepository();
14+
private static addressRepo: AddressRepository = new AddressRepository();
1515

1616
constructor() {
1717
this.blockchain = new ChiaBlockchainService();
@@ -29,14 +29,14 @@ export class WalletService {
2929
}
3030

3131
public static async createAddress(addressName: string, mnemonic?: string): Promise<Wallet> {
32-
if (await this.walletExists(addressName)) {
32+
if (await this.addressExists(addressName)) {
3333
throw new Error('Address with the same name already exists.');
3434
}
3535
const generatedMnemonic = bip39.generateMnemonic(256);
3636
await this.saveAddressToKeyring(addressName, mnemonic ?? generatedMnemonic);
3737
let wallet = await this.loadAddress(addressName);
3838
const address = await wallet.getOwnerPublicKey();
39-
await WalletService.walletRepo.addAddress(address, addressName);
39+
await AddressService.addressRepo.addAddress(address, addressName);
4040

4141
return wallet;
4242
}
@@ -48,12 +48,12 @@ export class WalletService {
4848
deleted = await nconfService.deleteConfigValue(addressName);
4949
}
5050
// Remove from DB as well
51-
await WalletService.walletRepo.removeAddressByName(addressName);
51+
await AddressService.addressRepo.removeAddressByName(addressName);
5252
return deleted;
5353
}
5454

5555
public static async getAddresses(): Promise<AddressRow[]> {
56-
return WalletService.walletRepo.getAddresses();
56+
return AddressService.addressRepo.getAddresses();
5757
}
5858

5959
public async calculateFeeForCoinSpends(): Promise<bigint> {
@@ -77,7 +77,7 @@ export class WalletService {
7777
await nconfService.setConfigValue(walletName, encryptedData);
7878
}
7979

80-
private static async walletExists(addressName: string): Promise<boolean> {
80+
private static async addressExists(addressName: string): Promise<boolean> {
8181
const nconfService = new NconfService(KEYRING_FILE);
8282
if (await nconfService.configExists()) {
8383
const existing = await nconfService.getConfigValue(addressName);

0 commit comments

Comments
 (0)