From 763803e10626202fc9c32cd3f9df6964bc564bec Mon Sep 17 00:00:00 2001 From: Mark Paul Date: Thu, 22 Feb 2024 19:17:00 +1100 Subject: [PATCH] feature: #100 viewData and viewDataViaMVXNativeAuth supports new optional asDeputyOnAppointerAddr --- package.json | 2 +- src/common/utils.ts | 26 +++++++++++++++++++++++++- src/datanft.ts | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 739a253..5a48954 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@itheum/sdk-mx-data-nft", - "version": "2.6.3", + "version": "2.7.0-alpha1", "description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain", "main": "out/index.js", "types": "out/index.d.js", diff --git a/src/common/utils.ts b/src/common/utils.ts index 0a40ae0..7ef37d1 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -166,6 +166,7 @@ export function validateSpecificParamsViewData(params: { fwdHeaderMapLookup?: any; nestedIdxToStream?: number | undefined; _fwdHeaderMapLookupMustContainBearerAuthHeader?: boolean | undefined; + asDeputyOnAppointerAddr?: string | undefined; _mandatoryParamsList: string[]; // a pure JS fallback way to validate mandatory params, as typescript rules for mandatory can be bypassed by client app }): { allPassed: boolean; @@ -390,6 +391,28 @@ export function validateSpecificParamsViewData(params: { } } + // asDeputyOnAppointerAddr test + let asDeputyOnAppointerAddrIsValid = true; + + if ( + params.asDeputyOnAppointerAddr !== undefined || + params._mandatoryParamsList.includes('asDeputyOnAppointerAddr') + ) { + asDeputyOnAppointerAddrIsValid = false; + + if ( + params.asDeputyOnAppointerAddr !== undefined && + typeof params.asDeputyOnAppointerAddr === 'string' && + params.asDeputyOnAppointerAddr.trim() !== '' && + params.asDeputyOnAppointerAddr.length > 10 + ) { + asDeputyOnAppointerAddrIsValid = true; + } else { + validationMessages += + '[asDeputyOnAppointerAddr needs to be a multiversx smart contract address in an string. e.g. erd1qqqqqqqqqqqqqpgqd2y9zvaehkn4arsjwxp8vs3rjmdwyffafsxsgjkdw8]'; + } + } + if ( !signedMessageValid || !signableMessageValid || @@ -399,7 +422,8 @@ export function validateSpecificParamsViewData(params: { !fwdHeaderMapLookupIsValid || !mvxNativeAuthMaxExpirySecondsValid || !mvxNativeAuthOriginsIsValid || - !nestedIdxToStreamValid + !nestedIdxToStreamValid || + !asDeputyOnAppointerAddrIsValid ) { allPassed = false; } diff --git a/src/datanft.ts b/src/datanft.ts index 3799187..bb0f356 100644 --- a/src/datanft.ts +++ b/src/datanft.ts @@ -302,6 +302,7 @@ export class DataNft implements DataNftType { * @param fwdHeaderKeys [optional] Forward only selected headers to the Origin Data Stream server. Has priority over fwdAllHeaders param. A comma separated lowercase string with less than 5 items. e.g. cookie,authorization * @param fwdHeaderMapLookup [optional] Used with fwdHeaderKeys to set a front-end client side lookup map of headers the SDK uses to setup the forward. e.g. { cookie : "xyz", authorization : "Bearer zxy" }. Note that these are case-sensitive and need to match fwdHeaderKeys exactly. * @param nestedIdxToStream [optional] If you are accessing a "nested stream", this is the index of the nested item you want drill into and fetch + * @param asDeputyOnAppointerAddr [optional] Put caller in the "deputy persona" of this deputy appointer address (which should be a smart contract that holds the Data NFT Id) */ async viewData(p: { signedMessage: string; @@ -313,6 +314,7 @@ export class DataNft implements DataNftType { [key: string]: any; }; nestedIdxToStream?: number; + asDeputyOnAppointerAddr?: string; }): Promise { DataNft.ensureNetworkConfigSet(); if (!this.dataMarshal) { @@ -334,6 +336,7 @@ export class DataNft implements DataNftType { fwdHeaderKeys: p.fwdHeaderKeys, fwdHeaderMapLookup: p.fwdHeaderMapLookup, nestedIdxToStream: p.nestedIdxToStream, + asDeputyOnAppointerAddr: p.asDeputyOnAppointerAddr, _mandatoryParamsList: ['signedMessage', 'signableMessage'] }); @@ -409,6 +412,10 @@ export class DataNft implements DataNftType { }); } } + + if (typeof p.asDeputyOnAppointerAddr !== 'undefined') { + url += `&asDeputyOnAppointerAddr=${p.asDeputyOnAppointerAddr}`; + } // E: append optional params... const response = await fetch(url, fetchConfig); @@ -451,6 +458,7 @@ export class DataNft implements DataNftType { * @param fwdAllHeaders [optional] Forward all request headers to the Origin Data Stream server. * @param stream [optional] Instead of auto-downloading if possible, request if data should always be streamed or not.i.e true=stream, false/undefined=default behavior * @param nestedIdxToStream [optional] If you are accessing a "nested stream", this is the index of the nested item you want drill into and fetch + * @param asDeputyOnAppointerAddr [optional] Put caller in the "deputy persona" of this deputy appointer address (which should be a smart contract that holds the Data NFT Id) */ async viewDataViaMVXNativeAuth(p: { mvxNativeAuthOrigins: string[]; @@ -462,6 +470,7 @@ export class DataNft implements DataNftType { fwdAllHeaders?: boolean; stream?: boolean; nestedIdxToStream?: number; + asDeputyOnAppointerAddr?: string; }): Promise { try { // S: run any format specific validation @@ -473,6 +482,7 @@ export class DataNft implements DataNftType { fwdAllHeaders: p.fwdAllHeaders, stream: p.stream, nestedIdxToStream: p.nestedIdxToStream, + asDeputyOnAppointerAddr: p.asDeputyOnAppointerAddr, _fwdHeaderMapLookupMustContainBearerAuthHeader: true, _mandatoryParamsList: [ 'mvxNativeAuthOrigins', @@ -570,6 +580,10 @@ export class DataNft implements DataNftType { }); } } + + if (typeof p.asDeputyOnAppointerAddr !== 'undefined') { + url += `&asDeputyOnAppointerAddr=${p.asDeputyOnAppointerAddr}`; + } // E: append optional params... const response = await fetch(url, fetchConfig);