Skip to content

Commit

Permalink
[NFT Mirroring] set up CORS policy
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored Mar 12, 2022
1 parent 70515b3 commit 3768fa0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
56 changes: 40 additions & 16 deletions nft_mirror/oracle_server/src/airdrop/airdropService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

interface AirdropClaimMessage {
import { ValidateError } from 'tsoa';

interface AirdropClaimInfo {
/**
* Name of the source chain
* @pattern ethereum
Expand Down Expand Up @@ -31,21 +33,15 @@ interface AirdropClaimMessage {
*
*
* @example {
* "message": {
* "source_chain": "ethereum",
* "source_contract_address": "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D",
* "source_token_id": "101",
* "source_owner_address": "0x529f501ceb3ab599274a38f2aee41a7eba1fcead",
* "destination_sui_address": "0x10"
* },
* "signature": "0x21fbf0696d5e0aa2ef41a2b4ffb623bcaf070461d61cf7251c74161f82fec3a4370854bc0a34b3ab487c1bc021cd318c734c51ae29374f2beb0e6f2dd49b4bf41c"
* "wallet_message": "{\"domain\":{\"chainId\":1,\"name\":\"SuiDrop\",\"version\":\"1\"},\"message\":{\"source_chain\":\"ethereum\",\"source_contract_address\":\"0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D\",\"source_token_id\":\"8937\",\"source_owner_address\":\"0x09dbc4a902199bbe7f7ec29b3714731786f2e878\",\"destination_sui_address\":\"0xa5e6dbcf33730ace6ec8b400ff4788c1f150ff7e\"},\"primaryType\":\"ClaimRequest\",\"types\":{\"EIP712Domain\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"},{\"name\":\"chainId\",\"type\":\"uint256\"}],\"ClaimRequest\":[{\"name\":\"source_chain\",\"type\":\"string\"},{\"name\":\"source_contract_address\",\"type\":\"string\"},{\"name\":\"source_token_id\",\"type\":\"string\"},{\"name\":\"source_owner_address\",\"type\":\"string\"},{\"name\":\"destination_sui_address\",\"type\":\"string\"}]}}",
* "signature": "abc"
* }
*/
export interface AirdropClaimRequest {
/**
* unsigned message
* An EIP-712 compliant message
*/
message: AirdropClaimMessage;
wallet_message: string;
/**
* Digital signature of `message` signed by the private key of `source_owner_address`
*/
Expand Down Expand Up @@ -87,12 +83,40 @@ export class AirdropService {
public async claim(
claimMessage: AirdropClaimRequest
): Promise<AirdropClaimResponse> {
const message = claimMessage.message;
const { wallet_message } = claimMessage;
const data = JSON.parse(wallet_message);
const claimInfo = this.parseClaimInfo(data);
return {
source_chain: message.source_chain,
source_contract_address: message.source_contract_address,
source_token_id: message.source_token_id,
sui_explorer_link: 'www.mystenlabs.com',
source_chain: claimInfo.source_chain,
source_contract_address: claimInfo.source_contract_address,
source_token_id: claimInfo.source_token_id,
sui_explorer_link:
'https://djgd7fpxio1yh.cloudfront.net/objects/7bc832ec31709638cd8d9323e90edf332gff4389',
};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
parseClaimInfo(data: any): AirdropClaimInfo {
if (isAirdropClaimInfo(data['message'])) {
return data['message'] as AirdropClaimInfo;
}
throw new ValidateError(
{ messages: { message: 'Wrong format', value: data['message'] } },
'Wrong format for wallet message'
);
}
}

/**
* User Defined Type Guard for AirdropClaimInfo
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isAirdropClaimInfo(arg: any): arg is AirdropClaimInfo {
return (
arg.source_chain &&
arg.source_contract_address &&
arg.source_token_id &&
arg.source_owner_address &&
arg.destination_sui_address
);
}
12 changes: 12 additions & 0 deletions nft_mirror/oracle_server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ const app: Application = express();
app.use(morgan('tiny'));
app.use(express.static('public'));

// Set up CORS Policy
// TODO: update the allowed origin before launch
app.use(function (_, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});

// Use body parser to read sent json payloads
app.use(
bodyParser.urlencoded({
Expand Down Expand Up @@ -61,6 +72,7 @@ app.use(function errorHandler(
});
}
if (err instanceof Error) {
console.warn('Internal Server Error:', err);
return res.status(500).json({
message: 'Internal Server Error',
});
Expand Down

0 comments on commit 3768fa0

Please sign in to comment.