This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * Fix issues * add output type filter * wip * wip * update electron * update node * replace remote module * wip * wip * clean up electron.js * wip * Fix bug * wip * integrate asset info fetching * clean up comments * disable logging * Feat update lib (#17) * wip * Fix issues * add output type filter * wip * wip * update electron * update node * replace remote module * wip * wip * clean up electron.js * Fix bug * clean up comments * disable logging * fix apiRegistryClient unhandled exception * wip Co-authored-by: Begoña Alvarez <[email protected]>
- Loading branch information
1 parent
dca785d
commit adb5175
Showing
13 changed files
with
275 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Changelog | ||
|
||
## v0.6.2 | ||
|
||
Add Asset Registry support | ||
|
||
## v0.6.0 | ||
|
||
Add PoW for requesting funds | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "pollen-wallet", | ||
"description": "IOTA Pollen Wallet", | ||
"version": "0.6.0", | ||
"version": "0.6.2", | ||
"author": "Martyn Janes <[email protected]>", | ||
"repository": { | ||
"type": "git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import fetch from "node-fetch"; | ||
import { IAssetRequest } from "./models/IAssetRequest"; | ||
import { IAssetResponse } from "./models/IAssetResponse"; | ||
import { IResponse } from "./models/IResponse"; | ||
|
||
/** | ||
* Class to handle API communications. | ||
*/ | ||
export class ApiRegistryClient { | ||
/** | ||
* The end point of the api. | ||
*/ | ||
private readonly _endpoint: string; | ||
|
||
/** | ||
* The user of the api. | ||
*/ | ||
private readonly _user?: string; | ||
|
||
/** | ||
* The password of the api. | ||
*/ | ||
private readonly _password?: string; | ||
|
||
/** | ||
* Create a new instance of ApiClient. | ||
* @param endPoint The endpoint for the API. | ||
* @param user The user for the API. | ||
* @param password The password for the API. | ||
*/ | ||
constructor(endPoint: string, user?: string, password?: string) { | ||
this._endpoint = endPoint; | ||
this._user = user; | ||
this._password = password; | ||
} | ||
|
||
/** | ||
* Fetch Asset info. | ||
* @returns The response from the request. | ||
*/ | ||
public async fetchAsset(assetID: string): Promise<IAssetResponse> { | ||
return this.sendRequest<null, IAssetResponse>( | ||
"get", "registries/test/assets/"+assetID); | ||
} | ||
|
||
/** | ||
* Reigester Asset info. | ||
* @returns The response from the request. | ||
*/ | ||
public async registerAsset(request: IAssetRequest): Promise<IAssetResponse> { | ||
return this.sendRequest<IAssetRequest, IAssetResponse>( | ||
"post", "registries/test/assets", request); | ||
} | ||
|
||
/** | ||
* Send a request and handle errors. | ||
* @param verb The HTTP verb to make the request. | ||
* @param path The path to send the request to. | ||
* @param request The request to send. | ||
* @returns The response from the request. | ||
*/ | ||
private async sendRequest<T, U extends IResponse>( | ||
verb: "put" | "post" | "get" | "delete", | ||
path: string, | ||
request?: T | undefined): Promise<U> { | ||
let response: U; | ||
|
||
try { | ||
const headers: { [id: string]: string } = {}; | ||
headers["Content-Type"] = "application/json"; | ||
|
||
if (this._user && this._password) { | ||
headers.Authorization = `Basic ${btoa(`${this._user}:${this._password}`)}`; | ||
} | ||
|
||
let fetchResponse; | ||
|
||
if (verb === "get") { | ||
fetchResponse = await fetch( | ||
`${this._endpoint}/${path}`, | ||
{ | ||
method: "get", | ||
headers | ||
} | ||
); | ||
} else if (verb === "post") { | ||
fetchResponse = await fetch( | ||
`${this._endpoint}/${path}`, | ||
{ | ||
method: "post", | ||
headers, | ||
body: JSON.stringify(request) | ||
} | ||
); | ||
} else if (verb === "put") { | ||
fetchResponse = await fetch( | ||
`${this._endpoint}/${path}`, | ||
{ | ||
method: "put", | ||
headers, | ||
body: JSON.stringify(request) | ||
} | ||
); | ||
} else if (verb === "delete") { | ||
fetchResponse = await fetch( | ||
`${this._endpoint}/${path}`, | ||
{ | ||
method: "delete", | ||
headers | ||
} | ||
); | ||
} | ||
|
||
if (!fetchResponse) { | ||
throw new Error("No data was returned from the API"); | ||
} else { | ||
try { | ||
response = await fetchResponse.json(); | ||
} catch (err) { | ||
const text = await fetchResponse.text(); | ||
throw new Error(err.message + " --- " + text); | ||
} | ||
if (!fetchResponse.ok) { | ||
if (response.error) { | ||
throw new Error(response.error); | ||
} else { | ||
const isComError = fetchResponse.status >= 500; | ||
let msg = fetchResponse.statusText; | ||
|
||
if (msg === "Network Error") { | ||
msg = "There is a problem communicating with the network"; | ||
} | ||
|
||
if (!msg.endsWith(".")) { | ||
msg += "."; | ||
} | ||
|
||
if (isComError) { | ||
msg += "\n\nPlease try again later."; | ||
} | ||
|
||
throw new Error(msg); | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
throw new Error(`The application is not able to complete the request, due to the following error:\n\n${err.message}`); | ||
} | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface IAssetRequest { | ||
ID: string; | ||
name: string; | ||
symbol: string; | ||
supply: number; | ||
transactionID: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IResponse } from "./IResponse"; | ||
|
||
export interface IAssetResponse extends IResponse { | ||
ID: string; | ||
name: string; | ||
symbol: string; | ||
supply: number; | ||
transactionID: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ISendFundsResponse { | ||
transactionID: string; | ||
|
||
assetID?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.