From bd898149a625415a744ffa6f8c43cfb2493edf98 Mon Sep 17 00:00:00 2001 From: William Chong Date: Sun, 27 Oct 2024 17:07:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BD=EF=B8=8F=20Add=20auth=20to=20arwea?= =?UTF-8?q?ve=20upload=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/IscnUploadForm.vue | 4 ++++ constant/index.ts | 2 ++ pages/nft/iscn/_iscnId.vue | 4 ++++ utils/arweave/v2.ts | 25 +++++++++++++++++++++---- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/components/IscnUploadForm.vue b/components/IscnUploadForm.vue index 306737dd..480c3ecd 100644 --- a/components/IscnUploadForm.vue +++ b/components/IscnUploadForm.vue @@ -262,6 +262,7 @@ import { ISCNRecordWithID } from '~/utils/cosmos/iscn/iscn.type' const iscnModule = namespace('iscn') const walletModule = namespace('wallet') +const bookApiModule = namespace('book-api') type UploadStatus = '' | 'loading' | 'signing' | 'uploading'; const MODE = { @@ -286,6 +287,7 @@ export default class IscnUploadForm extends Vue { @walletModule.Getter('getSigner') signer!: OfflineSigner | null @walletModule.Action('initIfNecessary') initIfNecessary!: () => Promise @walletModule.Getter('getWalletAddress') address!: string + @bookApiModule.Getter('getToken') getToken!: string isImage: boolean = false ipfsURL: string = '' @@ -848,6 +850,7 @@ export default class IscnUploadForm extends Vue { ipfsHash: tempRecord.ipfsHash, fileType: tempRecord.fileType as string, txHash: tempRecord.transactionHash, + token: this.getToken, }); if (arweaveId) { const uploadedData = this.sentArweaveTransactionInfo.get(record.ipfsHash) || {}; @@ -881,6 +884,7 @@ export default class IscnUploadForm extends Vue { ipfsHash: file.ipfsHash, fileType: file.fileType, txHash, + token: this.getToken, }) } diff --git a/constant/index.ts b/constant/index.ts index e3fdf8c4..57503cfc 100644 --- a/constant/index.ts +++ b/constant/index.ts @@ -114,6 +114,8 @@ export const NFT_BOOK_PRESS_URL = IS_TESTNET ? 'https://likecoin-nft-book-press- export const SIGN_AUTHORIZATION_PERMISSIONS = [ 'profile', + 'read:iscn', + 'write:iscn', 'read:nftbook', 'write:nftbook', 'read:nftcollection', diff --git a/pages/nft/iscn/_iscnId.vue b/pages/nft/iscn/_iscnId.vue index 414d4acc..7abebfdf 100644 --- a/pages/nft/iscn/_iscnId.vue +++ b/pages/nft/iscn/_iscnId.vue @@ -151,6 +151,7 @@ import { estimateBundlrFilePrice, uploadSingleFileToBundlr } from '~/utils/arwea const iscnModule = namespace('iscn') const walletModule = namespace('wallet') +const bookApiModule = namespace('book-api') export enum ErrorType { INSUFFICIENT_BALANCE = 'INSUFFICIENT_BALANCE', @@ -226,6 +227,8 @@ export default class NFTMintPage extends Vue { @walletModule.Getter('getWalletAddress') address!: string @walletModule.Getter('getSigner') signer!: OfflineSigner | null + @bookApiModule.Getter('getToken') getToken!: string + platform = this.$route.query.platform as string || '' isNewsPress = !!this.$route.query.news_press && this.$route.query.news_press !== '0' @@ -818,6 +821,7 @@ export default class NFTMintPage extends Vue { ipfsHash: await this.getOgImageIpfsHash(), fileType: this.ogImageBlob.type, txHash: this.ogImageArweaveFeeTxHash, + token: this.getToken, }); logTrackerEvent(this, 'IscnMintNFT', 'SubmitToArweaveSuccess', arweaveId as string, 1); this.ogImageArweaveId = arweaveId as string diff --git a/utils/arweave/v2.ts b/utils/arweave/v2.ts index c469081b..94421a40 100644 --- a/utils/arweave/v2.ts +++ b/utils/arweave/v2.ts @@ -13,22 +13,26 @@ class Provider { fileSize = 0 ipfsHash = '' txHash = '' + token = '' constructor({ publicKey, fileSize, ipfsHash, txHash, + token, }: { publicKey: string fileSize: number ipfsHash: string txHash: string + token: string }) { this.pubKey = Buffer.from(publicKey, 'base64'); this.fileSize = fileSize this.ipfsHash = ipfsHash this.txHash = txHash + this.token = token } setLikeCoinTxInfo({ @@ -53,6 +57,10 @@ class Provider { return this.pubKey } + setAuthToken(token: string) { + this.token = token + } + getSigner = () => ({ getAddress: () => this.pubKey?.toString(), _signTypedData: async ( @@ -68,6 +76,8 @@ class Provider { fileSize: this.fileSize, ipfsHash: this.ipfsHash, txHash: this.txHash, + }, { + headers: { Authorization: this.token ? `Bearer ${this.token}` : '' }, }) const { signature } = await res.data const bSig = Buffer.from(signature, 'base64') @@ -92,14 +102,16 @@ async function getProvider({ fileSize, ipfsHash, txHash, + token, }: { fileSize: number ipfsHash: string txHash: string + token: string }) { const { data } = await axios.get(API_GET_ARWEAVE_V2_PUBLIC_KEY) const { publicKey } = data - const provider = new Provider({ publicKey, fileSize, ipfsHash, txHash }) + const provider = new Provider({ publicKey, fileSize, ipfsHash, txHash, token }) return provider } @@ -107,15 +119,17 @@ async function getBundler({ fileSize, ipfsHash, txHash, + token, }: { fileSize: number ipfsHash: string txHash: string + token: string }) { if (!WebBundlr) { WebBundlr = (await import('@bundlr-network/client')).default; } - const p = await getProvider({ fileSize, ipfsHash, txHash }) + const p = await getProvider({ fileSize, ipfsHash, txHash, token }) const bundlr = new WebBundlr( IS_TESTNET ? 'https://devnet.irys.xyz' @@ -148,9 +162,10 @@ export async function uploadSingleFileToBundlr( fileSize, ipfsHash, txHash, - }: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string }, + token, + }: { fileSize: number; fileType?: string, ipfsHash: string; txHash: string, token: string }, ) { - const bundler = await getBundler({ fileSize, ipfsHash, txHash }) + const bundler = await getBundler({ fileSize, ipfsHash, txHash, token }) const tags = [ { name: 'App-Name', value: 'app.like.co' }, { name: 'App-Version', value: '2.0' }, @@ -167,6 +182,8 @@ export async function uploadSingleFileToBundlr( ipfsHash, txHash, arweaveId, + },{ + headers: { Authorization: token ? `Bearer ${token}` : '' }, }); } return arweaveId;