Skip to content

Commit

Permalink
👽️ Add auth to arweave upload api
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Oct 31, 2024
1 parent 1411098 commit b4d8f90
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions components/IscnUploadForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -286,6 +287,7 @@ export default class IscnUploadForm extends Vue {
@walletModule.Getter('getSigner') signer!: OfflineSigner | null
@walletModule.Action('initIfNecessary') initIfNecessary!: () => Promise<any>
@walletModule.Getter('getWalletAddress') address!: string
@bookApiModule.Getter('getToken') getToken!: string
isImage: boolean = false
ipfsURL: string = ''
Expand Down Expand Up @@ -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) || {};
Expand Down Expand Up @@ -881,6 +884,7 @@ export default class IscnUploadForm extends Vue {
ipfsHash: file.ipfsHash,
fileType: file.fileType,
txHash,
token: this.getToken,
})
}
Expand Down
2 changes: 2 additions & 0 deletions constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions pages/nft/iscn/_iscnId.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions utils/arweave/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -53,6 +57,10 @@ class Provider {
return this.pubKey
}

setAuthToken(token: string) {
this.token = token
}

getSigner = () => ({
getAddress: () => this.pubKey?.toString(),
_signTypedData: async (
Expand All @@ -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')
Expand All @@ -92,30 +102,34 @@ 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
}

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'
Expand Down Expand Up @@ -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' },
Expand All @@ -167,6 +182,8 @@ export async function uploadSingleFileToBundlr(
ipfsHash,
txHash,
arweaveId,
},{
headers: { Authorization: token ? `Bearer ${token}` : '' },
});
}
return arweaveId;
Expand Down

0 comments on commit b4d8f90

Please sign in to comment.