Skip to content

Commit 950505d

Browse files
authored
Merge pull request #6 from tgerboui/issue/3-block-creation-uses-invalid-link
Block creation uses invalid link
2 parents 8e1ee39 + 4386f87 commit 950505d

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ For more details about websocket, see [Websocket](#websocket).
169169
- [Expand](#expand)
170170
- [Key Service](#key-service)
171171
- [Generate seed](#generate-seed)
172-
- [Get secret key](#get-secret-key)
172+
- [Get private key](#get-private-key)
173173
- [Get public key](#get-public-key)
174174
- [Get address](#get-address)
175175
- [Node](#node)
@@ -736,6 +736,7 @@ You have currently 3 nano in your account.
736736
const wallet = nona.wallet(privateKey);
737737
const info = await wallet.info();
738738
const recipient = 'nano_1send...';
739+
const recipientPublicKey = KeyService.getPublicKey(recipient);
739740
740741
const sendBlock = await this.blocks.create({
741742
// Current account
@@ -749,7 +750,7 @@ const sendBlock = await this.blocks.create({
749750
// If the block is sending funds, set link to the public key of the destination account.
750751
// If it is receiving funds, set link to the hash of the block to receive.
751752
// If the block has no balance change but is updating representative only, set link to 0.
752-
link: recipient,
753+
link: recipientPublicKey,
753754
// Private key of the account
754755
key: privateKey,
755756
});
@@ -838,25 +839,25 @@ Generates a random seed.
838839
const seed = await KeyService.generateSeed();
839840
```
840841

841-
### Get secret key
842+
### Get private key
842843

843844
```typescript
844-
KeyService.getSecretKey(seed: string, index: number): string
845+
KeyService.getPrivateKey(seed: string, index: number): string
845846
```
846847

847-
Derive a secret key from a seed, given an index.
848+
Derive a private key from a seed, given an index.
848849

849850
```typescript
850-
const privateKey = KeyService.getSecretKey(seed, 0);
851+
const privateKey = KeyService.getPrivateKey(seed, 0);
851852
```
852853

853854
### Get public key
854855

855856
```typescript
856-
KeyService.getPublicKey(privateKey: string): string
857+
KeyService.getPublicKey(privateKeyOrAddress: string): string
857858
```
858859

859-
Derive a public key from a secret key.
860+
Derive a public key from a private key or an address.
860861

861862
```typescript
862863
const publicKey = KeyService.getPublicKey(privateKey);

lib/modules/key/key.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Key {
1414
generativeSeed = await KeyService.generateSeed();
1515
}
1616

17-
const privateKey = KeyService.getSecretKey(generativeSeed, 0);
17+
const privateKey = KeyService.getPrivateKey(generativeSeed, 0);
1818
const publicKey = KeyService.getPublicKey(privateKey);
1919
const address = KeyService.getAddress(publicKey);
2020

lib/modules/wallet/wallet.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,15 @@ export class Wallet extends Account {
138138
throw new NonaUserError('Insufficient balance');
139139
}
140140
const finalBalance = balance.minus(rawAmount);
141+
const receiverPublicKey = KeyService.getPublicKey(address);
141142

142143
// TODO: Maybe set create block in a function in this class
143144
const block = await this.blocks.create({
144145
account: this.address,
145146
previous: info.frontier,
146147
representative: info.representative,
147148
balance: finalBalance.toString(),
148-
link: address,
149+
link: receiverPublicKey,
149150
key: this.privateKey,
150151
});
151152

lib/services/hash/key-service.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ export class KeyService {
1111
}
1212

1313
/**
14-
* Derive a secret key from a seed, given an index.
14+
* Derive a private key from a seed, given an index.
1515
*
16-
* @param seed - The seed to generate the secret key from, in hexadecimal format
17-
* @param index - The index to generate the secret key from
18-
* @returns Secret key, in hexadecimal format
16+
* @param seed - The seed to generate the private key from, in hexadecimal format
17+
* @param index - The index to generate the private key from
18+
* @returns Private key, in hexadecimal format
1919
*/
20-
public static getSecretKey(seed: string, index: number): string {
20+
public static getPrivateKey(seed: string, index: number): string {
2121
return deriveSecretKey(seed, index);
2222
}
2323

2424
/**
25-
* Derive a public key from a secret key.
25+
* Derive a public key from a private key.
2626
*
27-
* @param privateKey - Private key to generate the public key from, in hexadecimal or address format
27+
* @param privateKeyOrAddress - Private key or address to derive the public key from, in hexadecimal or address format
2828
* @returns Public key, in hexadecimal format
2929
*/
30-
public static getPublicKey(privateKey: string): string {
31-
return derivePublicKey(privateKey);
30+
public static getPublicKey(privateKeyOrAddress: string): string {
31+
return derivePublicKey(privateKeyOrAddress);
3232
}
3333

3434
/**

test/unit/modules/key.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ describe('Key', () => {
1818
const publicKey = 'publicKey';
1919
const address = 'address';
2020

21-
keyServiceMock.getSecretKey.mockReturnValue(privateKey);
21+
keyServiceMock.getPrivateKey.mockReturnValue(privateKey);
2222
keyServiceMock.getPublicKey.mockReturnValue(publicKey);
2323
keyServiceMock.getAddress.mockReturnValue(address);
2424

2525
const result = await key.create(seed);
26-
expect(keyServiceMock.getSecretKey).toHaveBeenCalledWith(seed, 0);
26+
expect(keyServiceMock.getPrivateKey).toHaveBeenCalledWith(seed, 0);
2727
expect(keyServiceMock.getPublicKey).toHaveBeenCalledWith(privateKey);
2828
expect(keyServiceMock.getAddress).toHaveBeenCalledWith(publicKey);
2929
expect(result).toEqual({ privateKey, publicKey, address });
@@ -36,13 +36,13 @@ describe('Key', () => {
3636
const address = 'address';
3737

3838
keyServiceMock.generateSeed.mockResolvedValue(generatedSeed);
39-
keyServiceMock.getSecretKey.mockReturnValue(privateKey);
39+
keyServiceMock.getPrivateKey.mockReturnValue(privateKey);
4040
keyServiceMock.getPublicKey.mockReturnValue(publicKey);
4141
keyServiceMock.getAddress.mockReturnValue(address);
4242

4343
const result = await key.create();
4444
expect(keyServiceMock.generateSeed).toHaveBeenCalled();
45-
expect(keyServiceMock.getSecretKey).toHaveBeenCalledWith(generatedSeed, 0);
45+
expect(keyServiceMock.getPrivateKey).toHaveBeenCalledWith(generatedSeed, 0);
4646
expect(keyServiceMock.getPublicKey).toHaveBeenCalledWith(privateKey);
4747
expect(keyServiceMock.getAddress).toHaveBeenCalledWith(publicKey);
4848
expect(result).toEqual({ privateKey, publicKey, address });

test/unit/modules/wallet.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ describe('Wallet class', () => {
238238
wallet.info = jest.fn().mockResolvedValue(info);
239239
blocksMock.create.mockResolvedValue({ hash: 'createdSendHash' } as any);
240240
blocksMock.process.mockResolvedValue('processedSendHash');
241+
KeyService.getPublicKey = jest.fn().mockReturnValue('publicKey');
241242

242243
const result = await wallet.send('destinationAddress', '1');
243244
expect(wallet.info).toHaveBeenCalledWith({
@@ -249,9 +250,10 @@ describe('Wallet class', () => {
249250
previous: 'frontierHash',
250251
representative: 'representative',
251252
balance: '4000000000000000000000000000000', // Adjusted for unit conversion in test setup
252-
link: 'destinationAddress',
253+
link: 'publicKey',
253254
key: privateKey,
254255
});
256+
expect(KeyService.getPublicKey).toHaveBeenCalledWith('destinationAddress');
255257
expect(blocksMock.process).toHaveBeenCalledWith({ hash: 'createdSendHash' }, 'send');
256258
expect(result).toBe('processedSendHash');
257259
});

test/unit/services/key-service.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ describe('KeyService', () => {
2424
expect(result).toBe(seed);
2525
});
2626

27-
it('should derive a secret key', () => {
27+
it('should derive a private key', () => {
2828
const seed = 'cca6fda2102c958239b2e0f02e688414c23939271b7bcfe0d5014ab246071c12';
2929
const index = 0;
3030
const secretKey = 'secretKey123';
3131
(deriveSecretKey as jest.Mock).mockReturnValue(secretKey);
3232

33-
const result = KeyService.getSecretKey(seed, index);
33+
const result = KeyService.getPrivateKey(seed, index);
3434

3535
expect(deriveSecretKey).toHaveBeenCalledWith(seed, index);
3636
expect(result).toBe(secretKey);

0 commit comments

Comments
 (0)