Skip to content

Commit

Permalink
Merge pull request #223 from OriginTrail/update-assertion-tools
Browse files Browse the repository at this point in the history
Update assertion tools
  • Loading branch information
Mihajlo-Pavlovic authored Feb 6, 2025
2 parents c6e74ea + 90b4407 commit 3d775c4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
80 changes: 44 additions & 36 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ function deriveUAL(blockchain, contract, tokenId) {
return `did:dkg:${blockchain.toLowerCase()}/${contract.toLowerCase()}/${tokenId}`;
}

// TODO: Fix this to work with KC & KA UAL
function resolveUAL(ual) {
const segments = ual.split(':');
const argsString = segments.length === 3 ? segments[2] : `${segments[2]}:${segments[3]}`;
const args = argsString.split('/');

if (args.length !== 3) {
if (!(args.length === 3 || args.length === 4)) {
throw new Error(`UAL doesn't have correct format: ${ual}`);
}

return {
blockchain: args[0],
contract: args[1],
tokenId: parseInt(args[2], 10),
knowledgeCollectionId: parseInt(args[2], 10),
...(args[3] !== undefined ? { tokenId: parseInt(args[3], 10) } : {}),
};
}

Expand Down Expand Up @@ -853,19 +853,18 @@ class AssetOperationsManager {
* @param {Object} [options={}] - Additional options for asset transfer.
* @returns {Object} Object containing UAL, owner's address and operation status.
*/
// TODO: Update this for v8
async transfer(UAL, newOwner, options = {}) {
const blockchain = this.inputService.getBlockchain(options);

this.validationService.validateAssetTransfer(UAL, newOwner, blockchain);

const { tokenId } = resolveUAL(UAL);
const receipt = await this.blockchainService.transferAsset(tokenId, newOwner, blockchain);
const owner = await this.blockchainService.getAssetOwner(tokenId, blockchain);
const { knowledgeCollectionId, tokenId } = resolveUAL(UAL);
const assetId = (knowledgeCollectionId - 1) * 1_000_000 + tokenId;
const receipt = await this.blockchainService.transferAsset(assetId, newOwner, blockchain);
// const owner = await this.blockchainService.getAssetOwner(tokenId, blockchain);

return {
UAL,
owner,
operation: receipt,
};
}
Expand Down Expand Up @@ -3041,30 +3040,30 @@ class BlockchainServiceBase {
}

// TODO: Fix this
async burnAsset(tokenId, blockchain) {
return this.executeContractFunction('ContentAsset', 'burnAsset', [tokenId], blockchain);
}
// async burnAsset(tokenId, blockchain) {
// return this.executeContractFunction('ContentAsset', 'burnAsset', [tokenId], blockchain);
// }

// TODO: Fix this
async getAssertionSize(assertionId, blockchain) {
return this.callContractFunction(
'AssertionStorage',
'getAssertionSize',
[assertionId],
blockchain,
);
}
// async getAssertionSize(assertionId, blockchain) {
// return this.callContractFunction(
// 'AssertionStorage',
// 'getAssertionSize',
// [assertionId],
// blockchain,
// );
// }

// Paranets operations

async registerParanet(requestData, blockchain) {
return this.executeContractFunction(
'Paranet',
'registerParanet',
Object.values(requestData),
blockchain,
);
}
// async registerParanet(requestData, blockchain) {
// return this.executeContractFunction(
// 'Paranet',
// 'registerParanet',
// Object.values(requestData),
// blockchain,
// );
// }

// async addParanetCuratedNodes(requestData, blockchain) {
// return this.executeContractFunction(
Expand Down Expand Up @@ -3696,12 +3695,11 @@ class BrowserBlockchainService extends BlockchainServiceBase {
return this.account;
}

// TODO: Update this for V8
async transferAsset(tokenId, to, blockchain) {
return this.executeContractFunction(
'ContentAssetStorage',
'transferFrom',
[await this.getAccount(), to, tokenId],
'KnowledgeCollectionStorage',
'safeTransferFrom',
[await this.getAccount(), to, tokenId, 1, '0x'],
blockchain,
);
}
Expand Down Expand Up @@ -3834,12 +3832,11 @@ class NodeBlockchainService extends BlockchainServiceBase {
return receipt;
}

// TODO: Fix this
async transferAsset(tokenId, to, blockchain) {
return this.executeContractFunction(
'ContentAssetStorage',
'transferFrom',
[blockchain.publicKey, to, tokenId],
'KnowledgeCollectionStorage',
'safeTransferFrom',
[blockchain.publicKey, to, tokenId, 1, '0x'],
blockchain,
);
}
Expand Down Expand Up @@ -3990,7 +3987,7 @@ class ValidationService {
}

validateAssetTransfer(UAL, newOwner, blockchain) {
this.validateUAL(UAL);
this.validateKAUAL(UAL);
this.validateNewOwner(newOwner);
this.validateBlockchain(blockchain);
}
Expand Down Expand Up @@ -4256,6 +4253,17 @@ class ValidationService {
return true;
}

validateKAUAL(ual) {
this.validateRequiredParam('UAL', ual);
this.validateParamType('UAL', ual, 'string');

const segments = ual.split(':');
const argsString = segments.length === 3 ? segments[2] : `${segments[2]}:${segments[3]}`;
const args = argsString.split('/');
if (args?.length !== 4) throw Error('Invalid UAL.');
return true;
}

validateStateIndex(stateIndex) {
this.validateRequiredParam('stateIndex', stateIndex);
this.validateParamType('stateIndex', stateIndex, 'number');
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dkg.js",
"version": "8.0.5",
"version": "8.0.6",
"description": "Javascript library for interaction with the OriginTrail Decentralized Knowledge Graph",
"main": "index.js",
"exports": {
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://github.com/OriginTrail/dkg.js/tree/main#readme",
"dependencies": {
"assertion-tools": "^8.0.0-gamma.2",
"assertion-tools": "^8.0.1",
"axios": "^0.27.2",
"dkg-evm-module": "^8.0.0",
"dotenv": "^16.4.7",
Expand Down
2 changes: 1 addition & 1 deletion services/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function resolveUAL(ual) {
blockchain: args[0],
contract: args[1],
knowledgeCollectionId: parseInt(args[2], 10),
tokenId: parseInt(args[3], 10),
...(args[3] !== undefined ? { tokenId: parseInt(args[3], 10) } : {}),
};
}

Expand Down

0 comments on commit 3d775c4

Please sign in to comment.