diff --git a/README.md b/README.md index f8ef342..6dee5e9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index d133dd4..e3fe186 100644 --- a/package.json +++ b/package.json @@ -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", @@ -43,4 +43,4 @@ "dependencies": { "axios": "^0.27.2" } -} +} \ No newline at end of file diff --git a/src/entities/native.auth.client.config.ts b/src/entities/native.auth.client.config.ts index e02d1b8..5f0ee77 100644 --- a/src/entities/native.auth.client.config.ts +++ b/src/entities/native.auth.client.config.ts @@ -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; diff --git a/src/native.auth.client.ts b/src/native.auth.client.ts index 9896cf1..91ab981 100644 --- a/src/native.auth.client.ts +++ b/src/native.auth.client.ts @@ -19,9 +19,9 @@ export class NativeAuthClient { async initialize(extraInfo: any = {}): Promise { 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 { @@ -42,10 +42,10 @@ export class NativeAuthClient { private async getCurrentRound(): Promise { 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}`; diff --git a/test/native.auth.test.ts b/test/native.auth.test.ts index fc54882..c2d5bf8 100644 --- a/test/native.auth.test.ts +++ b/test/native.auth.test.ts @@ -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'); @@ -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 }]); @@ -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 @@ -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(); @@ -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(); @@ -135,9 +135,9 @@ describe("Native Auth with gateway", () => { }); const accessToken = client.getToken( - ADDRESS, - TOKEN, - SIGNATURE + ADDRESS, + TOKEN, + SIGNATURE ); expect(accessToken).toStrictEqual(ACCESS_TOKEN);