Skip to content

Commit

Permalink
v1.0.0: Rename host to origin (#11)
Browse files Browse the repository at this point in the history
* rename host to origin

* fixed tests

* v1.0.0
  • Loading branch information
tanghel committed Feb 15, 2023
1 parent 1bcb964 commit 3dcb878
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ When initializing the client object, an optional config can also be specified wi
// When used from within a browser, will contain the hostname by default.
// It can be overridden for special situations
// Note: The server-side component will validate the `origin` header, which must
// match with the provided host in the client-side configuration
host: string = 'myApp.com';
// match with the provided origin in the client-side configuration
origin: string = 'https://myApp.com';

// The endpoint from where the current block information will be fetched upon initialization.
// The default value points to the mainnet API, but can be overridden to be network-specific
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-native-auth-client",
"version": "0.1.8",
"version": "1.0.0",
"description": "Native authentication client-side",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
Expand Down Expand Up @@ -43,4 +43,4 @@
"dependencies": {
"axios": "^0.27.2"
}
}
}
2 changes: 1 addition & 1 deletion src/entities/native.auth.client.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class NativeAuthClientConfig {
host: string = typeof window !== "undefined" ? window.location.hostname : '';
origin: string = typeof window !== "undefined" ? window.location.hostname : '';
apiUrl: string = 'https://api.multiversx.com';
expirySeconds: number = 60 * 60 * 24;
blockHashShard?: number;
Expand Down
8 changes: 4 additions & 4 deletions src/native.auth.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class NativeAuthClient {
async initialize(extraInfo: any = {}): Promise<string> {
const blockHash = await this.getCurrentBlockHash();
const encodedExtraInfo = this.encodeValue(JSON.stringify(extraInfo));
const host = this.encodeValue(this.config.host);
const origin = this.encodeValue(this.config.origin);

return `${host}.${blockHash}.${this.config.expirySeconds}.${encodedExtraInfo}`;
return `${origin}.${blockHash}.${this.config.expirySeconds}.${encodedExtraInfo}`;
}

private async getCurrentBlockHash(): Promise<string> {
Expand All @@ -42,10 +42,10 @@ export class NativeAuthClient {

private async getCurrentRound(): Promise<number> {
if (!this.config.gatewayUrl) {
throw new Error("Gateway URL not set");
throw new Error("Gateway URL not set");
}
if (!this.config.blockHashShard) {
throw new Error("Blockhash shard not set");
throw new Error("Blockhash shard not set");
}

const url = `${this.config.gatewayUrl}/network/status/${this.config.blockHashShard}`;
Expand Down
40 changes: 20 additions & 20 deletions test/native.auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { NativeAuthClient } from '../src';
describe("Native Auth", () => {
let mock: MockAdapter;
const ADDRESS = 'erd1qnk2vmuqywfqtdnkmauvpm8ls0xh00k8xeupuaf6cm6cd4rx89qqz0ppgl';
const SIGNATURE = '563cb2dfdf96ab335423a05287fa3cd00154034423d0062421ee6ce03230d941da6df9ce79689fcd173c0ba5d4331b3ccd82c8ec2e6ab4d875db1587c2ab720c';
const BLOCK_HASH = '82ec8044966efb2d00e8a6367ea23ddbc7bea6504ed98f4a1a536d7c21bb2682';
const SIGNATURE = '906e79d54e69e688680abee54ec0c49ce2561eb5abfd01865b31cb3ed738272c7cfc4fc8cc1c3590dd5757e622639b01a510945d7f7c9d1ceda20a50a817080d';
const BLOCK_HASH = 'ab459013b27fdc6fe98eed567bd0c1754e0628a4cc16883bf0170a29da37ad46';
const TTL = 86400;
const HOST = 'api.multiversx.com';
const TOKEN = `YXBpLm11bHRpdmVyc3guY29t.${BLOCK_HASH}.${TTL}.e30`;
const ACCESS_TOKEN = 'ZXJkMXFuazJ2bXVxeXdmcXRkbmttYXV2cG04bHMweGgwMGs4eGV1cHVhZjZjbTZjZDRyeDg5cXF6MHBwZ2w.WVhCcExtMTFiSFJwZG1WeWMzZ3VZMjl0LjgyZWM4MDQ0OTY2ZWZiMmQwMGU4YTYzNjdlYTIzZGRiYzdiZWE2NTA0ZWQ5OGY0YTFhNTM2ZDdjMjFiYjI2ODIuODY0MDAuZTMw.563cb2dfdf96ab335423a05287fa3cd00154034423d0062421ee6ce03230d941da6df9ce79689fcd173c0ba5d4331b3ccd82c8ec2e6ab4d875db1587c2ab720c';
const ORIGIN = 'https://api.multiversx.com';
const TOKEN = `aHR0cHM6Ly9hcGkubXVsdGl2ZXJzeC5jb20.${BLOCK_HASH}.${TTL}.e30`;
const ACCESS_TOKEN = 'ZXJkMXFuazJ2bXVxeXdmcXRkbmttYXV2cG04bHMweGgwMGs4eGV1cHVhZjZjbTZjZDRyeDg5cXF6MHBwZ2w.YUhSMGNITTZMeTloY0drdWJYVnNkR2wyWlhKemVDNWpiMjAuYWI0NTkwMTNiMjdmZGM2ZmU5OGVlZDU2N2JkMGMxNzU0ZTA2MjhhNGNjMTY4ODNiZjAxNzBhMjlkYTM3YWQ0Ni44NjQwMC5lMzA.906e79d54e69e688680abee54ec0c49ce2561eb5abfd01865b31cb3ed738272c7cfc4fc8cc1c3590dd5757e622639b01a510945d7f7c9d1ceda20a50a817080d';

const onLatestBlockHashGet = function (mock: MockAdapter): RequestHandler {
return mock.onGet('https://api.multiversx.com/blocks?size=1&fields=hash');
Expand All @@ -27,7 +27,7 @@ describe("Native Auth", () => {
describe("Client", () => {
it("Latest block should return signable token", async () => {
const client = new NativeAuthClient({
host: HOST,
origin: ORIGIN,
});

onLatestBlockHashGet(mock).reply(200, [{ hash: BLOCK_HASH }]);
Expand Down Expand Up @@ -62,18 +62,18 @@ describe("Native Auth", () => {
describe("Native Auth with gateway", () => {
let mock: MockAdapter;
const ADDRESS = 'erd1qnk2vmuqywfqtdnkmauvpm8ls0xh00k8xeupuaf6cm6cd4rx89qqz0ppgl';
const SIGNATURE = '563cb2dfdf96ab335423a05287fa3cd00154034423d0062421ee6ce03230d941da6df9ce79689fcd173c0ba5d4331b3ccd82c8ec2e6ab4d875db1587c2ab720c';
const BLOCK_HASH = '82ec8044966efb2d00e8a6367ea23ddbc7bea6504ed98f4a1a536d7c21bb2682';
const SIGNATURE = '906e79d54e69e688680abee54ec0c49ce2561eb5abfd01865b31cb3ed738272c7cfc4fc8cc1c3590dd5757e622639b01a510945d7f7c9d1ceda20a50a817080d';
const BLOCK_HASH = 'ab459013b27fdc6fe98eed567bd0c1754e0628a4cc16883bf0170a29da37ad46';
const TTL = 86400;
const HOST = 'api.multiversx.com';
const TOKEN = `YXBpLm11bHRpdmVyc3guY29t.${BLOCK_HASH}.${TTL}.e30`;
const ACCESS_TOKEN = 'ZXJkMXFuazJ2bXVxeXdmcXRkbmttYXV2cG04bHMweGgwMGs4eGV1cHVhZjZjbTZjZDRyeDg5cXF6MHBwZ2w.WVhCcExtMTFiSFJwZG1WeWMzZ3VZMjl0LjgyZWM4MDQ0OTY2ZWZiMmQwMGU4YTYzNjdlYTIzZGRiYzdiZWE2NTA0ZWQ5OGY0YTFhNTM2ZDdjMjFiYjI2ODIuODY0MDAuZTMw.563cb2dfdf96ab335423a05287fa3cd00154034423d0062421ee6ce03230d941da6df9ce79689fcd173c0ba5d4331b3ccd82c8ec2e6ab4d875db1587c2ab720c';
const latestRound = 115656;
const ORIGIN = 'https://api.multiversx.com';
const TOKEN = `aHR0cHM6Ly9hcGkubXVsdGl2ZXJzeC5jb20.${BLOCK_HASH}.${TTL}.e30`;
const ACCESS_TOKEN = 'ZXJkMXFuazJ2bXVxeXdmcXRkbmttYXV2cG04bHMweGgwMGs4eGV1cHVhZjZjbTZjZDRyeDg5cXF6MHBwZ2w.YUhSMGNITTZMeTloY0drdWJYVnNkR2wyWlhKemVDNWpiMjAuYWI0NTkwMTNiMjdmZGM2ZmU5OGVlZDU2N2JkMGMxNzU0ZTA2MjhhNGNjMTY4ODNiZjAxNzBhMjlkYTM3YWQ0Ni44NjQwMC5lMzA.906e79d54e69e688680abee54ec0c49ce2561eb5abfd01865b31cb3ed738272c7cfc4fc8cc1c3590dd5757e622639b01a510945d7f7c9d1ceda20a50a817080d';
const LATEST_ROUND = 115656;
const METASHARD = 4294967295;
const GATEWAY = 'https://gateway.multiversx.com';

const onBlocksByRound = function (mock: MockAdapter): RequestHandler {
return mock.onGet(`${GATEWAY}/blocks/by-round/${latestRound}`);
return mock.onGet(`${GATEWAY}/blocks/by-round/${LATEST_ROUND}`);
};

//https://testnet-gateway.multiversx.com/network/status/4294967295
Expand All @@ -92,13 +92,13 @@ describe("Native Auth with gateway", () => {
describe("Client", () => {
it("Latest block should return signable token", async () => {
const client = new NativeAuthClient({
host: HOST,
origin: ORIGIN,
gatewayUrl: GATEWAY,
blockHashShard: METASHARD,
});

onNetworkStatus(mock).reply(200, { data: { status: {erd_current_round: latestRound}} });
onBlocksByRound(mock).reply(200, { data: { blocks: [ {shard: METASHARD, hash: BLOCK_HASH}] }});
onNetworkStatus(mock).reply(200, { data: { status: { erd_current_round: LATEST_ROUND } } });
onBlocksByRound(mock).reply(200, { data: { blocks: [{ shard: METASHARD, hash: BLOCK_HASH }] } });

const token = await client.initialize();

Expand All @@ -122,7 +122,7 @@ describe("Native Auth with gateway", () => {
blockHashShard: METASHARD,
});

onNetworkStatus(mock).reply(200, [{ data: { status: {erd_current_round: latestRound}} }]);
onNetworkStatus(mock).reply(200, [{ data: { status: { erd_current_round: LATEST_ROUND } } }]);
onBlocksByRound(mock).reply(500);

await expect(client.initialize()).rejects.toThrow();
Expand All @@ -135,9 +135,9 @@ describe("Native Auth with gateway", () => {
});

const accessToken = client.getToken(
ADDRESS,
TOKEN,
SIGNATURE
ADDRESS,
TOKEN,
SIGNATURE
);

expect(accessToken).toStrictEqual(ACCESS_TOKEN);
Expand Down

0 comments on commit 3dcb878

Please sign in to comment.