@trustvc/trustvc 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/core/endorsement-chain/fetchEscrowTransfer.d.mts +8 -0
  2. package/dist/core/endorsement-chain/fetchEscrowTransfer.d.ts +8 -0
  3. package/dist/core/endorsement-chain/fetchEscrowTransfer.js +167 -0
  4. package/dist/core/endorsement-chain/fetchTokenTransfer.d.mts +8 -0
  5. package/dist/core/endorsement-chain/fetchTokenTransfer.d.ts +8 -0
  6. package/dist/core/endorsement-chain/fetchTokenTransfer.js +58 -0
  7. package/dist/core/endorsement-chain/helpers.d.mts +11 -0
  8. package/dist/core/endorsement-chain/helpers.d.ts +11 -0
  9. package/dist/core/endorsement-chain/helpers.js +99 -0
  10. package/dist/core/endorsement-chain/index.d.mts +11 -0
  11. package/dist/core/endorsement-chain/index.d.ts +11 -0
  12. package/dist/core/endorsement-chain/index.js +47 -0
  13. package/dist/core/endorsement-chain/retrieveEndorsementChain.d.mts +8 -0
  14. package/dist/core/endorsement-chain/retrieveEndorsementChain.d.ts +8 -0
  15. package/dist/core/endorsement-chain/retrieveEndorsementChain.js +43 -0
  16. package/dist/core/endorsement-chain/types.d.mts +50 -0
  17. package/dist/core/endorsement-chain/types.d.ts +50 -0
  18. package/dist/core/endorsement-chain/types.js +2 -0
  19. package/dist/core/endorsement-chain/useEndorsementChain.d.mts +14 -0
  20. package/dist/core/endorsement-chain/useEndorsementChain.d.ts +14 -0
  21. package/dist/core/endorsement-chain/useEndorsementChain.js +103 -0
  22. package/dist/core/index.d.mts +11 -0
  23. package/dist/core/index.d.ts +11 -0
  24. package/dist/core/index.js +7 -0
  25. package/dist/esm/core/endorsement-chain/fetchEscrowTransfer.js +164 -0
  26. package/dist/esm/core/endorsement-chain/fetchTokenTransfer.js +56 -0
  27. package/dist/esm/core/endorsement-chain/helpers.js +94 -0
  28. package/dist/esm/core/endorsement-chain/index.js +6 -0
  29. package/dist/esm/core/endorsement-chain/retrieveEndorsementChain.js +41 -0
  30. package/dist/esm/core/endorsement-chain/types.js +1 -0
  31. package/dist/esm/core/endorsement-chain/useEndorsementChain.js +98 -0
  32. package/dist/esm/core/index.js +1 -0
  33. package/dist/index.d.mts +10 -0
  34. package/dist/index.d.ts +10 -0
  35. package/dist/token-registry-v5/typedContractMethod.d.mts +1 -1
  36. package/dist/token-registry-v5/typedContractMethod.d.ts +1 -1
  37. package/package.json +1 -1
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ var ethers = require('ethers');
4
+ var decrypt = require('../decrypt');
5
+ var fetchEscrowTransfer = require('../endorsement-chain/fetchEscrowTransfer');
6
+ var fetchTokenTransfer = require('../endorsement-chain/fetchTokenTransfer');
7
+ var helpers = require('../endorsement-chain/helpers');
8
+ var retrieveEndorsementChain = require('../endorsement-chain/retrieveEndorsementChain');
9
+ var contracts = require('../../token-registry-v4/contracts');
10
+ var supportInterfaceIds = require('../../token-registry-v4/supportInterfaceIds');
11
+ var supportInterfaceIds$1 = require('../../token-registry-v5/supportInterfaceIds');
12
+
13
+ var __defProp = Object.defineProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ const TitleEscrowInterface = {
16
+ V4: supportInterfaceIds.supportInterfaceIds.TitleEscrow,
17
+ V5: supportInterfaceIds$1.supportInterfaceIds.TitleEscrow
18
+ };
19
+ const fetchTitleEscrowFactoryAddress = /* @__PURE__ */ __name(async (tokenRegistry) => {
20
+ return tokenRegistry.titleEscrowFactory();
21
+ }, "fetchTitleEscrowFactoryAddress");
22
+ const resolveTitleEscrowAddress = /* @__PURE__ */ __name(async (titleEscrowFactoryContract, tokenRegistryAddress, tokenId) => {
23
+ try {
24
+ return await titleEscrowFactoryContract.getEscrowAddress(tokenRegistryAddress, tokenId);
25
+ } catch {
26
+ return titleEscrowFactoryContract.getAddress(tokenRegistryAddress, tokenId);
27
+ }
28
+ }, "resolveTitleEscrowAddress");
29
+ const getTitleEscrowAddress = /* @__PURE__ */ __name(async (tokenRegistryAddress, tokenId, provider) => {
30
+ const tokenRegistryAbi = [
31
+ "function titleEscrowFactory() external view returns (address)",
32
+ "function ownerOf(uint256 tokenId) view returns (address)"
33
+ ];
34
+ const tokenRegistry = new ethers.ethers.Contract(tokenRegistryAddress, tokenRegistryAbi, provider);
35
+ const titleEscrowOwner = await tokenRegistry.ownerOf(tokenId);
36
+ const BURN_ADDRESS = "0x000000000000000000000000000000000000dEaD";
37
+ const isInactiveEscrow = [BURN_ADDRESS, tokenRegistryAddress].map((address) => address.toLowerCase()).includes(titleEscrowOwner.toLowerCase());
38
+ if (!isInactiveEscrow) return titleEscrowOwner;
39
+ const titleEscrowFactoryAddress = await fetchTitleEscrowFactoryAddress(tokenRegistry);
40
+ const titleEscrowFactoryContract = new ethers.ethers.Contract(
41
+ titleEscrowFactoryAddress,
42
+ [
43
+ "function getAddress(address, uint256) view returns (address)",
44
+ "function getEscrowAddress(address, uint256) view returns (address)"
45
+ ],
46
+ provider
47
+ );
48
+ return resolveTitleEscrowAddress(titleEscrowFactoryContract, tokenRegistryAddress, tokenId);
49
+ }, "getTitleEscrowAddress");
50
+ const checkSupportsInterface = /* @__PURE__ */ __name(async (titleEscrowAddress, interfaceId, provider) => {
51
+ try {
52
+ const titleEscrowAbi = [
53
+ "function supportsInterface(bytes4 interfaceId) external view returns (bool)"
54
+ ];
55
+ const titleEscrowContract = new ethers.ethers.Contract(titleEscrowAddress, titleEscrowAbi, provider);
56
+ return await titleEscrowContract.supportsInterface(interfaceId);
57
+ } catch {
58
+ return false;
59
+ }
60
+ }, "checkSupportsInterface");
61
+ const isTitleEscrowVersion = /* @__PURE__ */ __name(async (versionInterface, tokenRegistryAddress, tokenId, provider) => {
62
+ try {
63
+ const titleEscrowAddress = await getTitleEscrowAddress(tokenRegistryAddress, tokenId, provider);
64
+ return await checkSupportsInterface(titleEscrowAddress, versionInterface, provider);
65
+ } catch {
66
+ return false;
67
+ }
68
+ }, "isTitleEscrowVersion");
69
+ const fetchEndorsementChain = /* @__PURE__ */ __name(async (tokenRegistry, tokenId, provider, keyId) => {
70
+ if (!tokenRegistry || !tokenId || !provider) {
71
+ throw new Error("Missing required dependencies");
72
+ }
73
+ const [isV4, isV5] = await Promise.all([
74
+ isTitleEscrowVersion(TitleEscrowInterface.V4, tokenRegistry, tokenId, provider),
75
+ isTitleEscrowVersion(TitleEscrowInterface.V5, tokenRegistry, tokenId, provider)
76
+ ]);
77
+ if (!isV4 && !isV5) {
78
+ throw new Error("Only Token Registry V4/V5 is supported");
79
+ }
80
+ const titleEscrowAddress = await getTitleEscrowAddress(tokenRegistry, tokenId, provider);
81
+ let transferEvents = [];
82
+ if (isV4) {
83
+ const tokenRegistryContract = contracts.TradeTrustToken__factory.connect(tokenRegistry, provider);
84
+ const [tokenLogs, titleEscrowLogs] = await Promise.all([
85
+ fetchTokenTransfer.fetchTokenTransfers(tokenRegistryContract, tokenId),
86
+ fetchEscrowTransfer.fetchEscrowTransfersV4(provider, titleEscrowAddress)
87
+ ]);
88
+ transferEvents = helpers.mergeTransfersV4([...titleEscrowLogs, ...tokenLogs]);
89
+ } else if (isV5) {
90
+ const titleEscrowLogs = await fetchEscrowTransfer.fetchEscrowTransfersV5(provider, titleEscrowAddress);
91
+ transferEvents = helpers.mergeTransfersV5(titleEscrowLogs);
92
+ }
93
+ const endorsementChain = await retrieveEndorsementChain.getEndorsementChain(provider, transferEvents);
94
+ return isV4 ? endorsementChain : endorsementChain.map((event) => ({
95
+ ...event,
96
+ remark: event?.remark?.slice(2) ? decrypt.decrypt(event.remark.slice(2), keyId ?? "") : ""
97
+ }));
98
+ }, "fetchEndorsementChain");
99
+
100
+ exports.TitleEscrowInterface = TitleEscrowInterface;
101
+ exports.fetchEndorsementChain = fetchEndorsementChain;
102
+ exports.getTitleEscrowAddress = getTitleEscrowAddress;
103
+ exports.isTitleEscrowVersion = isTitleEscrowVersion;
@@ -1,5 +1,16 @@
1
1
  export { decrypt } from './decrypt.mjs';
2
2
  export { encrypt } from './encrypt.mjs';
3
3
  export { verifyDocument } from './verify.mjs';
4
+ export { fetchEscrowTransfersV4, fetchEscrowTransfersV5 } from './endorsement-chain/fetchEscrowTransfer.mjs';
5
+ export { fetchTokenTransfers } from './endorsement-chain/fetchTokenTransfer.mjs';
6
+ export { fetchEventTime, mergeTransfersV4, mergeTransfersV5, sortLogChain } from './endorsement-chain/helpers.mjs';
7
+ export { getEndorsementChain } from './endorsement-chain/retrieveEndorsementChain.mjs';
8
+ export { EndorsementChain, ParsedLog, TitleEscrowTransferEvent, TitleEscrowTransferEventType, TokenTransferEvent, TokenTransferEventType, TradeTrustTokenEventType, TransferBaseEvent, TransferEvent, TransferEventType, TypedEvent } from './endorsement-chain/types.mjs';
9
+ export { TitleEscrowInterface, fetchEndorsementChain, getTitleEscrowAddress, isTitleEscrowVersion } from './endorsement-chain/useEndorsementChain.mjs';
4
10
  import '@trustvc/w3c-vc';
5
11
  import '@tradetrust-tt/tt-verify/dist/types/src/types/core';
12
+ import 'ethers';
13
+ import '@tradetrust-tt/token-registry-v4/contracts';
14
+ import '@ethersproject/abstract-provider';
15
+ import 'ethers/lib/utils';
16
+ import '@ethersproject/providers';
@@ -1,5 +1,16 @@
1
1
  export { decrypt } from './decrypt.js';
2
2
  export { encrypt } from './encrypt.js';
3
3
  export { verifyDocument } from './verify.js';
4
+ export { fetchEscrowTransfersV4, fetchEscrowTransfersV5 } from './endorsement-chain/fetchEscrowTransfer.js';
5
+ export { fetchTokenTransfers } from './endorsement-chain/fetchTokenTransfer.js';
6
+ export { fetchEventTime, mergeTransfersV4, mergeTransfersV5, sortLogChain } from './endorsement-chain/helpers.js';
7
+ export { getEndorsementChain } from './endorsement-chain/retrieveEndorsementChain.js';
8
+ export { EndorsementChain, ParsedLog, TitleEscrowTransferEvent, TitleEscrowTransferEventType, TokenTransferEvent, TokenTransferEventType, TradeTrustTokenEventType, TransferBaseEvent, TransferEvent, TransferEventType, TypedEvent } from './endorsement-chain/types.js';
9
+ export { TitleEscrowInterface, fetchEndorsementChain, getTitleEscrowAddress, isTitleEscrowVersion } from './endorsement-chain/useEndorsementChain.js';
4
10
  import '@trustvc/w3c-vc';
5
11
  import '@tradetrust-tt/tt-verify/dist/types/src/types/core';
12
+ import 'ethers';
13
+ import '@tradetrust-tt/token-registry-v4/contracts';
14
+ import '@ethersproject/abstract-provider';
15
+ import 'ethers/lib/utils';
16
+ import '@ethersproject/providers';
@@ -3,6 +3,7 @@
3
3
  var decrypt = require('./decrypt');
4
4
  var encrypt = require('./encrypt');
5
5
  var verify = require('./verify');
6
+ var endorsementChain = require('./endorsement-chain');
6
7
 
7
8
 
8
9
 
@@ -24,3 +25,9 @@ Object.keys(verify).forEach(function (k) {
24
25
  get: function () { return verify[k]; }
25
26
  });
26
27
  });
28
+ Object.keys(endorsementChain).forEach(function (k) {
29
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
30
+ enumerable: true,
31
+ get: function () { return endorsementChain[k]; }
32
+ });
33
+ });
@@ -0,0 +1,164 @@
1
+ import { TitleEscrow__factory } from '../../token-registry-v4/contracts';
2
+ import { TitleEscrow__factory as TitleEscrow__factory$1 } from '../../token-registry-v5/contracts';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ const fetchEscrowTransfersV4 = /* @__PURE__ */ __name(async (provider, address) => {
7
+ const titleEscrowContract = TitleEscrow__factory.connect(address, provider);
8
+ const holderChangeLogsDeferred = await fetchHolderTransfers(titleEscrowContract, provider);
9
+ const ownerChangeLogsDeferred = await fetchOwnerTransfers(titleEscrowContract, provider);
10
+ const [holderChangeLogs, ownerChangeLogs] = await Promise.all([
11
+ holderChangeLogsDeferred,
12
+ ownerChangeLogsDeferred
13
+ ]);
14
+ return [...holderChangeLogs, ...ownerChangeLogs];
15
+ }, "fetchEscrowTransfersV4");
16
+ const fetchEscrowTransfersV5 = /* @__PURE__ */ __name(async (provider, address) => {
17
+ const titleEscrowContract = TitleEscrow__factory$1.connect(address, provider);
18
+ const holderChangeLogsDeferred = await fetchAllTransfers(titleEscrowContract);
19
+ return holderChangeLogsDeferred;
20
+ }, "fetchEscrowTransfersV5");
21
+ const getParsedLogs = /* @__PURE__ */ __name((logs, titleEscrow) => {
22
+ return logs.map((log) => {
23
+ if (!log.blockNumber) throw new Error("Block number not present");
24
+ return {
25
+ ...log,
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ ...titleEscrow.interface.parseLog(log)
28
+ };
29
+ });
30
+ }, "getParsedLogs");
31
+ const fetchOwnerTransfers = /* @__PURE__ */ __name(async (titleEscrowContract, provider) => {
32
+ const ownerChangeFilter = titleEscrowContract.filters.BeneficiaryTransfer(null, null);
33
+ const ownerChangeLogs = await provider.getLogs({ ...ownerChangeFilter, fromBlock: 0 });
34
+ const ownerChangeLogsParsed = getParsedLogs(ownerChangeLogs, titleEscrowContract);
35
+ return ownerChangeLogsParsed.map((event) => ({
36
+ type: "TRANSFER_BENEFICIARY",
37
+ owner: event.args.toBeneficiary,
38
+ blockNumber: event.blockNumber,
39
+ transactionHash: event.transactionHash,
40
+ transactionIndex: event.transactionIndex
41
+ }));
42
+ }, "fetchOwnerTransfers");
43
+ const fetchHolderTransfers = /* @__PURE__ */ __name(async (titleEscrowContract, provider) => {
44
+ const holderChangeFilter = titleEscrowContract.filters.HolderTransfer(null, null);
45
+ const holderChangeLogs = await provider.getLogs({ ...holderChangeFilter, fromBlock: 0 });
46
+ const holderChangeLogsParsed = getParsedLogs(holderChangeLogs, titleEscrowContract);
47
+ return holderChangeLogsParsed.map((event) => ({
48
+ type: "TRANSFER_HOLDER",
49
+ blockNumber: event.blockNumber,
50
+ holder: event.args.toHolder,
51
+ transactionHash: event.transactionHash,
52
+ transactionIndex: event.transactionIndex
53
+ }));
54
+ }, "fetchHolderTransfers");
55
+ const fetchAllTransfers = /* @__PURE__ */ __name(async (titleEscrowContract) => {
56
+ const allFilters = [
57
+ titleEscrowContract.filters.HolderTransfer,
58
+ titleEscrowContract.filters.BeneficiaryTransfer,
59
+ titleEscrowContract.filters.TokenReceived,
60
+ titleEscrowContract.filters.ReturnToIssuer,
61
+ // titleEscrowContract.filters.Nomination,
62
+ titleEscrowContract.filters.RejectTransferOwners,
63
+ titleEscrowContract.filters.RejectTransferBeneficiary,
64
+ titleEscrowContract.filters.RejectTransferHolder,
65
+ titleEscrowContract.filters.Shred
66
+ ];
67
+ const allLogs = await Promise.all(
68
+ allFilters.map(async (filter) => {
69
+ const logs = await titleEscrowContract.queryFilter(filter, 0, "latest");
70
+ return logs;
71
+ })
72
+ );
73
+ const holderChangeLogsParsed = getParsedLogs(allLogs.flat(), titleEscrowContract);
74
+ const tokenRegistryAddress = await titleEscrowContract.registry();
75
+ return holderChangeLogsParsed.map((event) => {
76
+ if (event?.name === "HolderTransfer") {
77
+ return {
78
+ type: "TRANSFER_HOLDER",
79
+ blockNumber: event.blockNumber,
80
+ holder: event.args.toHolder,
81
+ transactionHash: event.transactionHash,
82
+ transactionIndex: event.transactionIndex,
83
+ remark: event.args?.remark
84
+ };
85
+ } else if (event?.name === "BeneficiaryTransfer") {
86
+ return {
87
+ type: "TRANSFER_BENEFICIARY",
88
+ owner: event.args.toBeneficiary,
89
+ blockNumber: event.blockNumber,
90
+ transactionHash: event.transactionHash,
91
+ transactionIndex: event.transactionIndex,
92
+ remark: event.args?.remark
93
+ };
94
+ } else if (event?.name === "TokenReceived") {
95
+ const type = identifyTokenReceivedType(event);
96
+ return {
97
+ type,
98
+ from: type === "INITIAL" ? "0x0000000000000000000000000000000000000000" : tokenRegistryAddress,
99
+ to: titleEscrowContract.address,
100
+ blockNumber: event.blockNumber,
101
+ transactionHash: event.transactionHash,
102
+ transactionIndex: event.transactionIndex,
103
+ remark: event.args?.remark
104
+ };
105
+ } else if (event?.name === "ReturnToIssuer") {
106
+ return {
107
+ type: "RETURNED_TO_ISSUER",
108
+ blockNumber: event.blockNumber,
109
+ from: titleEscrowContract.address,
110
+ to: tokenRegistryAddress,
111
+ transactionHash: event.transactionHash,
112
+ transactionIndex: event.transactionIndex,
113
+ remark: event.args?.remark
114
+ };
115
+ } else if (event?.name === "Nomination") {
116
+ return void 0;
117
+ } else if (event?.name === "RejectTransferOwners") {
118
+ return {
119
+ type: "REJECT_TRANSFER_OWNERS",
120
+ blockNumber: event.blockNumber,
121
+ transactionHash: event.transactionHash,
122
+ transactionIndex: event.transactionIndex,
123
+ remark: event.args?.remark
124
+ };
125
+ } else if (event?.name === "RejectTransferBeneficiary") {
126
+ return {
127
+ type: "REJECT_TRANSFER_BENEFICIARY",
128
+ blockNumber: event.blockNumber,
129
+ transactionHash: event.transactionHash,
130
+ transactionIndex: event.transactionIndex,
131
+ remark: event.args?.remark
132
+ };
133
+ } else if (event?.name === "RejectTransferHolder") {
134
+ return {
135
+ type: "REJECT_TRANSFER_HOLDER",
136
+ blockNumber: event.blockNumber,
137
+ transactionHash: event.transactionHash,
138
+ transactionIndex: event.transactionIndex,
139
+ remark: event.args?.remark
140
+ };
141
+ } else if (event?.name === "Shred") {
142
+ return {
143
+ type: "RETURN_TO_ISSUER_ACCEPTED",
144
+ blockNumber: event.blockNumber,
145
+ from: tokenRegistryAddress,
146
+ to: "0x00000000000000000000000000000000000dead",
147
+ transactionHash: event.transactionHash,
148
+ transactionIndex: event.transactionIndex,
149
+ remark: event.args?.remark
150
+ };
151
+ }
152
+ return void 0;
153
+ }).filter((event) => event !== void 0);
154
+ }, "fetchAllTransfers");
155
+ function identifyTokenReceivedType(event) {
156
+ if (event.args.isMinting) {
157
+ return "INITIAL";
158
+ } else {
159
+ return "RETURN_TO_ISSUER_REJECTED";
160
+ }
161
+ }
162
+ __name(identifyTokenReceivedType, "identifyTokenReceivedType");
163
+
164
+ export { fetchEscrowTransfersV4, fetchEscrowTransfersV5 };
@@ -0,0 +1,56 @@
1
+ import { sortLogChain } from '../endorsement-chain/helpers';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ const fetchTokenTransfers = /* @__PURE__ */ __name(async (tokenRegistry, tokenId) => {
6
+ const logs = await fetchLogs(tokenRegistry, tokenId);
7
+ const parsedLogs = parseLogs(logs, tokenRegistry);
8
+ const tokenRegistryAddress = tokenRegistry.address;
9
+ const reformattedLogs = parsedLogs.map(
10
+ (event) => formatTokenTransferEvent(event, tokenRegistryAddress)
11
+ );
12
+ sortLogChain(reformattedLogs);
13
+ return reformattedLogs;
14
+ }, "fetchTokenTransfers");
15
+ const fetchLogs = /* @__PURE__ */ __name(async (tokenRegistry, tokenId) => {
16
+ const transferLogFilter = tokenRegistry.filters.Transfer(null, null, tokenId);
17
+ const logs = await tokenRegistry.queryFilter(transferLogFilter, 0);
18
+ if (logs.length === 0) {
19
+ throw new Error("Unminted Title Escrow");
20
+ }
21
+ return logs;
22
+ }, "fetchLogs");
23
+ const parseLogs = /* @__PURE__ */ __name((logs, tokenRegistry) => {
24
+ return logs.map((log) => {
25
+ if (!log.args) throw new Error(`Transfer log malformed: ${log}`);
26
+ if (!log.blockNumber) throw new Error("Block number not present");
27
+ if (!log.transactionHash) throw new Error("Transaction hash not present");
28
+ return {
29
+ ...log,
30
+ ...tokenRegistry.interface.parseLog(log)
31
+ };
32
+ });
33
+ }, "parseLogs");
34
+ const formatTokenTransferEvent = /* @__PURE__ */ __name((event, tokenRegistryAddress) => {
35
+ const type = identifyTokenTransferEvent(event, tokenRegistryAddress);
36
+ return {
37
+ type,
38
+ from: event.args.from,
39
+ to: event.args.to,
40
+ blockNumber: event.blockNumber,
41
+ transactionHash: event.transactionHash,
42
+ transactionIndex: event.transactionIndex
43
+ };
44
+ }, "formatTokenTransferEvent");
45
+ const identifyTokenTransferEvent = /* @__PURE__ */ __name((log, tokenRegistryAddress) => {
46
+ const InitialAddress = "0x0000000000000000000000000000000000000000";
47
+ const BurnAddress = "0x000000000000000000000000000000000000dEaD";
48
+ const { from, to } = log.args;
49
+ if (to === tokenRegistryAddress) return "SURRENDERED";
50
+ if (to === BurnAddress) return "SURRENDER_ACCEPTED";
51
+ if (from === tokenRegistryAddress) return "SURRENDER_REJECTED";
52
+ if (from === InitialAddress) return "INITIAL";
53
+ throw new Error("Unidentified transfer event");
54
+ }, "identifyTokenTransferEvent");
55
+
56
+ export { fetchTokenTransfers };
@@ -0,0 +1,94 @@
1
+ import { groupBy } from 'lodash';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ const fetchEventTime = /* @__PURE__ */ __name(async (blockNumber, provider) => {
6
+ const msecToSec = 1e3;
7
+ const eventTimestamp = (await provider.getBlock(blockNumber)).timestamp * msecToSec;
8
+ return eventTimestamp;
9
+ }, "fetchEventTime");
10
+ const getHolderOwner = /* @__PURE__ */ __name((events) => {
11
+ let owner = "";
12
+ let holder = "";
13
+ for (const event of events) {
14
+ owner = event.owner || owner;
15
+ holder = event.holder || holder;
16
+ }
17
+ return { owner, holder };
18
+ }, "getHolderOwner");
19
+ const mergeTransfersV4 = /* @__PURE__ */ __name((transferEvents) => {
20
+ const groupedEventsDict = groupBy(
21
+ transferEvents,
22
+ "transactionHash"
23
+ );
24
+ const transactionHashValues = Object.values(groupedEventsDict);
25
+ const mergedTransaction = transactionHashValues.flatMap((groupedEvents) => {
26
+ if (groupedEvents.length === 1) return groupedEvents;
27
+ if (groupedEvents.length === 2) {
28
+ const type = "TRANSFER_OWNERS";
29
+ const base = groupedEvents[0];
30
+ const { owner, holder } = getHolderOwner(groupedEvents);
31
+ return [{ ...base, type, owner, holder }];
32
+ }
33
+ if (groupedEvents.length === 3) {
34
+ const base = groupedEvents[0];
35
+ const type = "INITIAL";
36
+ const { owner, holder } = getHolderOwner(groupedEvents);
37
+ const found = groupedEvents.find((x) => {
38
+ return x.type === "INITIAL" || x.type === "SURRENDER_ACCEPTED";
39
+ });
40
+ return [{ ...base, owner, holder, type: found?.type || type }];
41
+ }
42
+ throw new Error("Invalid hash, update your configuration");
43
+ });
44
+ return mergedTransaction;
45
+ }, "mergeTransfersV4");
46
+ const mergeTransfersV5 = /* @__PURE__ */ __name((transferEvents) => {
47
+ const groupedEventsDict = groupBy(
48
+ transferEvents,
49
+ "transactionHash"
50
+ );
51
+ const transactionHashValues = Object.values(groupedEventsDict);
52
+ const mergedTransaction = transactionHashValues.flatMap((groupedEvents) => {
53
+ if (groupedEvents.length === 1) return groupedEvents;
54
+ if (groupedEvents.length > 1) {
55
+ const { owner, holder } = getHolderOwner(groupedEvents);
56
+ const base = groupedEvents[0];
57
+ const type = identifyEventTypeFromLogs(groupedEvents);
58
+ return [{ ...base, owner, holder, type }];
59
+ }
60
+ throw new Error("Invalid hash, update your configuration");
61
+ });
62
+ return mergedTransaction;
63
+ }, "mergeTransfersV5");
64
+ const identifyEventTypeFromLogs = /* @__PURE__ */ __name((groupedEvents) => {
65
+ for (const event of groupedEvents) {
66
+ if ([
67
+ "INITIAL",
68
+ "RETURNED_TO_ISSUER",
69
+ "RETURN_TO_ISSUER_ACCEPTED",
70
+ "RETURN_TO_ISSUER_REJECTED"
71
+ ].includes(event.type) || event.type.startsWith("REJECT_")) {
72
+ return event.type;
73
+ }
74
+ }
75
+ const isTransferHolder = groupedEvents.some((event) => event.type === "TRANSFER_HOLDER");
76
+ const isTransferBeneficiary = groupedEvents.some(
77
+ (event) => event.type === "TRANSFER_BENEFICIARY"
78
+ );
79
+ if (isTransferHolder && isTransferBeneficiary) {
80
+ return "TRANSFER_OWNERS";
81
+ } else if (isTransferHolder) {
82
+ return "TRANSFER_HOLDER";
83
+ } else if (isTransferBeneficiary) {
84
+ return "TRANSFER_BENEFICIARY";
85
+ }
86
+ throw new Error("Unable to identify event type");
87
+ }, "identifyEventTypeFromLogs");
88
+ const sortLogChain = /* @__PURE__ */ __name((logChain) => {
89
+ return logChain.sort((a, b) => {
90
+ return a.blockNumber - b.blockNumber;
91
+ });
92
+ }, "sortLogChain");
93
+
94
+ export { fetchEventTime, mergeTransfersV4, mergeTransfersV5, sortLogChain };
@@ -0,0 +1,6 @@
1
+ export * from './fetchEscrowTransfer';
2
+ export * from './fetchTokenTransfer';
3
+ export * from './helpers';
4
+ export * from './retrieveEndorsementChain';
5
+ export * from './types';
6
+ export * from './useEndorsementChain';
@@ -0,0 +1,41 @@
1
+ import { sortLogChain, fetchEventTime } from '../endorsement-chain/helpers';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+ const getEndorsementChain = /* @__PURE__ */ __name(async (provider, logChain) => {
6
+ const historyChain = [];
7
+ sortLogChain(logChain);
8
+ let previousBeneficiary = "";
9
+ let previousHolder = "";
10
+ const timestampPromises = logChain.map((log) => fetchEventTime(log.blockNumber, provider));
11
+ const timestamps = await Promise.all(timestampPromises);
12
+ logChain.forEach((log, index) => {
13
+ const timestamp = timestamps[index];
14
+ const transactionDetails = {
15
+ type: log.type,
16
+ transactionHash: log.transactionHash,
17
+ transactionIndex: log.transactionIndex,
18
+ blockNumber: log.blockNumber,
19
+ owner: log.owner || previousBeneficiary,
20
+ holder: log.holder || previousHolder,
21
+ timestamp,
22
+ remark: log?.remark || ""
23
+ };
24
+ if (log.type === "TRANSFER_OWNERS" || log.type === "TRANSFER_BENEFICIARY" || log.type === "TRANSFER_HOLDER" || log.type === "INITIAL") {
25
+ historyChain.push(transactionDetails);
26
+ previousHolder = transactionDetails.holder;
27
+ previousBeneficiary = transactionDetails.owner;
28
+ } else if (log.type === "SURRENDER_ACCEPTED" || log.type === "RETURN_TO_ISSUER_ACCEPTED") {
29
+ previousHolder = "";
30
+ previousBeneficiary = "";
31
+ historyChain.push(transactionDetails);
32
+ } else if (log.type === "SURRENDERED" || log.type === "SURRENDER_REJECTED" || log.type === "RETURNED_TO_ISSUER" || log.type === "RETURN_TO_ISSUER_REJECTED") {
33
+ historyChain.push(transactionDetails);
34
+ } else {
35
+ historyChain.push(transactionDetails);
36
+ }
37
+ });
38
+ return historyChain;
39
+ }, "getEndorsementChain");
40
+
41
+ export { getEndorsementChain };
@@ -0,0 +1,98 @@
1
+ import { ethers } from 'ethers';
2
+ import { decrypt } from '../decrypt';
3
+ import { fetchEscrowTransfersV4, fetchEscrowTransfersV5 } from '../endorsement-chain/fetchEscrowTransfer';
4
+ import { fetchTokenTransfers } from '../endorsement-chain/fetchTokenTransfer';
5
+ import { mergeTransfersV4, mergeTransfersV5 } from '../endorsement-chain/helpers';
6
+ import { getEndorsementChain } from '../endorsement-chain/retrieveEndorsementChain';
7
+ import { TradeTrustToken__factory } from '../../token-registry-v4/contracts';
8
+ import { supportInterfaceIds } from '../../token-registry-v4/supportInterfaceIds';
9
+ import { supportInterfaceIds as supportInterfaceIds$1 } from '../../token-registry-v5/supportInterfaceIds';
10
+
11
+ var __defProp = Object.defineProperty;
12
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
13
+ const TitleEscrowInterface = {
14
+ V4: supportInterfaceIds.TitleEscrow,
15
+ V5: supportInterfaceIds$1.TitleEscrow
16
+ };
17
+ const fetchTitleEscrowFactoryAddress = /* @__PURE__ */ __name(async (tokenRegistry) => {
18
+ return tokenRegistry.titleEscrowFactory();
19
+ }, "fetchTitleEscrowFactoryAddress");
20
+ const resolveTitleEscrowAddress = /* @__PURE__ */ __name(async (titleEscrowFactoryContract, tokenRegistryAddress, tokenId) => {
21
+ try {
22
+ return await titleEscrowFactoryContract.getEscrowAddress(tokenRegistryAddress, tokenId);
23
+ } catch {
24
+ return titleEscrowFactoryContract.getAddress(tokenRegistryAddress, tokenId);
25
+ }
26
+ }, "resolveTitleEscrowAddress");
27
+ const getTitleEscrowAddress = /* @__PURE__ */ __name(async (tokenRegistryAddress, tokenId, provider) => {
28
+ const tokenRegistryAbi = [
29
+ "function titleEscrowFactory() external view returns (address)",
30
+ "function ownerOf(uint256 tokenId) view returns (address)"
31
+ ];
32
+ const tokenRegistry = new ethers.Contract(tokenRegistryAddress, tokenRegistryAbi, provider);
33
+ const titleEscrowOwner = await tokenRegistry.ownerOf(tokenId);
34
+ const BURN_ADDRESS = "0x000000000000000000000000000000000000dEaD";
35
+ const isInactiveEscrow = [BURN_ADDRESS, tokenRegistryAddress].map((address) => address.toLowerCase()).includes(titleEscrowOwner.toLowerCase());
36
+ if (!isInactiveEscrow) return titleEscrowOwner;
37
+ const titleEscrowFactoryAddress = await fetchTitleEscrowFactoryAddress(tokenRegistry);
38
+ const titleEscrowFactoryContract = new ethers.Contract(
39
+ titleEscrowFactoryAddress,
40
+ [
41
+ "function getAddress(address, uint256) view returns (address)",
42
+ "function getEscrowAddress(address, uint256) view returns (address)"
43
+ ],
44
+ provider
45
+ );
46
+ return resolveTitleEscrowAddress(titleEscrowFactoryContract, tokenRegistryAddress, tokenId);
47
+ }, "getTitleEscrowAddress");
48
+ const checkSupportsInterface = /* @__PURE__ */ __name(async (titleEscrowAddress, interfaceId, provider) => {
49
+ try {
50
+ const titleEscrowAbi = [
51
+ "function supportsInterface(bytes4 interfaceId) external view returns (bool)"
52
+ ];
53
+ const titleEscrowContract = new ethers.Contract(titleEscrowAddress, titleEscrowAbi, provider);
54
+ return await titleEscrowContract.supportsInterface(interfaceId);
55
+ } catch {
56
+ return false;
57
+ }
58
+ }, "checkSupportsInterface");
59
+ const isTitleEscrowVersion = /* @__PURE__ */ __name(async (versionInterface, tokenRegistryAddress, tokenId, provider) => {
60
+ try {
61
+ const titleEscrowAddress = await getTitleEscrowAddress(tokenRegistryAddress, tokenId, provider);
62
+ return await checkSupportsInterface(titleEscrowAddress, versionInterface, provider);
63
+ } catch {
64
+ return false;
65
+ }
66
+ }, "isTitleEscrowVersion");
67
+ const fetchEndorsementChain = /* @__PURE__ */ __name(async (tokenRegistry, tokenId, provider, keyId) => {
68
+ if (!tokenRegistry || !tokenId || !provider) {
69
+ throw new Error("Missing required dependencies");
70
+ }
71
+ const [isV4, isV5] = await Promise.all([
72
+ isTitleEscrowVersion(TitleEscrowInterface.V4, tokenRegistry, tokenId, provider),
73
+ isTitleEscrowVersion(TitleEscrowInterface.V5, tokenRegistry, tokenId, provider)
74
+ ]);
75
+ if (!isV4 && !isV5) {
76
+ throw new Error("Only Token Registry V4/V5 is supported");
77
+ }
78
+ const titleEscrowAddress = await getTitleEscrowAddress(tokenRegistry, tokenId, provider);
79
+ let transferEvents = [];
80
+ if (isV4) {
81
+ const tokenRegistryContract = TradeTrustToken__factory.connect(tokenRegistry, provider);
82
+ const [tokenLogs, titleEscrowLogs] = await Promise.all([
83
+ fetchTokenTransfers(tokenRegistryContract, tokenId),
84
+ fetchEscrowTransfersV4(provider, titleEscrowAddress)
85
+ ]);
86
+ transferEvents = mergeTransfersV4([...titleEscrowLogs, ...tokenLogs]);
87
+ } else if (isV5) {
88
+ const titleEscrowLogs = await fetchEscrowTransfersV5(provider, titleEscrowAddress);
89
+ transferEvents = mergeTransfersV5(titleEscrowLogs);
90
+ }
91
+ const endorsementChain = await getEndorsementChain(provider, transferEvents);
92
+ return isV4 ? endorsementChain : endorsementChain.map((event) => ({
93
+ ...event,
94
+ remark: event?.remark?.slice(2) ? decrypt(event.remark.slice(2), keyId ?? "") : ""
95
+ }));
96
+ }, "fetchEndorsementChain");
97
+
98
+ export { TitleEscrowInterface, fetchEndorsementChain, getTitleEscrowAddress, isTitleEscrowVersion };
@@ -1,3 +1,4 @@
1
1
  export * from './decrypt';
2
2
  export * from './encrypt';
3
3
  export * from './verify';
4
+ export * from './endorsement-chain';
package/dist/index.d.mts CHANGED
@@ -12,6 +12,12 @@ export { TypedContractMethod } from '@tradetrust-tt/token-registry-v5/dist/contr
12
12
  export { decrypt } from './core/decrypt.mjs';
13
13
  export { encrypt } from './core/encrypt.mjs';
14
14
  export { verifyDocument } from './core/verify.mjs';
15
+ export { fetchEscrowTransfersV4, fetchEscrowTransfersV5 } from './core/endorsement-chain/fetchEscrowTransfer.mjs';
16
+ export { fetchTokenTransfers } from './core/endorsement-chain/fetchTokenTransfer.mjs';
17
+ export { fetchEventTime, mergeTransfersV4, mergeTransfersV5, sortLogChain } from './core/endorsement-chain/helpers.mjs';
18
+ export { getEndorsementChain } from './core/endorsement-chain/retrieveEndorsementChain.mjs';
19
+ export { EndorsementChain, ParsedLog, TitleEscrowTransferEvent, TitleEscrowTransferEventType, TokenTransferEvent, TokenTransferEventType, TradeTrustTokenEventType, TransferBaseEvent, TransferEvent, TransferEventType, TypedEvent } from './core/endorsement-chain/types.mjs';
20
+ export { TitleEscrowInterface, fetchEndorsementChain, getTitleEscrowAddress, isTitleEscrowVersion } from './core/endorsement-chain/useEndorsementChain.mjs';
15
21
  export { signOA } from './open-attestation/sign.mjs';
16
22
  export { KeyPair } from './open-attestation/types.mjs';
17
23
  export { diagnose, getAssetId, getDocumentData, getIssuerAddress, getTemplateURL, isObfuscated, isRawV2Document, isRawV3Document, isSignedWrappedV2Document, isSignedWrappedV3Document, isTransferableAsset, isWrappedV2Document, isWrappedV3Document } from './open-attestation/utils.mjs';
@@ -36,6 +42,10 @@ export { OpenAttestationDocument, SUPPORTED_SIGNING_ALGORITHM, SchemaId, SignedW
36
42
  export { DiagnoseError } from '@tradetrust-tt/tradetrust/dist/types/shared/utils';
37
43
  export { createResolver, getIdentifier, isValid, openAttestationDidIdentityProof, utils, verificationBuilder, verify } from '@tradetrust-tt/tt-verify';
38
44
  export { DocumentsToVerify, ErrorVerificationFragment, InvalidVerificationFragment, ProviderDetails, providerType as ProviderType, SkippedVerificationFragment, ValidVerificationFragment, VerificationBuilderOptions, VerificationFragment, VerificationFragmentStatus, VerificationFragmentType, VerificationFragmentWithData, Verifier, VerifierOptions } from '@tradetrust-tt/tt-verify/dist/types/src/types/core';
45
+ import 'ethers';
46
+ import '@ethersproject/abstract-provider';
47
+ import 'ethers/lib/utils';
48
+ import '@ethersproject/providers';
39
49
  import '@ethersproject/abstract-signer';
40
50
  import '@tradetrust-tt/tradetrust/dist/types/3.0/types';
41
51
  import '@tradetrust-tt/tradetrust/dist/types/__generated__/schema.3.0';