Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion e2e/infrastructure/NodeHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('NodeHttp', () => {
expect(nodeInfo.publicKey).not.to.be.undefined;
expect(nodeInfo.roles).not.to.be.undefined;
expect(nodeInfo.version).not.to.be.undefined;
expect(nodeInfo.nodePublicKey).not.to.be.undefined;
});
});

Expand Down Expand Up @@ -93,7 +94,7 @@ describe('NodeHttp', () => {

describe('getUnlockedAccount', () => {
it('should return unlocked account', async () => {
const unlockedAccount = await nodeRepository.getUnlockedAccount('test').toPromise();
const unlockedAccount = await nodeRepository.getUnlockedAccount().toPromise();
expect(unlockedAccount).not.to.be.null;
expect(unlockedAccount.length).greaterThan(0);
});
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"ripemd160": "^2.0.2",
"rxjs": "^6.6.3",
"rxjs-compat": "^6.6.3",
"symbol-openapi-typescript-fetch-client": "0.10.1-SNAPSHOT.202011091143",
"symbol-openapi-typescript-fetch-client": "0.10.1-SNAPSHOT.202011141947",
"tweetnacl": "^1.0.3",
"ws": "^7.3.1"
},
Expand Down
6 changes: 3 additions & 3 deletions src/infrastructure/NodeHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ export class NodeHttp extends Http implements NodeRepository {

/**
* Return unlocked harvesting account from node.
* @param peer Peer node host name.
* @returns Observable<string[]>
*/
getUnlockedAccount(peer: string): Observable<string[]> {
return this.call(this.nodeRoutesApi.getUnlockedAccount(peer), (body) => {
getUnlockedAccount(): Observable<string[]> {
return this.call(this.nodeRoutesApi.getUnlockedAccount(), (body) => {
return body.unlockedAccount;
});
}
Expand All @@ -138,6 +137,7 @@ export class NodeHttp extends Http implements NodeRepository {
this.getNodeRoles(nodeInfo.roles.valueOf()),
nodeInfo.host,
nodeInfo.friendlyName,
nodeInfo.nodePublicKey,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/NodeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface NodeRepository {

/**
* Gets blockchain storage info.
* @param hostName Peer node host name.
* @returns Observable<StorageInfo>
*/
getStorageInfo(): Observable<StorageInfo>;
Expand All @@ -66,8 +67,7 @@ export interface NodeRepository {

/**
* Return unlocked harvesting account from node.
* @param peer Peer node host name.
* @returns Observable<string[]>
*/
getUnlockedAccount(peer: string): Observable<string[]>;
getUnlockedAccount(): Observable<string[]>;
}
5 changes: 5 additions & 0 deletions src/infrastructure/RepositoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@ export interface RepositoryFactory {
* @returns the network currencies.
*/
getCurrencies(): Observable<NetworkCurrencies>;

/**
* @returns the node public key
*/
getNodePublicKey(): Observable<string | undefined>;
}
5 changes: 5 additions & 0 deletions src/infrastructure/RepositoryFactoryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ export interface RepositoryFactoryConfig {
* The preconfigured symbol network currencies for offline access. They are loaded from server by default if not provided.
*/
networkCurrencies?: NetworkCurrencies;

/**
* The node public key used for NodeKeyLink transaction in delegated harvesting.
*/
nodePublicKey?: string;
}
22 changes: 15 additions & 7 deletions src/infrastructure/RepositoryFactoryHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class RepositoryFactoryHttp implements RepositoryFactory {
private readonly epochAdjustment: Observable<number>;
private readonly networkProperties: Observable<NetworkConfiguration>;
private readonly networkCurrencies: Observable<NetworkCurrencies>;
private readonly nodePublicKey: Observable<string | undefined>;
/**
* Constructor
* @param url the server url.
Expand All @@ -93,13 +94,14 @@ export class RepositoryFactoryHttp implements RepositoryFactory {
}),
),
);
this.generationHash = configs?.generationHash
? observableOf(configs.generationHash)
: this.cache(() =>
this.createNodeRepository()
.getNodeInfo()
.pipe(map((b) => b.networkGenerationHashSeed)),
);
if (configs?.generationHash && configs?.nodePublicKey) {
this.generationHash = observableOf(configs.generationHash);
this.nodePublicKey = observableOf(configs.nodePublicKey);
} else {
const nodeInfoObservable = this.createNodeRepository().getNodeInfo();
this.generationHash = this.cache(() => nodeInfoObservable.pipe(map((b) => b.networkGenerationHashSeed)));
this.nodePublicKey = this.cache(() => nodeInfoObservable.pipe(map((b) => b.nodePublicKey)));
}
this.websocketUrl = configs?.websocketUrl ? configs?.websocketUrl : `${url.replace(/\/$/, '')}/ws`;
this.websocketInjected = configs?.websocketInjected;
this.networkCurrencies = configs?.networkCurrencies
Expand Down Expand Up @@ -198,4 +200,10 @@ export class RepositoryFactoryHttp implements RepositoryFactory {
getCurrencies(): Observable<NetworkCurrencies> {
return this.networkCurrencies;
}
/**
* @returns the node public key
*/
getNodePublicKey(): Observable<string | undefined> {
return this.nodePublicKey;
}
}
5 changes: 5 additions & 0 deletions src/model/node/NodeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class NodeInfo {
* @param roles
* @param host
* @param friendlyName
* @param nodePublicKey
*/
constructor(
/**
Expand Down Expand Up @@ -63,5 +64,9 @@ export class NodeInfo {
* The name of the node.
*/
public readonly friendlyName: string,
/**
* The node public key used for NodeKeyLink transaction.
*/
public readonly nodePublicKey?: string,
) {}
}
4 changes: 2 additions & 2 deletions test/infrastructure/NodeHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ describe('NodeHttp', () => {
it('getUnlockedAccount', async () => {
const body = { unlockedAccount: ['key1', 'key2'] };

when(nodeRoutesApi.getUnlockedAccount('test')).thenReturn(Promise.resolve(body));
when(nodeRoutesApi.getUnlockedAccount()).thenReturn(Promise.resolve(body));

const unlockedAccount = await nodeRepository.getUnlockedAccount('test').toPromise();
const unlockedAccount = await nodeRepository.getUnlockedAccount().toPromise();
expect(unlockedAccount).to.be.not.null;
expect(unlockedAccount[0]).to.be.equal('key1');
expect(unlockedAccount[1]).to.be.equal('key2');
Expand Down
35 changes: 35 additions & 0 deletions test/infrastructure/RepositoryFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ describe('RepositoryFactory', () => {
});
});

it('Should get nodePubicKey from cache', (done) => {
let counter = 0;
const repositoryMock: NodeRepository = mock();
const observableOfNodeInfo = observableOf({ nodePublicKey: 'aaaa' } as NodeInfo).pipe(
map((v) => {
counter++;
return v;
}),
);
when(repositoryMock.getNodeInfo()).thenReturn(observableOfNodeInfo);
expect(observableOfNodeInfo).to.be.equals(observableOfNodeInfo);
const repositoryFactory = new (class RepositoryFactoryHttpForTest extends RepositoryFactoryHttp {
createNodeRepository(): NodeRepository {
return instance(repositoryMock);
}
})('http://localhost:3000', {
networkType: NetworkType.PRIVATE_TEST,
});

expect(counter).to.be.equals(0);
repositoryFactory.getNodePublicKey().subscribe((gh) => {
expect(counter).to.be.equals(1);
expect(gh).to.be.equals('aaaa');
repositoryFactory.getNodePublicKey().subscribe((g) => {
expect(counter).to.be.equals(1);
expect(g).to.be.equals('aaaa');
repositoryFactory.getNodePublicKey().subscribe((h) => {
expect(counter).to.be.equals(1);
expect(h).to.be.equals('aaaa');
done();
});
});
});
});

it('Should get NetworkType from cache', (done) => {
let counter = 0;
const repositoryMock: NetworkRepository = mock();
Expand Down