mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Improve token validation.
This commit is contained in:
parent
3abe15a023
commit
b32f708385
@ -28,7 +28,7 @@ export class OceanAccounts extends Instantiable {
|
|||||||
const ethAccounts: string[] = await this.web3.eth.getAccounts()
|
const ethAccounts: string[] = await this.web3.eth.getAccounts()
|
||||||
|
|
||||||
const accountPromises = ethAccounts
|
const accountPromises = ethAccounts
|
||||||
.map(address => new Account(address, this.instanceConfig))
|
.map((address) => new Account(address, this.instanceConfig))
|
||||||
return Promise.all(accountPromises)
|
return Promise.all(accountPromises)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ export class OceanAuth extends Instantiable {
|
|||||||
* @return {Promise<string>} Token
|
* @return {Promise<string>} Token
|
||||||
*/
|
*/
|
||||||
public async get(account: Account): Promise<string> {
|
public async get(account: Account): Promise<string> {
|
||||||
const time = Date.now()
|
const time = Math.floor(Date.now() / 1000)
|
||||||
const message = `${this.getMessage()}\n${time}`
|
const message = `${this.getMessage()}\n${time}`
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -51,16 +51,16 @@ export class OceanAuth extends Instantiable {
|
|||||||
*/
|
*/
|
||||||
public async check(token: string): Promise<string> {
|
public async check(token: string): Promise<string> {
|
||||||
const expiration = this.getExpiration()
|
const expiration = this.getExpiration()
|
||||||
const [signature, timestamp] = token.split('-')
|
const [signature, timestamp] = token.split("-")
|
||||||
|
|
||||||
const message = `${this.getMessage()}\n${timestamp}`
|
const message = `${this.getMessage()}\n${timestamp}`
|
||||||
|
|
||||||
if ((+timestamp + expiration) < Date.now()) {
|
if (((+timestamp * 1000) + expiration) < Date.now()) {
|
||||||
return `0x${"0".repeat(40)}`
|
return `0x${"0".repeat(40)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.web3.utils.toChecksumAddress(
|
return this.web3.utils.toChecksumAddress(
|
||||||
await this.ocean.utils.signature.verifyText(message, signature)
|
await this.ocean.utils.signature.verifyText(message, signature),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,12 +43,12 @@ export class SubscribablePromise<T extends any, P extends any> {
|
|||||||
|
|
||||||
Promise.resolve(execution as any)
|
Promise.resolve(execution as any)
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
if (Promise.resolve(execution as any) === execution) {
|
if (typeof (execution as any).then === "function") {
|
||||||
this.observer.complete(result)
|
this.observer.complete(result)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((result) => {
|
.catch((result) => {
|
||||||
if (Promise.resolve(execution as any) === execution) {
|
if (typeof (execution as any).then === "function") {
|
||||||
this.observer.error(result)
|
this.observer.error(result)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -55,7 +55,7 @@ describe("OceanAuth", () => {
|
|||||||
|
|
||||||
describe("#store()", () => {
|
describe("#store()", () => {
|
||||||
it("should sign and store the token", async () => {
|
it("should sign and store the token", async () => {
|
||||||
const writeTokenSpy = spy.on(oceanAuth as any, 'writeToken', () => {})
|
const writeTokenSpy = spy.on(oceanAuth as any, "writeToken", () => {})
|
||||||
|
|
||||||
await oceanAuth.store(account)
|
await oceanAuth.store(account)
|
||||||
|
|
||||||
@ -65,17 +65,17 @@ describe("OceanAuth", () => {
|
|||||||
|
|
||||||
describe("#restore()", () => {
|
describe("#restore()", () => {
|
||||||
it("should return a stored token", async () => {
|
it("should return a stored token", async () => {
|
||||||
spy.on(oceanAuth as any, 'readToken', () => 'token')
|
spy.on(oceanAuth as any, "readToken", () => "token")
|
||||||
spy.on(oceanAuth as any, 'check', () => account.getId())
|
spy.on(oceanAuth as any, "check", () => account.getId())
|
||||||
|
|
||||||
const token = await oceanAuth.restore(account)
|
const token = await oceanAuth.restore(account)
|
||||||
|
|
||||||
assert.equal(token, 'token')
|
assert.equal(token, "token")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should not return values if there is any error", async () => {
|
it("should not return values if there is any error", async () => {
|
||||||
spy.on(oceanAuth as any, 'readToken', () => 'token')
|
spy.on(oceanAuth as any, "readToken", () => "token")
|
||||||
spy.on(oceanAuth as any, 'check', () => '0x...')
|
spy.on(oceanAuth as any, "check", () => "0x...")
|
||||||
|
|
||||||
const token = await oceanAuth.restore(account)
|
const token = await oceanAuth.restore(account)
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ describe("OceanAuth", () => {
|
|||||||
|
|
||||||
describe("#isStored()", () => {
|
describe("#isStored()", () => {
|
||||||
it("should know if the token is stored", async () => {
|
it("should know if the token is stored", async () => {
|
||||||
spy.on(oceanAuth as any, 'restore', () => account.getId())
|
spy.on(oceanAuth as any, "restore", () => account.getId())
|
||||||
|
|
||||||
const isStored = await oceanAuth.isStored(account)
|
const isStored = await oceanAuth.isStored(account)
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ describe("OceanAuth", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should know if the token is not stored", async () => {
|
it("should know if the token is not stored", async () => {
|
||||||
spy.on(oceanAuth as any, 'restore', () => undefined)
|
spy.on(oceanAuth as any, "restore", () => undefined)
|
||||||
|
|
||||||
const isStored = await oceanAuth.isStored(account)
|
const isStored = await oceanAuth.isStored(account)
|
||||||
|
|
||||||
@ -101,4 +101,3 @@ describe("OceanAuth", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user