-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
983 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@moralisweb3/common-aptos-utils': minor | ||
'@moralisweb3/common-auth-utils': minor | ||
'@moralisweb3/auth': minor | ||
--- | ||
|
||
Add Aptos support for Moralis.Auth |
79 changes: 79 additions & 0 deletions
79
packages/auth/integration/mocks/endpoints/requestAptosChallenge.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { MockScenarios } from '@moralisweb3/test-utils'; | ||
import { createErrorResponse } from '../response/errorResponse'; | ||
import { createSimpleAuthResponse } from '../response/simpleAuthResponse'; | ||
|
||
export const mockRequestAptosChallenge = MockScenarios.create( | ||
{ | ||
method: 'post', | ||
name: 'mockRequestAptosChallenge', | ||
url: `/challenge/request/aptos`, | ||
getParams: ({ reqBody }) => { | ||
return { | ||
domain: reqBody?.domain, | ||
chainId: reqBody?.chainId, | ||
address: reqBody?.address, | ||
publicKey: reqBody?.publicKey, | ||
statement: reqBody?.statement, | ||
uri: reqBody?.uri, | ||
expirationTime: reqBody?.expirationTime, | ||
notBefore: reqBody?.notBefore, | ||
resources: reqBody?.resources, | ||
timeout: reqBody?.timeout, | ||
networkType: reqBody?.networkType, | ||
}; | ||
}, | ||
}, | ||
[ | ||
{ | ||
condition: { | ||
domain: 'defi.finance', | ||
chainId: 'mainnet', | ||
address: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
publicKey: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
statement: 'Please confirm', | ||
uri: 'https://defi.finance/', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 15, | ||
}, | ||
response: createSimpleAuthResponse( | ||
'Mk5deGOhekYev18pJ', | ||
'defi.finance wants you to sign in with your Aptos account:\n0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d\n\nPlease confirm\n\nURI: https://defi.finance/\nVersion: 1\nChain ID: 1\nNonce: 3c00srSBbEfdOwn4M\nIssued At: 2023-02-06T08:38:56.456Z\nExpiration Time: 2020-01-01T00:00:00.000Z\nNot Before: 2020-01-01T00:00:00.000Z\nResources:\n- https://docs.moralis.io/', | ||
'0x13e04b6cd6f84deef360a444499cbaccae717624f96cfa6dfe7cb250eced74eb', | ||
), | ||
}, | ||
{ | ||
condition: { | ||
statement: 'INVALID_ADDRESS', | ||
domain: 'defi.finance', | ||
chain: 'mainnet', | ||
address: 'some-address', | ||
uri: 'https://defi.finance/', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 15, | ||
networkType: 'aptos', | ||
}, | ||
responseStatus: 400, | ||
response: createErrorResponse('INVALID_ADDRESS: some-address'), | ||
}, | ||
{ | ||
condition: { | ||
statement: 'MULTI_ERROR', | ||
domain: 'defi.finance', | ||
chain: 'mainnet', | ||
address: 'some-address', | ||
uri: 'finance', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 15, | ||
networkType: 'aptos', | ||
}, | ||
responseStatus: 400, | ||
response: createErrorResponse('MULTI ERROR'), | ||
}, | ||
], | ||
); |
42 changes: 42 additions & 0 deletions
42
packages/auth/integration/mocks/endpoints/verifyAptosChallenge.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { MockScenarios } from '@moralisweb3/test-utils'; | ||
import { createAptosAuthResponse } from '../response/aptosAuthResponse'; | ||
import { createErrorResponse } from '../response/errorResponse'; | ||
|
||
export const mockVerifyAptosChallenge = MockScenarios.create( | ||
{ | ||
method: 'post', | ||
name: 'mockVerifyAptosChallenge', | ||
url: `/challenge/verify/aptos`, | ||
getParams: ({ reqBody }) => { | ||
return { | ||
message: reqBody?.message, | ||
signature: reqBody?.signature, | ||
}; | ||
}, | ||
}, | ||
[ | ||
{ | ||
condition: { | ||
message: 'VALID_RESPONSE', | ||
signature: '2pH9DqD5rve2qV4yBDshcAjWd2y8TqMx8BPb7f3KoNnuLEhE5JwjruYi4jaFaD4HN6wriLz2Vdr32kRBAJmHcyny', | ||
}, | ||
response: createAptosAuthResponse('VALID_RESPONSE'), | ||
}, | ||
{ | ||
condition: { | ||
message: 'INVALID_SIGNATURE', | ||
signature: 'some-signature', | ||
}, | ||
responseStatus: 400, | ||
response: createErrorResponse('INVALID_SIGNATURE: some-signature'), | ||
}, | ||
{ | ||
condition: { | ||
message: '', | ||
signature: 'some-signature', | ||
}, | ||
responseStatus: 400, | ||
response: createErrorResponse('MULTI_ERROR'), | ||
}, | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/auth/integration/mocks/response/aptosAuthResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const defaultMockAptosAuth = { | ||
id: 'fRyt67D3eRss3RrX', | ||
domain: 'defi.finance', | ||
statement: 'Please confirm', | ||
uri: 'https://defi.finance/', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
version: '1.0', | ||
nonce: '0x1234567890abcdef0123456789abcdef1234567890abcdef', | ||
profileId: '0xbfbcfab169c67072ff418133124480fea02175f1402aaa497daa4fd09026b0e1', | ||
chainId: '1', | ||
address: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
publicKey: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
}; | ||
|
||
export const createAptosAuthResponse = (tag: string) => ({ | ||
...defaultMockAptosAuth, | ||
tag, | ||
}); |
72 changes: 72 additions & 0 deletions
72
packages/auth/integration/test/requestAptosChallenge.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Auth } from '../../src/Auth'; | ||
import { cleanAuth, setupAuth } from '../setup'; | ||
|
||
describe('requestEvmChallenge', () => { | ||
let AuthApi: Auth; | ||
|
||
beforeAll(() => { | ||
AuthApi = setupAuth(); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanAuth(); | ||
}); | ||
|
||
describe('Request Aptos Challenge', () => { | ||
it('should request aptos challenge successfully', async () => { | ||
const result = await AuthApi.requestMessage({ | ||
networkType: 'aptos', | ||
domain: 'defi.finance', | ||
statement: 'Please confirm', | ||
uri: 'https://defi.finance/', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 15, | ||
chain: 'mainnet', | ||
address: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
publicKey: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
}); | ||
expect(result).toBeDefined(); | ||
expect(result.result.id).toEqual('Mk5deGOhekYev18pJ'); | ||
expect(result.result.message).toEqual("defi.finance wants you to sign in with your Aptos account:\n0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d\n\nPlease confirm\n\nURI: https://defi.finance/\nVersion: 1\nChain ID: 1\nNonce: 3c00srSBbEfdOwn4M\nIssued At: 2023-02-06T08:38:56.456Z\nExpiration Time: 2020-01-01T00:00:00.000Z\nNot Before: 2020-01-01T00:00:00.000Z\nResources:\n- https://docs.moralis.io/"); | ||
expect(result.result.profileId).toEqual('0x13e04b6cd6f84deef360a444499cbaccae717624f96cfa6dfe7cb250eced74eb'); | ||
}); | ||
|
||
it('should throw a 400 Error on invalid address', async () => { | ||
expect( | ||
AuthApi.requestMessage({ | ||
networkType: 'aptos', | ||
publicKey: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
statement: 'INVALID_ADDRESS', | ||
domain: 'defi.finance', | ||
chain: 'mainnet', | ||
address: 'some-address', | ||
uri: 'https://defi.finance/', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 15, | ||
}), | ||
).rejects.toThrowError('C0005] Invalid address provided'); | ||
}); | ||
|
||
it('should throw a 400 Error on multi error', async () => { | ||
expect( | ||
AuthApi.requestMessage({ | ||
networkType: 'aptos', | ||
statement: 'MULTI_ERROR', | ||
domain: 'defi.finance', | ||
chain: '1', | ||
address: 'some-address', | ||
publicKey: '0xfb2853744bb8afd58d9386d1856afd8e08de135019961dfa3a10d8c9bf83b99d', | ||
uri: 'finance', | ||
expirationTime: '2020-01-01T00:00:00.000Z', | ||
notBefore: '2020-01-01T00:00:00.000Z', | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 15, | ||
}), | ||
).rejects.toThrowError('C0005] Invalid address provided'); | ||
}); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/auth/integration/test/verifyAptosChallenge.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Auth } from '../../src/Auth'; | ||
import { cleanAuth, setupAuth } from '../setup'; | ||
|
||
describe('verifyAptosChallenge', () => { | ||
let AuthApi: Auth; | ||
|
||
beforeAll(() => { | ||
AuthApi = setupAuth(); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanAuth(); | ||
}); | ||
|
||
describe('Verify Aptos Challenge', () => { | ||
it('should verify aptos challenge successfully', async () => { | ||
const result = await AuthApi.verify({ | ||
networkType: 'aptos', | ||
message: 'VALID_RESPONSE', | ||
signature: '2pH9DqD5rve2qV4yBDshcAjWd2y8TqMx8BPb7f3KoNnuLEhE5JwjruYi4jaFaD4HN6wriLz2Vdr32kRBAJmHcyny', | ||
}); | ||
expect(result).toBeDefined(); | ||
expect(result.result.id).toBeDefined(); | ||
expect(result.result.profileId).toBeDefined(); | ||
}); | ||
|
||
it('should throw a 400 Error on invalid signature', async () => { | ||
expect( | ||
AuthApi.verify({ | ||
networkType: 'aptos', | ||
message: 'INVALID_SIGNATURE', | ||
signature: 'some-signature', | ||
}), | ||
).rejects.toThrowError('[C0006] Request failed, Bad Request(400): INVALID_SIGNATURE: some-signature'); | ||
}); | ||
|
||
it('should throw a 400 Error on multi error', async () => { | ||
expect( | ||
AuthApi.verify({ | ||
networkType: 'aptos', | ||
message: '', | ||
signature: 'some-signature', | ||
}), | ||
).rejects.toThrowError('[C0006] Request failed, Bad Request(400): MULTI_ERROR'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.