Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate nonce test #146

Merged
merged 17 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export FEE_AMOUNT="{ \"amount\": 1, \"unit\": \"MB\" }"
Where FEE_TOKENS is a map (chainID => Token address) and FEE_AMOUNT is the fees amount (unit of fee token).
The 'unit' parameter is not used at the moment, but allows to specify an specific unit of size (MB, KB, GB, etc). Default is MB.

Start the database with the following command:

```bash
docker-compose -f typesense-compose.yml -p ocean-node up -d
```

Then start the node:

```bash
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"format": "prettier --parser typescript --ignore-path .gitignore --write '**/*.{js,jsx,ts,tsx}'",
"type-check": "tsc --noEmit",
"check-nonce": "npm run build && node dist/helpers/scripts/checkNonceTracking.js",
"mocha": "mocha --config=tests/.mocharc.json --node-env=test --exit",
"mocha": "mocha --config=tests/.mocharc.json --node-env=test --timeout 10000 --exit",
"test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover",
"test:unit": "npm run mocha -- 'tests/unit/**/*.test.ts'",
"test:unit:storage": "npm run mocha -- 'tests/unit/storage.test.ts'",
Expand Down
10 changes: 10 additions & 0 deletions tests/data/nonceSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { TypesenseCollectionCreateSchema } from '../../src/@types'

export const nonceSchema: TypesenseCollectionCreateSchema = {
name: 'nonce',
enable_nested_fields: true,
fields: [
{ name: 'id', type: 'string' },
{ name: 'nonce', type: 'int64', sort: true } // store nonce as string
]
}
81 changes: 81 additions & 0 deletions tests/integration/nonce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { expect } from 'chai'
import { ethers, ZeroAddress } from 'ethers'
import { nonceSchema } from '../data/nonceSchema.js'
import {
Typesense,
convertTypesenseConfig
} from '../../src/components/database/typesense'

describe('handle nonce', () => {
let typesense: Typesense
let error: Error

before(() => {
const url = 'http://localhost:8108/?apiKey=xyz'
typesense = new Typesense(convertTypesenseConfig(url))
})

it('instance Typesense', async () => {
expect(typesense).to.be.instanceOf(Typesense)
})

it('create nonce collection', async () => {
let result
try {
result = await typesense.collections(nonceSchema.name).retrieve()
} catch (error) {
result = await typesense.collections().create(nonceSchema)
}
expect(result.enable_nested_fields).to.equal(true)
expect(result.fields).to.not.be.an('undefined')
expect(result.name).to.be.equal(nonceSchema.name)
expect(result.num_documents).to.equal(0)
})

it('should validate signature', async () => {
try {
await typesense
.collections(nonceSchema.name)
.documents()
.retrieve('0x4cc9DBfc4bEeA8c986c61DAABB350C2eC55e29d1')
console.log('document in checkDocumentExists: ', document)
// if not, create it now
} catch (ex) {
await typesense.collections(nonceSchema.name).documents().create({
id: '0x4cc9DBfc4bEeA8c986c61DAABB350C2eC55e29d1',
nonce: 1
})
}
const wallet = new ethers.Wallet(
'0xbee525d70c715bee6ca15ea5113e544d13cc1bb2817e07113d0af7755ddb6391'
)
// message to sign
const nonce = '1'
const expectedAddress = await wallet.getAddress()
// '0x8F292046bb73595A978F4e7A131b4EBd03A15e8a'
// sign message/nonce
const signature = await wallet.signMessage(nonce)
const actualAddress = ethers.verifyMessage(nonce, signature)
expect(actualAddress).to.be.equal(expectedAddress)
})

it('should get nonce (1)', async () => {
const document = await typesense
.collections(nonceSchema.name)
.documents()
.retrieve('0x4cc9DBfc4bEeA8c986c61DAABB350C2eC55e29d1')
expect(document.nonce).to.be.equal(1)
})

it('should throw error for retrieving unexistent address', async () => {
try {
await typesense.collections(nonceSchema.name).documents().retrieve(ZeroAddress)
} catch (err) {
error = err
}
console.log(error.message)
expect(error.message).to.eql(
'Could not find a document with id: 0x0000000000000000000000000000000000000000'
)
})
})
97 changes: 0 additions & 97 deletions tests/unit/nonce.spec.ts

This file was deleted.

Loading