Skip to content

Commit

Permalink
feat: generate message sign url (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored Aug 17, 2022
1 parent 1f1793d commit 56e2c20
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
distribution/
build/
coverage/
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,49 @@

> A package to generate URLs compatible with ARKVault
## Usage

Install the package in your project
## Installation

```bash
pnpm install @ardenthq/arkvault-url
```

Import the builder from the package
## Usage

Import and instantiate the builder.

```js
import { URLBuilder } from "@ardenthq/arkvault-url";

const builder = new URLBuilder("https://your-url.com");
```

> The `baseUrl` parameter is optional and defaults to "https://app.arkvault.io"
The builder can be further configured by setting a custom coin and nethash, which default to "ARK" and ARK's mainnet nethash otherwise.

```js
builder.setCoin("CUSTOM");
builder.setNethash("0123...cdef)";
```
Initiate the builder and generate your URL
### Methods
The following methods are available.
#### Transfer
```js
const builder = new URLBuilder();
const url = builder.generateTransfer("recipient");
builder.generateTransfer("recipient");

> https://app.arkvault.io/#/?method=transfer&recipient=recipient&coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988
```
For further customizations, you can use the following methods
#### Sign Message
```js
builder.setCoin("Custom"); // Defaults to "ARK"
builder.setNethash("0123..ef)"; // Defaults to ARK's mainnet nethash
builder.generateMessageSign("hello world");

new URLBuilder("https://your-url.com"); // Defaults to app.arkvault.io
> https://app.arkvault.io/#/?method=sign&message=hello+world&coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988"
```
> More detailed docs will follow soon
Expand All @@ -44,28 +57,28 @@ new URLBuilder("https://your-url.com"); // Defaults to app.arkvault.io
[pnpm](https://pnpm.js.org/en/) is required to be installed before starting. It is used to manage this repo.
### Apply `eslint` rules to source
### Install dependencies
```bash
pnpm run lint
pnpm install
```
### Apply `eslint` rules to tests
### Apply `eslint` rules and `prettier` formatting
```bash
pnpm run lint:test
pnpm format
```
### Apply `prettier` formatting
### Run tests
```bash
pnpm run prettier
pnpm test
```
### Run tests
### Build the production code
```bash
pnpm run test
pnpm build
```
## Security
Expand Down
15 changes: 13 additions & 2 deletions source/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Methods } from "./enums";

type BaseOptions = {
coin?: string;
nethash?: string;
Expand All @@ -8,9 +10,18 @@ type TransferOptions = {
amount?: number;
} & BaseOptions;

type MessageSignOptions = {
address?: string;
} & BaseOptions;

type GenerateTransferOptions = {
recipient?: string;
method?: string;
method?: Methods.Transfer;
} & TransferOptions;

export type { BaseOptions, GenerateTransferOptions, TransferOptions };
type GenerateMessageSignOptions = {
message?: string;
method?: Methods.Sign;
} & MessageSignOptions;

export type { BaseOptions, GenerateMessageSignOptions, GenerateTransferOptions, MessageSignOptions, TransferOptions };
1 change: 1 addition & 0 deletions source/enums.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
enum Methods {
"Transfer" = "transfer",
"Sign" = "sign",
}

enum Networks {
Expand Down
19 changes: 19 additions & 0 deletions source/url-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,23 @@ describe("URLBuilder", ({ assert, it }) => {
"baseUrl?method=transfer&recipient=recipient&coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988",
);
});

it("should generate sign message url", () => {
const builder = new URLBuilder("baseUrl");

assert.is(
builder.generateMessageSign("test", { address: "address" }),
"baseUrl?address=address&message=test&method=sign&coin=ARK&nethash=6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988",
);
});

it("should require message when generating sign message url", () => {
const builder = new URLBuilder("baseUrl");

assert.throws(() => builder.generateMessageSign(""), "message is required");
//@ts-ignore
assert.throws(() => builder.generateMessageSign(), "message is required");
//@ts-ignore
assert.throws(() => builder.generateMessageSign(undefined), "message is required");
});
});
21 changes: 19 additions & 2 deletions source/url-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { GenerateTransferOptions, TransferOptions } from "./contracts.js";
import {
GenerateMessageSignOptions,
GenerateTransferOptions,
MessageSignOptions,
TransferOptions,
} from "./contracts.js";
import { Methods, Networks } from "./enums.js";

export class URLBuilder {
Expand Down Expand Up @@ -54,7 +59,19 @@ export class URLBuilder {
});
}

#generate(options: GenerateTransferOptions): string {
public generateMessageSign(message: string, options: MessageSignOptions = {}) {
if (!message) {
throw new Error("message is required");
}

return this.#generate({
...options,
message,
method: Methods.Sign,
});
}

#generate(options: GenerateTransferOptions | GenerateMessageSignOptions): string {
if (!this.#coin) {
throw new Error("coin has to be set");
}
Expand Down

0 comments on commit 56e2c20

Please sign in to comment.