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 26, 2024
1 parent d3f440f commit d2702bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 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
16 changes: 14 additions & 2 deletions utils/arweave/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Provider {
fileSize = 0
ipfsHash = ''
txHash = ''
token = ''

constructor({
publicKey,
Expand Down Expand Up @@ -53,6 +54,10 @@ class Provider {
return this.pubKey
}

getArweaveRegisterApiToken() {
return this.token
}

getSigner = () => ({
getAddress: () => this.pubKey?.toString(),
_signTypedData: async (
Expand All @@ -69,7 +74,8 @@ class Provider {
ipfsHash: this.ipfsHash,
txHash: this.txHash,
})
const { signature } = await res.data
const { signature, token } = await res.data
this.token = token;
const bSig = Buffer.from(signature, 'base64')
// pad & convert so it's in the format the signer expects to have to convert from.
const pad = Buffer.concat([
Expand Down Expand Up @@ -123,6 +129,7 @@ async function getBundler({
'matic',
p,
)
bundlr.getArweaveApiToken = p.getArweaveRegisterApiToken.bind(p)
await bundlr.ready()
return bundlr
}
Expand All @@ -148,7 +155,8 @@ 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 tags = [
Expand All @@ -160,13 +168,17 @@ export async function uploadSingleFileToBundlr(
];
if (fileType) tags.push({ name: 'Content-Type', value: fileType })
const response = await bundler.upload(file, { tags })
const arweaveAPIToken = bundler.getArweaveApiToken();
const arweaveId = response.id;
if (arweaveId) {
await axios.post(API_POST_ARWEAVE_V2_REGISTER, {
fileSize,
ipfsHash,
txHash,
arweaveId,
},{
headers: { Authorization: token ? `Bearer ${token}` : '' },
params: { token: arweaveAPIToken },
});
}
return arweaveId;
Expand Down

0 comments on commit d2702bb

Please sign in to comment.