@t-rex-network/t-rex 5.0.0-beta2 → 5.0.0-beta3

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 (386) hide show
  1. package/NOTICE.md +78 -0
  2. package/README.md +19 -22
  3. package/contracts/{roles/AgentRole.sol → ERC-3643/ERC3643ErrorsLib.sol} +60 -33
  4. package/contracts/ERC-3643/IERC3643.sol +61 -5
  5. package/contracts/ERC-3643/IERC3643ClaimTopicsRegistry.sol +11 -2
  6. package/contracts/ERC-3643/IERC3643Compliance.sol +21 -8
  7. package/contracts/ERC-3643/IERC3643IdentityRegistry.sol +33 -0
  8. package/contracts/ERC-3643/IERC3643IdentityRegistryStorage.sol +62 -7
  9. package/contracts/ERC-3643/IERC3643TrustedIssuersRegistry.sol +29 -12
  10. package/contracts/ERC-3643/base/ERC3643ClaimTopicsRegistry.sol +140 -0
  11. package/contracts/ERC-3643/base/ERC3643Compliance.sol +223 -0
  12. package/contracts/ERC-3643/base/ERC3643IdentityRegistry.sol +382 -0
  13. package/contracts/ERC-3643/base/ERC3643IdentityRegistryStorage.sol +290 -0
  14. package/contracts/ERC-3643/base/ERC3643Token.sol +601 -0
  15. package/contracts/ERC-3643/base/ERC3643TrustedIssuersRegistry.sol +237 -0
  16. package/contracts/compliance/modular/ComplianceLedger.sol +175 -0
  17. package/contracts/compliance/modular/ComplianceLedgerLib.sol +120 -0
  18. package/contracts/compliance/modular/IComplianceLedger.sol +104 -0
  19. package/contracts/compliance/modular/IModularCompliance.sol +119 -28
  20. package/contracts/compliance/modular/ITransferValidation.sol +308 -0
  21. package/contracts/compliance/modular/ModularCompliance.sol +399 -240
  22. package/contracts/compliance/modular/ModuleSetLib.sol +182 -0
  23. package/contracts/compliance/modular/MovementKindLib.sol +97 -0
  24. package/contracts/compliance/modular/TransferContextLib.sol +140 -0
  25. package/contracts/compliance/modular/TransferValidation.sol +822 -0
  26. package/contracts/compliance/modular/TransferValidationLib.sol +183 -0
  27. package/contracts/compliance/modular/modules/AbstractModuleUpgradeable.sol +87 -56
  28. package/contracts/compliance/modular/modules/IModule.sol +142 -58
  29. package/contracts/compliance/modular/modules/MaxBalancePerIdentityModule.sol +175 -0
  30. package/contracts/compliance/modular/modules/ModuleProxy.sol +24 -24
  31. package/contracts/compliance/modular/modules/SpenderVerificationModule.sol +149 -0
  32. package/contracts/compliance/modular/modules/SpenderWhitelistModule.sol +215 -0
  33. package/contracts/factory/ITREXFactory.sol +94 -44
  34. package/contracts/factory/TREXFactory.sol +408 -201
  35. package/contracts/interop/ISettlementHandler.sol +101 -0
  36. package/contracts/interop/ITREXMessaging.sol +166 -0
  37. package/contracts/{proxy/ModularComplianceProxy.sol → interop/ITrustedGatewayRegistry.sol} +48 -43
  38. package/contracts/interop/TREXMessaging.sol +189 -0
  39. package/contracts/interop/TREXMessagingLib.sol +425 -0
  40. package/contracts/{proxy/TrustedIssuersRegistryProxy.sol → interop/TrustedGatewayRegistry.sol} +46 -37
  41. package/contracts/libraries/AccessManagerSetupLib.sol +484 -0
  42. package/contracts/libraries/ErrorsLib.sol +172 -65
  43. package/contracts/libraries/EventsLib.sol +168 -66
  44. package/contracts/libraries/MessageTypesLib.sol +259 -0
  45. package/contracts/libraries/RolesLib.sol +146 -0
  46. package/contracts/{roles/Roles.sol → libraries/VersionLib.sol} +46 -49
  47. package/contracts/libraries/WalletKeyLib.sol +156 -0
  48. package/contracts/proxy/beacon/ITREXImplementationAuthority.sol +157 -0
  49. package/contracts/proxy/beacon/TREXImplementationAuthority.sol +188 -0
  50. package/contracts/registry/implementation/IdentityRegistryStorage.sol +215 -163
  51. package/contracts/registry/implementation/TREXRegistry.sol +475 -0
  52. package/contracts/registry/interface/IIdentityRegistryStorage.sol +30 -25
  53. package/contracts/registry/interface/ITREXRegistry.sol +180 -0
  54. package/contracts/token/IToken.sol +210 -49
  55. package/contracts/token/Token.sol +411 -476
  56. package/contracts/token/TokenDelegationLib.sol +339 -0
  57. package/contracts/token/TokenGuardsLib.sol +123 -0
  58. package/contracts/token/TokenLedgerLib.sol +316 -0
  59. package/contracts/token/TokenRecoveryLib.sol +130 -0
  60. package/contracts/{roles/AgentRoleUpgradeable.sol → utils/AccessManagedOwnable.sol} +31 -31
  61. package/contracts/{proxy/IdentityRegistryStorageProxy.sol → utils/AccessManagedOwnableBase.sol} +56 -34
  62. package/contracts/utils/AccessManagedOwnableUpgradeable.sol +116 -0
  63. package/contracts/utils/IUtilityChecker.sol +32 -26
  64. package/contracts/{roles/AbstractAgentRole.sol → utils/TREXAccessManager.sol} +54 -57
  65. package/contracts/utils/UtilityChecker.sol +175 -52
  66. package/contracts/utils/UtilityCheckerProxy.sol +24 -24
  67. package/contracts/{proxy/interface/IProxy.sol → vendor/IERC173.sol} +15 -28
  68. package/out/AbstractModuleUpgradeable.sol/AbstractModuleUpgradeable.json +1 -1
  69. package/out/AbstractSigner.sol/AbstractSigner.json +1 -0
  70. package/out/AccessManaged.sol/AccessManaged.json +1 -0
  71. package/out/AccessManagedOwnable.sol/AccessManagedOwnable.json +1 -0
  72. package/out/AccessManagedOwnableBase.sol/AccessManagedOwnableBase.json +1 -0
  73. package/out/AccessManagedOwnableUpgradeable.sol/AccessManagedOwnableUpgradeable.json +1 -0
  74. package/out/AccessManagedUpgradeable.sol/AccessManagedUpgradeable.json +1 -0
  75. package/out/AccessManager.sol/AccessManager.json +1 -0
  76. package/out/AccessManagerHelper.sol/AccessManagerHelper.json +1 -0
  77. package/out/AccessManagerSetupLib.sol/AccessManagerSetupLib.json +1 -0
  78. package/out/AccessManagerUpgradeable.sol/AccessManagerUpgradeable.json +1 -0
  79. package/out/Account.sol/Account.json +1 -0
  80. package/out/Address.sol/Address.json +1 -1
  81. package/out/Arrays.sol/Arrays.json +1 -1
  82. package/out/AuthorityUtils.sol/AuthorityUtils.json +1 -0
  83. package/out/Base.sol/CommonBase.json +1 -1
  84. package/out/Base.sol/ScriptBase.json +1 -1
  85. package/out/Base.sol/TestBase.json +1 -1
  86. package/out/Base64.sol/Base64.json +1 -0
  87. package/out/BeaconProxy.sol/BeaconProxy.json +1 -0
  88. package/out/BeaconProxyDeployer.sol/BeaconProxyDeployer.json +1 -0
  89. package/out/Bytes.sol/Bytes.json +1 -1
  90. package/out/Calldata.sol/Calldata.json +1 -0
  91. package/out/CapabilityModules.sol/AllTypesModule.json +1 -0
  92. package/out/CapabilityModules.sol/CappedRecipientModule.json +1 -0
  93. package/out/CapabilityModules.sol/DelegatedCapRuleModule.json +1 -0
  94. package/out/CapabilityModules.sol/DuplicateTypeModule.json +1 -0
  95. package/out/CapabilityModules.sol/LockedSenderModule.json +1 -0
  96. package/out/CapabilityModules.sol/NoTypeModule.json +1 -0
  97. package/out/CapabilityModules.sol/RecordingModule.json +1 -0
  98. package/out/CapabilityModules.sol/RuleAndMintTrackerModule.json +1 -0
  99. package/out/CapabilityModules.sol/RuleAndTrackerModule.json +1 -0
  100. package/out/CapabilityModules.sol/RuleOnlyModule.json +1 -0
  101. package/out/CapabilityModules.sol/SpenderOnlyModule.json +1 -0
  102. package/out/CapabilityModules.sol/TrackerOnlyModule.json +1 -0
  103. package/out/CapabilityModules.sol/UndeclaredRuleModule.json +1 -0
  104. package/out/CapabilityModules.sol/WritingRuleModule.json +1 -0
  105. package/out/ClaimIssuerAlwaysReverts.sol/ClaimIssuerAlwaysReverts.json +1 -0
  106. package/out/ClaimIssuerTrick.sol/ClaimIssuerTrick.json +1 -1
  107. package/out/Comparators.sol/Comparators.json +1 -1
  108. package/out/ComplianceLedger.sol/ComplianceLedger.json +1 -0
  109. package/out/ComplianceLedgerLib.sol/ComplianceLedgerLib.json +1 -0
  110. package/out/Context.sol/Context.json +1 -1
  111. package/out/ContextUpgradeable.sol/ContextUpgradeable.json +1 -1
  112. package/out/Countries.sol/Countries.json +1 -1
  113. package/out/Create2.sol/Create2.json +1 -1
  114. package/out/Create3.sol/Create3.json +1 -1
  115. package/out/DeployComplianceModules.s.sol/DeployComplianceModules.json +1 -0
  116. package/out/DeployComplianceModules.s.sol/ITokenCompliance.json +1 -0
  117. package/out/DeployUtilityChecker.s.sol/DeployUtilityChecker.json +1 -0
  118. package/out/ECDSA.sol/ECDSA.json +1 -1
  119. package/out/EIP712.sol/EIP712.json +1 -1
  120. package/out/EIP712Upgradeable.sol/EIP712Upgradeable.json +1 -1
  121. package/out/ERC165.sol/ERC165.json +1 -0
  122. package/out/ERC165Checker.sol/ERC165Checker.json +1 -0
  123. package/out/ERC165Upgradeable.sol/ERC165Upgradeable.json +1 -0
  124. package/out/ERC1967Proxy.sol/ERC1967Proxy.json +1 -1
  125. package/out/ERC1967Utils.sol/ERC1967Utils.json +1 -1
  126. package/out/ERC20.sol/ERC20.json +1 -1
  127. package/out/ERC20Pausable.sol/ERC20Pausable.json +1 -1
  128. package/out/ERC20PermitUpgradeable.sol/ERC20PermitUpgradeable.json +1 -1
  129. package/out/ERC20Upgradeable.sol/ERC20Upgradeable.json +1 -1
  130. package/out/ERC3643ClaimTopicsRegistry.sol/ERC3643ClaimTopicsRegistry.json +1 -0
  131. package/out/ERC3643Compliance.sol/ERC3643Compliance.json +1 -0
  132. package/out/ERC3643ErrorsLib.sol/ERC3643ErrorsLib.json +1 -0
  133. package/out/ERC3643IdentityRegistry.sol/ERC3643IdentityRegistry.json +1 -0
  134. package/out/ERC3643IdentityRegistryStorage.sol/ERC3643IdentityRegistryStorage.json +1 -0
  135. package/out/ERC3643Token.sol/ERC3643Token.json +1 -0
  136. package/out/ERC3643TrustedIssuersRegistry.sol/ERC3643TrustedIssuersRegistry.json +1 -0
  137. package/out/ERC4337Utils.sol/ERC4337Utils.json +1 -0
  138. package/out/ERC4337Utils.sol/IEntryPointExtra.json +1 -0
  139. package/out/ERC734Validator.sol/ERC734Validator.json +1 -0
  140. package/out/ERC7579Validator.sol/ERC7579Validator.json +1 -0
  141. package/out/ERC7786GatewayMock.sol/ERC7786GatewayMock.json +1 -0
  142. package/out/ERC7786Recipient.sol/ERC7786Recipient.json +1 -0
  143. package/out/ERC7913WebAuthnVerifier.sol/ERC7913WebAuthnVerifier.json +1 -0
  144. package/out/EnumerableSet.sol/EnumerableSet.json +1 -1
  145. package/out/Errors.sol/Errors.json +1 -1
  146. package/out/ErrorsLib.sol/ErrorsLib.json +1 -1
  147. package/out/EventsLib.sol/EventsLib.json +1 -1
  148. package/out/Hashes.sol/Hashes.json +1 -0
  149. package/out/HostileModules.sol/RevertEverywhereModule.json +1 -0
  150. package/out/HostileModules.sol/UnbindRevertingModule.json +1 -0
  151. package/out/IAccessManaged.sol/IAccessManaged.json +1 -0
  152. package/out/IAccessManager.sol/IAccessManager.json +1 -0
  153. package/out/IAuthority.sol/IAuthority.json +1 -0
  154. package/out/IBeacon.sol/IBeacon.json +1 -1
  155. package/out/IClaimIssuer.sol/IClaimIssuer.json +1 -1
  156. package/out/IComplianceLedger.sol/IComplianceLedger.json +1 -0
  157. package/out/IERC1271.sol/IERC1271.json +1 -0
  158. package/out/IERC165.sol/IERC165.json +1 -1
  159. package/out/IERC173.sol/IERC173.json +1 -1
  160. package/out/IERC1967.sol/IERC1967.json +1 -1
  161. package/out/IERC20.sol/IERC20.json +1 -1
  162. package/out/IERC20Metadata.sol/IERC20Metadata.json +1 -1
  163. package/out/IERC20Permit.sol/IERC20Permit.json +1 -1
  164. package/out/IERC3643.sol/IERC3643.json +1 -1
  165. package/out/IERC3643ClaimTopicsRegistry.sol/IERC3643ClaimTopicsRegistry.json +1 -1
  166. package/out/IERC3643Compliance.sol/IERC3643Compliance.json +1 -1
  167. package/out/IERC3643IdentityRegistry.sol/IERC3643IdentityRegistry.json +1 -1
  168. package/out/IERC3643IdentityRegistryStorage.sol/IERC3643IdentityRegistryStorage.json +1 -1
  169. package/out/IERC3643TrustedIssuersRegistry.sol/IERC3643TrustedIssuersRegistry.json +1 -1
  170. package/out/IERC4337.sol/IAccount.json +1 -0
  171. package/out/IERC4337.sol/IAccountExecute.json +1 -0
  172. package/out/IERC4337.sol/IAggregator.json +1 -0
  173. package/out/IERC4337.sol/IEntryPoint.json +1 -0
  174. package/out/IERC4337.sol/IEntryPointNonces.json +1 -0
  175. package/out/IERC4337.sol/IEntryPointStake.json +1 -0
  176. package/out/IERC4337.sol/IPaymaster.json +1 -0
  177. package/out/IERC5267.sol/IERC5267.json +1 -1
  178. package/out/IERC734.sol/IERC734.json +1 -1
  179. package/out/IERC735.sol/IERC735.json +1 -1
  180. package/out/IERC7913.sol/IERC7913SignatureVerifier.json +1 -0
  181. package/out/IIdentity.sol/IIdentity.json +1 -1
  182. package/out/IIdentityFactory.sol/IIdentityFactory.json +1 -0
  183. package/out/IIdentityRegistryStorage.sol/IIdentityRegistryStorage.json +1 -1
  184. package/out/IKeyExecutor.sol/IKeyExecutor.json +1 -0
  185. package/out/IModularCompliance.sol/IModularCompliance.json +1 -1
  186. package/out/IModule.sol/IModule.json +1 -1
  187. package/out/IMulticall3.sol/IMulticall3.json +1 -1
  188. package/out/IReputationRegistry.sol/IReputationRegistry.json +1 -0
  189. package/out/ISettlementHandler.sol/ISettlementHandler.json +1 -0
  190. package/out/ITREXFactory.sol/ITREXFactory.json +1 -1
  191. package/out/ITREXImplementationAuthority.sol/ITREXImplementationAuthority.json +1 -1
  192. package/out/ITREXMessaging.sol/ITREXMessaging.json +1 -0
  193. package/out/ITREXRegistry.sol/ITREXRegistry.json +1 -0
  194. package/out/IToken.sol/IToken.json +1 -1
  195. package/out/ITransferValidation.sol/ITransferValidation.json +1 -0
  196. package/out/ITrustedGatewayRegistry.sol/ITrustedGatewayRegistry.json +1 -0
  197. package/out/IUtilityChecker.sol/IUtilityChecker.json +1 -1
  198. package/out/Identity.sol/Identity.json +1 -1
  199. package/out/IdentityFactory.sol/IdentityFactory.json +1 -0
  200. package/out/IdentityModulesHelper.sol/IdentityModulesHelper.json +1 -0
  201. package/out/IdentityRegistryStorage.sol/IdentityRegistryStorage.json +1 -1
  202. package/out/IdentityTypes.sol/IdentityTypes.json +1 -0
  203. package/out/Initializable.sol/Initializable.json +1 -1
  204. package/out/InteropSuiteTest.sol/InteropSuiteTest.json +1 -0
  205. package/out/KeyApprovalModule.sol/IIdentityAccount.json +1 -0
  206. package/out/KeyApprovalModule.sol/KeyApprovalModule.json +1 -0
  207. package/out/KeyManager.sol/KeyManager.json +1 -0
  208. package/out/KeyPurposes.sol/KeyPurposes.json +1 -1
  209. package/out/KeyTypes.sol/KeyTypes.json +1 -1
  210. package/out/LowLevelCall.sol/LowLevelCall.json +1 -1
  211. package/out/Math.sol/Math.json +1 -1
  212. package/out/MaxBalancePerIdentityModule.sol/MaxBalancePerIdentityModule.json +1 -0
  213. package/out/MessageHashUtils.sol/MessageHashUtils.json +1 -1
  214. package/out/MessageTypesLib.sol/MessageTypesLib.json +1 -0
  215. package/out/MockTREXAccessManagerV2.sol/MockTREXAccessManagerV2.json +1 -0
  216. package/out/MockTokenV2.sol/MockTokenV2.json +1 -0
  217. package/out/Mocks.sol/ClaimTopicsRegistryMock.json +1 -0
  218. package/out/Mocks.sol/ComplianceMock.json +1 -0
  219. package/out/Mocks.sol/IdentityRegistryMock.json +1 -0
  220. package/out/Mocks.sol/IdentityRegistryStorageMock.json +1 -0
  221. package/out/Mocks.sol/TokenMock.json +1 -0
  222. package/out/Mocks.sol/TrustedIssuersRegistryMock.json +1 -0
  223. package/out/ModularCompliance.sol/ModularCompliance.json +1 -1
  224. package/out/ModuleNotPnP.sol/ModuleNotPnP.json +1 -1
  225. package/out/ModuleProxy.sol/ModuleProxy.json +1 -1
  226. package/out/ModuleSetLib.sol/ModuleSetLib.json +1 -0
  227. package/out/MovementKindLib.sol/MovementKindLib.json +1 -0
  228. package/out/Multicall.sol/Multicall.json +1 -0
  229. package/out/MulticallUpgradeable.sol/MulticallUpgradeable.json +1 -1
  230. package/out/Nonces.sol/Nonces.json +1 -1
  231. package/out/NoncesUpgradeable.sol/NoncesUpgradeable.json +1 -1
  232. package/out/Ownable.sol/Ownable.json +1 -1
  233. package/out/OwnableUpgradeable.sol/OwnableUpgradeable.json +1 -1
  234. package/out/P256.sol/P256.json +1 -0
  235. package/out/Packing.sol/Packing.json +1 -0
  236. package/out/Panic.sol/Panic.json +1 -1
  237. package/out/Pausable.sol/Pausable.json +1 -1
  238. package/out/PausableUpgradeable.sol/PausableUpgradeable.json +1 -1
  239. package/out/Proxy.sol/Proxy.json +1 -1
  240. package/out/ReentrancyGuardTransient.sol/ReentrancyGuardTransient.json +1 -0
  241. package/out/ReentrantModule.sol/ReentrantModule.json +1 -0
  242. package/out/ReputationRegistry.sol/ReputationRegistry.json +1 -0
  243. package/out/RolesLib.sol/RolesLib.json +1 -0
  244. package/out/SafeCalldataBatch.sol/SafeCalldataBatch.json +1 -0
  245. package/out/SafeCast.sol/SafeCast.json +1 -1
  246. package/out/SatelliteLiteMock.sol/SatelliteLiteMock.json +1 -0
  247. package/out/Script.sol/Script.json +1 -0
  248. package/out/ShortStrings.sol/ShortStrings.json +1 -1
  249. package/out/SignatureChecker.sol/SignatureChecker.json +1 -0
  250. package/out/SignedMath.sol/SignedMath.json +1 -1
  251. package/out/SlotDerivation.sol/SlotDerivation.json +1 -1
  252. package/out/SmartAccount.sol/SmartAccount.json +1 -0
  253. package/out/SpenderVerificationModule.sol/SpenderVerificationModule.json +1 -0
  254. package/out/SpenderWhitelistModule.sol/SpenderWhitelistModule.json +1 -0
  255. package/out/StdAssertions.sol/StdAssertions.json +1 -1
  256. package/out/StdChains.sol/StdChains.json +1 -1
  257. package/out/StdCheats.sol/StdCheats.json +1 -1
  258. package/out/StdCheats.sol/StdCheatsSafe.json +1 -1
  259. package/out/StdConstants.sol/StdConstants.json +1 -1
  260. package/out/StdError.sol/stdError.json +1 -1
  261. package/out/StdInvariant.sol/StdInvariant.json +1 -1
  262. package/out/StdJson.sol/stdJson.json +1 -1
  263. package/out/StdMath.sol/stdMath.json +1 -1
  264. package/out/StdStorage.sol/stdStorage.json +1 -1
  265. package/out/StdStorage.sol/stdStorageSafe.json +1 -1
  266. package/out/StdStyle.sol/StdStyle.json +1 -1
  267. package/out/StdToml.sol/stdToml.json +1 -1
  268. package/out/StdUtils.sol/StdUtils.json +1 -1
  269. package/out/StorageSlot.sol/StorageSlot.json +1 -1
  270. package/out/Strings.sol/Strings.json +1 -1
  271. package/out/Structs.sol/Structs.json +1 -1
  272. package/out/TREXAccessManager.sol/TREXAccessManager.json +1 -0
  273. package/out/TREXFactory.sol/TREXFactory.json +1 -1
  274. package/out/TREXImplementationAuthority.sol/TREXImplementationAuthority.json +1 -1
  275. package/out/TREXMessaging.sol/TREXMessaging.json +1 -0
  276. package/out/TREXMessagingLib.sol/TREXMessagingLib.json +1 -0
  277. package/out/TREXRegistry.sol/TREXRegistry.json +1 -0
  278. package/out/TestERC20.sol/TestERC20.json +1 -1
  279. package/out/TestModule.sol/TestModule.json +1 -1
  280. package/out/TestTREXFactory.sol/TestTREXFactory.json +1 -1
  281. package/out/Time.sol/Time.json +1 -0
  282. package/out/Token.sol/Token.json +1 -1
  283. package/out/TokenDelegationLib.sol/TokenDelegationLib.json +1 -0
  284. package/out/TokenGuardsLib.sol/TokenGuardsLib.json +1 -0
  285. package/out/TokenHandler.sol/TokenHandler.json +1 -0
  286. package/out/TokenLedgerHarness.sol/TokenLedgerHarness.json +1 -0
  287. package/out/TokenLedgerLib.sol/TokenLedgerLib.json +1 -0
  288. package/out/TokenRecoveryLib.sol/TokenRecoveryLib.json +1 -0
  289. package/out/TokenReservationStub.sol/TokenReservationStub.json +1 -0
  290. package/out/TransferContextLib.sol/TransferContextLib.json +1 -0
  291. package/out/TransferValidation.sol/TransferValidation.json +1 -0
  292. package/out/TransferValidationHarness.sol/TransferValidationHarness.json +1 -0
  293. package/out/TransferValidationLib.sol/TransferValidationLib.json +1 -0
  294. package/out/TransientSlot.sol/TransientSlot.json +1 -0
  295. package/out/TrustedGatewayRegistry.sol/TrustedGatewayRegistry.json +1 -0
  296. package/out/UUPSUpgradeable.sol/UUPSUpgradeable.json +1 -1
  297. package/out/UpgradeableBeacon.sol/UpgradeableBeacon.json +1 -0
  298. package/out/UtilityChecker.sol/UtilityChecker.json +1 -1
  299. package/out/UtilityCheckerProxy.sol/UtilityCheckerProxy.json +1 -1
  300. package/out/Utils.sol/Utils.json +1 -1
  301. package/out/VersionLib.sol/VersionLib.json +1 -0
  302. package/out/Vm.sol/Vm.json +1 -1
  303. package/out/Vm.sol/VmSafe.json +1 -1
  304. package/out/WalletKeyLib.sol/WalletKeyLib.json +1 -0
  305. package/out/WebAuthn.sol/WebAuthn.json +1 -0
  306. package/out/console.sol/console.json +1 -1
  307. package/out/draft-AccountERC7579Upgradeable.sol/AccountERC7579Upgradeable.json +1 -0
  308. package/out/draft-ERC7579Utils.sol/ERC7579Utils.json +1 -0
  309. package/out/draft-IERC1822.sol/IERC1822Proxiable.json +1 -1
  310. package/out/draft-IERC6093.sol/IERC1155Errors.json +1 -1
  311. package/out/draft-IERC6093.sol/IERC20Errors.json +1 -1
  312. package/out/draft-IERC6093.sol/IERC721Errors.json +1 -1
  313. package/out/draft-IERC7579.sol/IERC7579AccountConfig.json +1 -0
  314. package/out/draft-IERC7579.sol/IERC7579Execution.json +1 -0
  315. package/out/draft-IERC7579.sol/IERC7579Hook.json +1 -0
  316. package/out/draft-IERC7579.sol/IERC7579Module.json +1 -0
  317. package/out/draft-IERC7579.sol/IERC7579ModuleConfig.json +1 -0
  318. package/out/draft-IERC7579.sol/IERC7579Validator.json +1 -0
  319. package/out/draft-IERC7786.sol/IERC7786GatewaySource.json +1 -0
  320. package/out/draft-IERC7786.sol/IERC7786Recipient.json +1 -0
  321. package/out/draft-InteroperableAddress.sol/InteroperableAddress.json +1 -0
  322. package/out/libraries/Errors.sol/Errors.json +1 -1
  323. package/out/safeconsole.sol/safeconsole.json +1 -1
  324. package/package.json +11 -7
  325. package/contracts/ERC-3643/ERC3643EventsLib.sol +0 -150
  326. package/contracts/factory/ITREXGateway.sol +0 -234
  327. package/contracts/factory/TREXGateway.sol +0 -274
  328. package/contracts/proxy/AbstractProxy.sol +0 -146
  329. package/contracts/proxy/ClaimTopicsRegistryProxy.sol +0 -83
  330. package/contracts/proxy/IdentityRegistryProxy.sol +0 -95
  331. package/contracts/proxy/TokenProxy.sol +0 -97
  332. package/contracts/proxy/authority/IAFactory.sol +0 -122
  333. package/contracts/proxy/authority/IIAFactory.sol +0 -87
  334. package/contracts/proxy/authority/ITREXImplementationAuthority.sol +0 -239
  335. package/contracts/proxy/authority/TREXImplementationAuthority.sol +0 -350
  336. package/contracts/registry/implementation/ClaimTopicsRegistry.sol +0 -152
  337. package/contracts/registry/implementation/IdentityRegistry.sol +0 -352
  338. package/contracts/registry/implementation/TrustedIssuersRegistry.sol +0 -272
  339. package/contracts/registry/interface/IClaimTopicsRegistry.sol +0 -70
  340. package/contracts/registry/interface/IIdentityRegistry.sol +0 -102
  341. package/contracts/registry/interface/ITrustedIssuersRegistry.sol +0 -70
  342. package/contracts/roles/IERC173.sol +0 -21
  343. package/contracts/token/TokenStructs.sol +0 -74
  344. package/contracts/utils/InterfaceIdCalculator.sol +0 -223
  345. package/contracts/vendor/openzeppelin/Create3.sol +0 -143
  346. package/out/AbstractAgentRole.sol/AbstractAgentRole.json +0 -1
  347. package/out/AbstractProxy.sol/AbstractProxy.json +0 -1
  348. package/out/AgentRole.sol/AgentRole.json +0 -1
  349. package/out/AgentRoleUpgradeable.sol/AgentRoleUpgradeable.json +0 -1
  350. package/out/ClaimIssuer.sol/ClaimIssuer.json +0 -1
  351. package/out/ClaimTopicsRegistry.sol/ClaimTopicsRegistry.json +0 -1
  352. package/out/ClaimTopicsRegistryProxy.sol/ClaimTopicsRegistryProxy.json +0 -1
  353. package/out/ERC2771Context.sol/ERC2771Context.json +0 -1
  354. package/out/ERC2771ContextUpgradeable.sol/ERC2771ContextUpgradeable.json +0 -1
  355. package/out/ERC2771Forwarder.sol/ERC2771Forwarder.json +0 -1
  356. package/out/ERC3643EventsLib.sol/ERC3643EventsLib.json +0 -1
  357. package/out/IAFactory.sol/IAFactory.json +0 -1
  358. package/out/IClaimTopicsRegistry.sol/IClaimTopicsRegistry.json +0 -1
  359. package/out/IERC1363.sol/IERC1363.json +0 -1
  360. package/out/IIAFactory.sol/IIAFactory.json +0 -1
  361. package/out/IIdFactory.sol/IIdFactory.json +0 -1
  362. package/out/IIdentityRegistry.sol/IIdentityRegistry.json +0 -1
  363. package/out/IImplementationAuthority.sol/IImplementationAuthority.json +0 -1
  364. package/out/IProxy.sol/IProxy.json +0 -1
  365. package/out/ITREXGateway.sol/ITREXGateway.json +0 -1
  366. package/out/ITrustedIssuersRegistry.sol/ITrustedIssuersRegistry.json +0 -1
  367. package/out/IdFactory.sol/IdFactory.json +0 -1
  368. package/out/IdentityProxy.sol/IdentityProxy.json +0 -1
  369. package/out/IdentityRegistry.sol/IdentityRegistry.json +0 -1
  370. package/out/IdentityRegistryProxy.sol/IdentityRegistryProxy.json +0 -1
  371. package/out/IdentityRegistryStorageProxy.sol/IdentityRegistryStorageProxy.json +0 -1
  372. package/out/ImplementationAuthority.sol/ImplementationAuthority.json +0 -1
  373. package/out/InterfaceIdCalculator.sol/InterfaceIdCalculator.json +0 -1
  374. package/out/ModularComplianceProxy.sol/ModularComplianceProxy.json +0 -1
  375. package/out/Ownable2Step.sol/Ownable2Step.json +0 -1
  376. package/out/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.json +0 -1
  377. package/out/Roles.sol/Roles.json +0 -1
  378. package/out/SafeERC20.sol/SafeERC20.json +0 -1
  379. package/out/Storage.sol/Storage.json +0 -1
  380. package/out/TREXGateway.sol/TREXGateway.json +0 -1
  381. package/out/TestAgentRole.sol/TestAgentRole.json +0 -1
  382. package/out/TestAgentRoleUpgradeable.sol/TestAgentRoleUpgradeable.json +0 -1
  383. package/out/TokenProxy.sol/TokenProxy.json +0 -1
  384. package/out/TrustedIssuersRegistry.sol/TrustedIssuersRegistry.json +0 -1
  385. package/out/TrustedIssuersRegistryProxy.sol/TrustedIssuersRegistryProxy.json +0 -1
  386. package/out/Version.sol/Version.json +0 -1
@@ -0,0 +1,822 @@
1
+ // SPDX-License-Identifier: GPL-3.0
2
+ //
3
+ // :+#####%%%%%%%%%%%%%%+
4
+ // .-*@@@%+.:+%@@@@@%%#***%@@%=
5
+ // :=*%@@@#=. :#@@% *@@@%=
6
+ // .-+*%@%*-.:+%@@@@@@+. -*+: .=#. :%@@@%-
7
+ // :=*@@@@%%@@@@@@@@@%@@@- .=#@@@%@%= =@@@@#.
8
+ // -=+#%@@%#*=:. :%@@@@%. -*@@#*@@@@@@@#=:- *@@@@+
9
+ // =@@%=:. :=: *@@@@@%#- =%*%@@@@#+-. =+ :%@@@%-
10
+ // -@@%. .+@@@ =+=-. @@#- +@@@%- =@@@@%:
11
+ // :@@@. .+@@#%: : .=*=-::.-%@@@+*@@= +@@@@#.
12
+ // %@@: +@%%* =%@@@@@@@@@@@#. .*@%- +@@@@*.
13
+ // #@@= .+@@@@%:=*@@@@@- :%@%: .*@@@@+
14
+ // *@@* +@@@#-@@%-:%@@* +@@#. :%@@@@-
15
+ // -@@% .:-=++*##%%%@@@@@@@@@@@@*. :@+.@@@%: .#@@+ =@@@@#:
16
+ // .@@@*-+*#%%%@@@@@@@@@@@@@@@@%%#**@@%@@@. *@=*@@# :#@%= .#@@@@#-
17
+ // -%@@@@@@@@@@@@@@@*+==-:-@@@= *@# .#@*-=*@@@@%= -%@@@* =@@@@@%-
18
+ // -+%@@@#. %@%%= -@@:+@: -@@* *@@*-:: -%@@%=. .*@@@@@#
19
+ // *@@@* +@* *@@##@@- #@*@@+ -@@= . :+@@@#: .-+@@@%+-
20
+ // +@@@%*@@:..=@@@@* .@@@* .#@#. .=+- .=%@@@*. :+#@@@@*=:
21
+ // =@@@@%@@@@@@@@@@@@@@@@@@@@@@%- :+#*. :*@@@%=. .=#@@@@%+:
22
+ // .%@@= ..... .=#@@+. .#@@@*: -*%@@@@%+.
23
+ // +@@#+===---:::... .=%@@*- +@@@+. -*@@@@@%+.
24
+ // -@@@@@@@@@@@@@@@@@@@@@@%@@@@= -@@@+ -#@@@@@#=.
25
+ // ..:::---===+++***###%%%@@@#- .#@@+ -*@@@@@#=.
26
+ // @@@@@@+. +@@*. .+@@@@@%=.
27
+ // -@@@@@= =@@%: -#@@@@%+.
28
+ // +@@@@@. =@@@= .+@@@@@*:
29
+ // #@@@@#:%@@#. :*@@@@#-
30
+ // @@@@@%@@@= :#@@@@+.
31
+ // :@@@@@@@#.:#@@@%-
32
+ // +@@@@@@-.*@@@*:
33
+ // #@@@@#.=@@@+.
34
+ // @@@@+-%@%=
35
+ // :@@@#%@%=
36
+ // +@@@@%-
37
+ // :#%%=
38
+ //
39
+
40
+ // T-REX Smart Contracts
41
+ // The ERC-3643 token suite of the T-REX ecosystem.
42
+ //
43
+ // Copyright (C) 2026 Digital Asset Operational Services ISAC Ltd. ("T-REX Network")
44
+ //
45
+ // This file is part of the T-REX smart contract suite.
46
+ //
47
+ // This program is free software: you can redistribute it and/or modify
48
+ // it under the terms of the GNU General Public License as published by
49
+ // the Free Software Foundation, either version 3 of the License, or
50
+ // (at your option) any later version.
51
+ //
52
+ // This program is distributed in the hope that it will be useful,
53
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
54
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55
+ // GNU General Public License for more details.
56
+ //
57
+ // You should have received a copy of the GNU General Public License
58
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
59
+ pragma solidity 0.8.30;
60
+
61
+ import { ErrorsLib } from "../../libraries/ErrorsLib.sol";
62
+ import { EventsLib } from "../../libraries/EventsLib.sol";
63
+ import { MessageTypesLib } from "../../libraries/MessageTypesLib.sol";
64
+ import { WalletKeyLib } from "../../libraries/WalletKeyLib.sol";
65
+ import { ITREXRegistry } from "../../registry/interface/ITREXRegistry.sol";
66
+ import { IToken } from "../../token/IToken.sol";
67
+ import { ComplianceLedger } from "./ComplianceLedger.sol";
68
+ import { ITransferValidation } from "./ITransferValidation.sol";
69
+ import { TransferContextLib } from "./TransferContextLib.sol";
70
+ import { TransferValidationLib } from "./TransferValidationLib.sol";
71
+ import { IModule } from "./modules/IModule.sol";
72
+
73
+ /**
74
+ * @title TransferValidation
75
+ * @dev The compliance's issuance layer for satellite movements: the two windows, the per-chain pause, the record
76
+ * of every issued validation, the issuance itself, the pending reservation it writes, settlement, discard and the
77
+ * late-reconciliation surface. Own ERC-7201 namespace, so the module registry's layout is untouched.
78
+ *
79
+ * Three flows, each readable top to bottom: `requestTransferValidation` issues, `_handleSettlement` applies what
80
+ * a satellite executed, `_discardExpiredValidations` rolls back what it never did. The reservation involves no
81
+ * module: issuance writes the pending amounts into the ledger and the other two flows release them once.
82
+ *
83
+ * `from` is always a satellite wallet, never a native one: the Lite that executes a validation has to physically
84
+ * hold the position it moves, where a native balance stays free to leave between issuance and settlement. A native
85
+ * position reaches a satellite through delegation-out instead, which burns before it instructs.
86
+ *
87
+ * `expiry` is the satellite's hard deadline; `expiry + reconciliationWindow` is when T-REX may release the
88
+ * reservation, never a refusal of a late leg. Validations expire; settlements never do.
89
+ */
90
+ abstract contract TransferValidation is ITransferValidation, ComplianceLedger {
91
+
92
+ /// @dev A leg is one satellite chain's half of a movement, and the notification it sends back once it has
93
+ /// executed.
94
+ ///
95
+ /// A movement inside one satellite chain, or from a satellite to this one, is done by a single chain: one
96
+ /// leg, one notification, and the movement is complete when it arrives. A movement between two different
97
+ /// satellite chains is done by two chains that cannot act atomically: the sending chain destroys the
98
+ /// tokens on its side, the receiving chain creates them on its side, and each reports its own half. The
99
+ /// movement is complete only when both have reported.
100
+ ///
101
+ /// This says which half a notification is.
102
+ enum Leg {
103
+ /// The whole movement, reported by the only chain involved.
104
+ Single,
105
+ /// The sending chain destroyed its side. The tokens are now in transit, belonging to nobody.
106
+ Burn,
107
+ /// The receiving chain created its side.
108
+ Mint
109
+ }
110
+
111
+ /* ----- Issuance ----- */
112
+
113
+ /// @inheritdoc ITransferValidation
114
+ /// @dev Five questions, in order: where is this going, may the caller ask for it, by when must it happen,
115
+ /// how much may actually move, and may the named spender execute it. Only then is anything written.
116
+ function requestTransferValidation(
117
+ bytes calldata from,
118
+ bytes calldata to,
119
+ uint256 requestedMin,
120
+ uint256 requestedMax,
121
+ bytes calldata spender
122
+ ) external returns (uint256 validationId) {
123
+ require(requestedMin <= requestedMax, ErrorsLib.InvalidRequestedRange(requestedMin, requestedMax));
124
+
125
+ TransferValidationLib.Draft memory draft;
126
+ draft.amountMin = requestedMin;
127
+ draft.amountMax = requestedMax;
128
+ _resolveRoute(draft, from, to, spender);
129
+ _requireCallerOwnsTheWallet(draft, from);
130
+ _setExpiryAndRelease(draft);
131
+ _narrowAmountRange(draft, from, to, spender);
132
+ _requireSpenderAllowed(draft, to, spender);
133
+
134
+ validationId = _issueValidation(from, to, spender, draft);
135
+ }
136
+
137
+ /// @dev Where the movement goes: both envelopes parse, the sender sits on a satellite, and the legs are
138
+ /// two when the wallets are on two different satellite chains. The spender is parsed here and judged in
139
+ /// {_requireSpenderAllowed}, once the range is known.
140
+ function _resolveRoute(
141
+ TransferValidationLib.Draft memory draft,
142
+ bytes calldata from,
143
+ bytes calldata to,
144
+ bytes calldata spender
145
+ ) private view {
146
+ (bool fromNative,) = WalletKeyLib.isReferenceChain(from);
147
+ require(!fromNative, ErrorsLib.SenderNotOnSatellite(from));
148
+ (bool toNative,) = WalletKeyLib.isReferenceChain(to);
149
+ draft.fromChainKey = _chainKeyOf(from);
150
+ draft.toChainKey = _chainKeyOf(to);
151
+ draft.twoLegs = !toNative && draft.fromChainKey != draft.toChainKey;
152
+ if (spender.length != 0) WalletKeyLib.parse(spender);
153
+ }
154
+
155
+ /// @dev Who may ask: the identity the sending wallet is linked to, or an address the AccessManager
156
+ /// authorised for this selector. A satellite wallet never matches its own bytes, so a wallet can never
157
+ /// request a validation for itself; its identity does that.
158
+ function _requireCallerOwnsTheWallet(TransferValidationLib.Draft memory draft, bytes calldata from) private view {
159
+ draft.fromIdentity = address(_boundRegistry().resolveIdentity(from));
160
+ require(draft.fromIdentity != address(0), ErrorsLib.UnverifiedWallet(from));
161
+ require(
162
+ msg.sender == draft.fromIdentity || _canCallSelector(msg.sender, this.requestTransferValidation.selector),
163
+ ErrorsLib.NotAuthorizedForWallet(msg.sender, from)
164
+ );
165
+ }
166
+
167
+ /// @dev By when: the satellite's deadline from the validity window, and the reservation's from the larger
168
+ /// reconciliation window of the chains involved. Every chain involved must be open and configured.
169
+ function _setExpiryAndRelease(TransferValidationLib.Draft memory draft) private view {
170
+ TransferValidationLib.ValidationStorage storage store = _validationStorage();
171
+ require(store.defaultValidityWindow != 0, ErrorsLib.ValidityWindowNotSet());
172
+ draft.reconciliationWindow = _openChainWindow(store, draft.fromChainKey);
173
+ if (draft.twoLegs) {
174
+ uint64 toWindow = _openChainWindow(store, draft.toChainKey);
175
+ if (toWindow > draft.reconciliationWindow) draft.reconciliationWindow = toWindow;
176
+ }
177
+ draft.expiry = uint64(block.timestamp) + store.defaultValidityWindow;
178
+ }
179
+
180
+ /// @dev How much may move. The range only ever narrows: first to what the sending wallet still has free,
181
+ /// then to the smallest amount the rules allow.
182
+ function _narrowAmountRange(
183
+ TransferValidationLib.Draft memory draft,
184
+ bytes calldata from,
185
+ bytes calldata to,
186
+ bytes calldata spender
187
+ ) private view {
188
+ ITREXRegistry registry = _boundRegistry();
189
+ require(registry.isWalletVerified(to), ErrorsLib.UnverifiedWallet(to));
190
+ draft.toIdentity = address(registry.resolveIdentity(to));
191
+ draft.fromKey = WalletKeyLib.canonicalKey(from);
192
+
193
+ _capAtWhatTheWalletCanSend(draft, from);
194
+ _capAtWhatTheRulesAllow(draft, to, spender);
195
+ require(draft.amountMax != 0, ErrorsLib.ZeroValue());
196
+ }
197
+
198
+ /// @dev Caps the range at what the sending wallet can still send: its bridged balance less what earlier
199
+ /// validations may still draw from it.
200
+ function _capAtWhatTheWalletCanSend(TransferValidationLib.Draft memory draft, bytes calldata from) private view {
201
+ uint256 free = _boundToken().availableOf(from);
202
+ if (free < draft.amountMax) draft.amountMax = free;
203
+ require(draft.amountMin <= draft.amountMax, ErrorsLib.EmptyValidationRange(draft.amountMin, draft.amountMax));
204
+ }
205
+
206
+ /// @dev Caps the range at the smallest amount the rules allow. Skipped when both wallets belong to one
207
+ /// identity, since relocating your own tokens changes no position and no distribution rule applies.
208
+ function _capAtWhatTheRulesAllow(
209
+ TransferValidationLib.Draft memory draft,
210
+ bytes calldata to,
211
+ bytes calldata spender
212
+ ) private view {
213
+ if (_isRelocation(draft.fromIdentity, draft.toIdentity)) return;
214
+ IModule.TransferContext memory ctx = TransferContextLib.issuance(
215
+ address(this),
216
+ draft.fromIdentity,
217
+ draft.toIdentity,
218
+ draft.fromKey,
219
+ WalletKeyLib.walletId(to),
220
+ draft.amountMin,
221
+ draft.amountMax,
222
+ spender
223
+ );
224
+ uint256 allowed = _minAllowedAmount(ctx);
225
+ if (allowed < draft.amountMax) draft.amountMax = allowed;
226
+ require(draft.amountMin <= draft.amountMax, ErrorsLib.EmptyValidationRange(draft.amountMin, draft.amountMax));
227
+ }
228
+
229
+ /// @dev Who may execute: every `SPENDER` module must accept the named spender, since no module runs on the
230
+ /// satellite and this is the only place it can be refused. A validation the sender executes itself names
231
+ /// no spender and asks nobody. Asked on a relocation too: who executes is not about distribution.
232
+ function _requireSpenderAllowed(TransferValidationLib.Draft memory draft, bytes calldata to, bytes calldata spender)
233
+ private
234
+ view
235
+ {
236
+ if (spender.length == 0) return;
237
+ IModule.TransferContext memory ctx = TransferContextLib.issuance(
238
+ address(this),
239
+ draft.fromIdentity,
240
+ draft.toIdentity,
241
+ draft.fromKey,
242
+ WalletKeyLib.walletId(to),
243
+ draft.amountMin,
244
+ draft.amountMax,
245
+ spender
246
+ );
247
+ require(_spenderAllowed(ctx), ErrorsLib.ValidationSpenderRefused(spender));
248
+ }
249
+
250
+ /// @dev Turns a settled draft into an issued validation, see {TransferValidationLib-issue}, then sends
251
+ /// one leg per satellite chain involved.
252
+ function _issueValidation(
253
+ bytes calldata from,
254
+ bytes calldata to,
255
+ bytes calldata spender,
256
+ TransferValidationLib.Draft memory draft
257
+ ) private returns (uint256 validationId) {
258
+ bytes memory body;
259
+ (validationId, body) = TransferValidationLib.issue(from, to, spender, draft, address(_boundToken()));
260
+
261
+ _dispatchLeg(draft.fromChainKey, validationId, body);
262
+ if (draft.twoLegs) _dispatchLeg(draft.toChainKey, validationId, body);
263
+ }
264
+
265
+ /* ----- Settlement ----- */
266
+
267
+ /// @dev Classifies an attributed settlement against the stored validation. Reverts on a leg that does not
268
+ /// match; returns `true` on the two emergencies (a replayed leg, a never-issued id), which write nothing and
269
+ /// halt the token; reconciles a `Discarded` validation late, with the warning and the per-chain pause on a
270
+ /// breach. See {ISettlementHandler}.
271
+ function _handleSettlement(bytes32 originChainKey, MessageTypesLib.SettlementNotification calldata notification)
272
+ internal
273
+ returns (bool halt)
274
+ {
275
+ TransferValidationLib.ValidationStorage storage store = _validationStorage();
276
+ if (!_isIssued(store, notification.validationId)) {
277
+ emit EventsLib.ReplayedSettlement(notification.validationId, originChainKey);
278
+ return true;
279
+ }
280
+ Validation storage validation = store.validations[notification.validationId];
281
+
282
+ // 1. Which leg this is, and that its amount is inside the issued range.
283
+ Leg leg = _matchLeg(validation, originChainKey, notification);
284
+ require(
285
+ validation.amountMin <= notification.amount && notification.amount <= validation.amountMax,
286
+ ErrorsLib.SettlementOutOfBounds(notification.validationId, notification.amount)
287
+ );
288
+ if (_alreadyAccountedFor(validation.status, leg)) {
289
+ emit EventsLib.ReplayedSettlement(notification.validationId, originChainKey);
290
+ return true;
291
+ }
292
+
293
+ // 2. Take in what this chain reported: complete the movement, or wait for the other chain.
294
+ bool late = _wasDiscarded(validation.status);
295
+ bool breachesRule = _applyChainReport(validation, leg, notification, originChainKey, late);
296
+
297
+ // 3. A late leg always warns; the chain pauses only when the recorded state breaches a rule, since late
298
+ // delivery is cheap to force and pausing on it would be a denial of service.
299
+ if (late) {
300
+ emit EventsLib.LateReconciliation(notification.validationId, originChainKey);
301
+ if (breachesRule) _setIssuancePaused(originChainKey, true);
302
+ }
303
+ }
304
+
305
+ /// @dev Takes in what one chain reported and moves the validation on.
306
+ ///
307
+ /// When one chain was doing the whole movement, its report completes it. When two chains are involved,
308
+ /// the first report to arrive is recorded and the validation waits, keeping that report's amount and
309
+ /// wallet for the other chain to match; the second report completes the movement.
310
+ ///
311
+ /// Only the report that completes the movement consults the rules, so a validation can produce at most
312
+ /// one finding.
313
+ function _applyChainReport(
314
+ Validation storage validation,
315
+ Leg leg,
316
+ MessageTypesLib.SettlementNotification calldata notification,
317
+ bytes32 originChainKey,
318
+ bool late
319
+ ) private returns (bool breachesRule) {
320
+ bool isBurnLeg = leg == Leg.Burn;
321
+ bool isSingleLeg = leg == Leg.Single;
322
+ (ValidationStatus next, bool completesMovement) = _stateAfterLeg(validation.status, isBurnLeg, isSingleLeg);
323
+
324
+ if (!completesMovement) {
325
+ _recordFirstLeg(
326
+ validation,
327
+ isBurnLeg ? notification.from : notification.to,
328
+ isBurnLeg,
329
+ notification,
330
+ originChainKey,
331
+ next
332
+ );
333
+ return false;
334
+ }
335
+
336
+ // The movement completes here. On a pair, the wallet the first leg carried is the far side of it.
337
+ bytes memory from = notification.from;
338
+ bytes memory to = notification.to;
339
+ if (!isSingleLeg) {
340
+ if (isBurnLeg) to = validation.legWallet;
341
+ else from = validation.legWallet;
342
+ }
343
+ return _settleValidation(validation, from, to, notification, originChainKey, late, next, !isSingleLeg);
344
+ }
345
+
346
+ /// @dev A leg is matched by the wallets it carries and the chain it comes from. A one-leg validation expects
347
+ /// both wallets, the issued ones, from `from`'s chain. A two-leg validation expects the burn leg, `to` empty,
348
+ /// from `from`'s chain, and the mint leg, `from` empty, from `to`'s chain; a leg carrying both wallets is
349
+ /// neither.
350
+ function _matchLeg(
351
+ Validation storage validation,
352
+ bytes32 originChainKey,
353
+ MessageTypesLib.SettlementNotification calldata notification
354
+ ) private view returns (Leg) {
355
+ bool fromMatches = notification.from.length != 0 && keccak256(notification.from) == validation.fromKey;
356
+ bool toMatches = notification.to.length != 0 && keccak256(notification.to) == validation.toKey;
357
+ if (!validation.twoLegs) {
358
+ bool originMatches = originChainKey == validation.fromChainKey;
359
+ require(
360
+ fromMatches && toMatches && originMatches, ErrorsLib.SettlementLegMismatch(notification.validationId)
361
+ );
362
+ return Leg.Single;
363
+ }
364
+ if (fromMatches && notification.to.length == 0 && originChainKey == validation.fromChainKey) return Leg.Burn;
365
+ if (toMatches && notification.from.length == 0 && originChainKey == validation.toChainKey) return Leg.Mint;
366
+ revert ErrorsLib.SettlementLegMismatch(notification.validationId);
367
+ }
368
+
369
+ /// @dev The first of the two chains to report, whichever it is: the amount and the wallet it carries are
370
+ /// kept for the other one, and the validation is pinned. A first burn leg is proof the sender's satellite position is gone, so
371
+ /// the amount leaves that position and waits in transit on the ledger, and the wallet's pending amount stops
372
+ /// counting it; a first mint leg moves nothing until the burn leg lands, and the pair is then applied
373
+ /// atomically. A late first leg stays `Discarded`, its flag set, and holds all the same: the burn is final
374
+ /// either way.
375
+ function _recordFirstLeg(
376
+ Validation storage validation,
377
+ bytes calldata wallet,
378
+ bool burn,
379
+ MessageTypesLib.SettlementNotification calldata notification,
380
+ bytes32 originChainKey,
381
+ ValidationStatus next
382
+ ) private {
383
+ validation.executedAmount = notification.amount;
384
+ validation.legWallet = wallet;
385
+
386
+ // A burn leg takes the tokens out of the wallet, and the hold gives back whatever this validation had
387
+ // reserved against it at the same time. So when there is something to give back, the hold carries it
388
+ // and `_moveTo` leaves the wallet's half alone; otherwise `_moveTo` releases it the usual way.
389
+ uint256 releasedByTheHold = 0;
390
+ if (burn && _stillReservesWallet(validation.status)) {
391
+ releasedByTheHold = validation.amountMax;
392
+ }
393
+
394
+ _moveTo(validation, next, releasedByTheHold > 0);
395
+
396
+ if (burn) _holdOnToken(wallet, notification.amount, notification.validationId, releasedByTheHold);
397
+
398
+ emit EventsLib.ValidationLegConfirmed(notification.validationId, originChainKey, notification.amount);
399
+ }
400
+
401
+ /// @dev The movement a settled validation turns out to be, as the modules are asked about it. Its own
402
+ /// function because the settlement path holds enough live values without it, and the legacy pipeline runs
403
+ /// out of stack there under `forge coverage`.
404
+ ///
405
+ /// A native recipient may have changed owner since issuance, so it is resolved again here, through the
406
+ /// registry; a wallet the registry no longer names keeps the identity it was issued to. The token has not
407
+ /// been credited yet at this point, which is why nothing is adjusted for this very movement.
408
+ function _settledMovement(Validation storage validation, bytes memory from, bytes memory to, uint256 amount)
409
+ private
410
+ returns (IModule.TransferContext memory)
411
+ {
412
+ address toIdentity = validation.toIdentity;
413
+ (bool toNative, address toWallet) = WalletKeyLib.isReferenceChain(to);
414
+ if (toNative) {
415
+ address registryIdentity = address(_boundRegistry().identity(toWallet));
416
+ if (registryIdentity == address(0)) registryIdentity = toIdentity;
417
+ toIdentity = _currentOwner(toWallet, registryIdentity, 0);
418
+ }
419
+
420
+ return TransferContextLib.settlement(
421
+ address(this),
422
+ validation.fromIdentity,
423
+ toIdentity,
424
+ WalletKeyLib.walletId(from),
425
+ WalletKeyLib.walletId(to),
426
+ amount,
427
+ validation.spender
428
+ );
429
+ }
430
+
431
+ /// @dev Every expected leg is in, so the movement completes: release whatever is still reserved, move the
432
+ /// positions and the token's ledger, mark the validation, announce it, then tell the tracker modules.
433
+ ///
434
+ /// A late settlement is applied all the same, since the satellite already executed it and nothing on this
435
+ /// chain can undo that. What a late one additionally produces is a verdict, see {_exceedsWhatRulesAllowNow},
436
+ /// and that verdict is what decides whether the chain that sent it stops issuing.
437
+ ///
438
+ /// A tracker that reverts here reverts the delivery, and the delivery stays deliverable. That is deliberate:
439
+ /// swallowing the revert would leave that module's counter silently wrong, which no later message can
440
+ /// repair, while a refused delivery is repaired by `forceRemoveModule` and a redelivery.
441
+ function _settleValidation(
442
+ Validation storage validation,
443
+ bytes memory from,
444
+ bytes memory to,
445
+ MessageTypesLib.SettlementNotification calldata notification,
446
+ bytes32 originChainKey,
447
+ bool late,
448
+ ValidationStatus next,
449
+ bool secondLeg
450
+ ) private returns (bool breachesRule) {
451
+ if (secondLeg) _requireRepeatsFirstLeg(validation, notification);
452
+
453
+ validation.executedAmount = notification.amount;
454
+ _moveTo(validation, next, false);
455
+
456
+ IModule.TransferContext memory ctx = _settledMovement(validation, from, to, notification.amount);
457
+ breachesRule = late && _exceedsWhatRulesAllowNow(ctx, notification.amount);
458
+
459
+ _movePosition(ctx.fromIdentity, ctx.toIdentity, ctx.fromWallet, ctx.toWallet, notification.amount);
460
+ _settleOnToken(from, to, notification.amount, notification.validationId);
461
+
462
+ emit EventsLib.ValidationSettled(notification.validationId, originChainKey, notification.amount);
463
+ _callAfterTransfer(ctx);
464
+ }
465
+
466
+ /// @dev Both chains must report the same amount: the first one to arrive recorded it, the second has to
467
+ /// repeat it. A mismatch means the two chains executed different movements under one id, which no ledger
468
+ /// can reconcile, so it is refused.
469
+ function _requireRepeatsFirstLeg(
470
+ Validation storage validation,
471
+ MessageTypesLib.SettlementNotification calldata notification
472
+ ) private view {
473
+ require(
474
+ notification.amount == validation.executedAmount,
475
+ ErrorsLib.SettlementAmountMismatch(
476
+ notification.validationId, validation.executedAmount, notification.amount
477
+ )
478
+ );
479
+ }
480
+
481
+ /// @dev Whether an amount a satellite already executed is more than the rules would allow today.
482
+ ///
483
+ /// Asked only of a settlement that arrives after its validation was discarded. The tokens moved on the
484
+ /// other chain regardless, so this is not permission, it is a finding: the answer decides whether the
485
+ /// chain that sent it is stopped from issuing until someone looks into it.
486
+ ///
487
+ /// Asked once the reservation is released, so the rules judge the state the movement actually lands on. A
488
+ /// relocation between two wallets of one identity moves no position, so no distribution rule applies to it.
489
+ function _exceedsWhatRulesAllowNow(IModule.TransferContext memory ctx, uint256 amount) private view returns (bool) {
490
+ if (_isRelocation(ctx.fromIdentity, ctx.toIdentity)) return false;
491
+ return amount > _minAllowedAmount(ctx);
492
+ }
493
+
494
+ /* ----- Discard ----- */
495
+
496
+ /// @dev The way out of a pair of which one leg never arrived. The gate is a second reconciliation window
497
+ /// past `releaseAt`, the same length as the first, so a leg that is merely slow still lands as a late
498
+ /// reconciliation and never as a resolution.
499
+ ///
500
+ /// When the burn leg is the one that landed, the tokens it burned are waiting in transit: they go back to
501
+ /// the wallet they left. When it was the mint leg, nothing on this chain can be returned, so the chain
502
+ /// that owes the burn stops being issued to instead and an operator investigates. Either way the
503
+ /// validation ends in `Resolved`, where any leg arriving afterwards halts the token.
504
+ function _resolveStuckValidation(uint256 validationId) internal {
505
+ TransferValidationLib.ValidationStorage storage store = _validationStorage();
506
+ require(_isIssued(store, validationId), ErrorsLib.UnknownValidation(validationId));
507
+ Validation storage validation = store.validations[validationId];
508
+ require(
509
+ _awaitingOtherLeg(validation.status), ErrorsLib.ValidationNotStuck(validationId, uint8(validation.status))
510
+ );
511
+
512
+ uint64 resolvableAt = _secondWindowEnd(validation);
513
+ require(block.timestamp > resolvableAt, ErrorsLib.ValidationNotYetResolvable(validationId, resolvableAt));
514
+
515
+ uint256 returnedAmount = 0;
516
+ if (_burnArrived(validation.status)) {
517
+ returnedAmount = validation.executedAmount;
518
+ // A validation issued with a zero minimum may burn nothing, and a burn of nothing held nothing in
519
+ // transit: there is nothing to give back, and the token refuses to return an empty hold.
520
+ if (returnedAmount != 0) _returnHeldOnToken(validation.legWallet, validationId);
521
+ } else {
522
+ _setIssuancePaused(validation.fromChainKey, true);
523
+ }
524
+
525
+ _moveTo(validation, ValidationStatus.Resolved, false);
526
+
527
+ emit EventsLib.ValidationResolved(validationId, returnedAmount);
528
+ }
529
+
530
+ /// @dev Each id must be stored `Pending` and past `releaseAt`; one still awaiting a leg is refused whatever
531
+ /// the clock says. One refused id reverts the whole batch.
532
+ /// @dev Who may: the keeper from `releaseAt`, anyone from the end of the second window. A discard turns a
533
+ /// leg that lands afterwards into a late reconciliation, which can pause its chain's issuance, so during the
534
+ /// first window the keeper alone judges that a leg is late rather than slow. Past the second window a pair
535
+ /// would be resolvable anyway, so whoever is left waiting on the reservation may release it, keeper or not.
536
+ function _discardExpiredValidations(uint256[] calldata validationIds) internal {
537
+ TransferValidationLib.ValidationStorage storage store = _validationStorage();
538
+ bool keeper = _canCallSelector(msg.sender, this.discardExpiredValidations.selector);
539
+ for (uint256 i = 0; i < validationIds.length; i++) {
540
+ uint256 validationId = validationIds[i];
541
+ require(_isIssued(store, validationId), ErrorsLib.UnknownValidation(validationId));
542
+ Validation storage validation = store.validations[validationId];
543
+ require(
544
+ validation.status == ValidationStatus.Pending,
545
+ ErrorsLib.ValidationNotDiscardable(validationId, uint8(validation.status))
546
+ );
547
+ require(
548
+ block.timestamp > validation.releaseAt,
549
+ ErrorsLib.ValidationNotReleasable(validationId, validation.releaseAt)
550
+ );
551
+ if (!keeper) {
552
+ uint64 publicAt = _secondWindowEnd(validation);
553
+ require(block.timestamp > publicAt, ErrorsLib.ValidationNotPubliclyDiscardable(validationId, publicAt));
554
+ }
555
+
556
+ _moveTo(validation, ValidationStatus.Discarded, false);
557
+
558
+ emit EventsLib.ValidationDiscarded(validationId);
559
+ }
560
+ }
561
+
562
+ /// @dev The end of the second reconciliation window: one more window past `releaseAt`, the same length as
563
+ /// the first. Resolution and the public discard both open here.
564
+ function _secondWindowEnd(Validation storage validation) private view returns (uint64) {
565
+ return validation.releaseAt + (validation.releaseAt - validation.expiry);
566
+ }
567
+
568
+ /* ----- Views ----- */
569
+
570
+ /// @inheritdoc ITransferValidation
571
+ function defaultValidityWindow() public view returns (uint64) {
572
+ return _validationStorage().defaultValidityWindow;
573
+ }
574
+
575
+ /// @inheritdoc ITransferValidation
576
+ function reconciliationWindowOf(bytes32 chainKey) public view returns (uint64) {
577
+ return _validationStorage().reconciliationWindows[chainKey];
578
+ }
579
+
580
+ /// @inheritdoc ITransferValidation
581
+ function isIssuancePaused(bytes32 chainKey) public view returns (bool) {
582
+ return _validationStorage().issuancePaused[chainKey];
583
+ }
584
+
585
+ /// @inheritdoc ITransferValidation
586
+ function lastValidationId() public view returns (uint256) {
587
+ return _validationStorage().lastValidationId;
588
+ }
589
+
590
+ /// @inheritdoc ITransferValidation
591
+ function validationOf(uint256 validationId) public view returns (Validation memory) {
592
+ return _validationStorage().validations[validationId];
593
+ }
594
+
595
+ /// @inheritdoc ITransferValidation
596
+ function statusOf(uint256 validationId) public view returns (ValidationStatus) {
597
+ TransferValidationLib.ValidationStorage storage store = _validationStorage();
598
+ require(_isIssued(store, validationId), ErrorsLib.UnknownValidation(validationId));
599
+ Validation storage validation = store.validations[validationId];
600
+ // `Expired` is the one status never stored: it is `Pending` once the clock has passed `releaseAt`. Every
601
+ // other state is returned as it stands, including which leg a pair is still waiting for.
602
+ if (validation.status == ValidationStatus.Pending && block.timestamp > validation.releaseAt) {
603
+ return ValidationStatus.Expired;
604
+ }
605
+ return validation.status;
606
+ }
607
+
608
+ /* ----- Settings ----- */
609
+
610
+ function _setDefaultValidityWindow(uint64 duration) internal {
611
+ require(duration != 0, ErrorsLib.ZeroDuration());
612
+ _validationStorage().defaultValidityWindow = duration;
613
+ emit EventsLib.DefaultValidityWindowSet(duration);
614
+ }
615
+
616
+ function _setReconciliationWindow(bytes32 chainKey, uint64 duration) internal {
617
+ require(duration != 0, ErrorsLib.ZeroDuration());
618
+ _validationStorage().reconciliationWindows[chainKey] = duration;
619
+ emit EventsLib.ReconciliationWindowSet(chainKey, duration);
620
+ }
621
+
622
+ /// @dev Idempotent: the same state again changes nothing and emits nothing.
623
+ function _setIssuancePaused(bytes32 chainKey, bool paused) internal {
624
+ TransferValidationLib.ValidationStorage storage store = _validationStorage();
625
+ if (store.issuancePaused[chainKey] == paused) return;
626
+ store.issuancePaused[chainKey] = paused;
627
+ if (paused) emit EventsLib.ValidationIssuancePaused(chainKey);
628
+ else emit EventsLib.ValidationIssuanceUnpaused(chainKey);
629
+ }
630
+
631
+ /* ----- What the compliance provides ----- */
632
+
633
+ /// @dev The token this compliance is bound to.
634
+ function _boundToken() internal view virtual returns (IToken);
635
+
636
+ /// @dev The registry of the bound token.
637
+ function _boundRegistry() internal view virtual returns (ITREXRegistry);
638
+
639
+ /// @dev Whether the AccessManager lets `caller` call `selector` on this contract now.
640
+ function _canCallSelector(address caller, bytes4 selector) internal view virtual returns (bool);
641
+
642
+ /// @dev The smallest amount any `RULE` module allows for `ctx`, `type(uint256).max` when none is bound.
643
+ /// Evaluated under `staticcall`: the ledger still describes the state before the move.
644
+ function _minAllowedAmount(IModule.TransferContext memory ctx) internal view virtual returns (uint256);
645
+
646
+ /// @dev Whether every `SPENDER` module accepts `ctx.spender`, `true` when none is bound. A view: the
647
+ /// modules are called under `staticcall`.
648
+ function _spenderAllowed(IModule.TransferContext memory ctx) internal view virtual returns (bool);
649
+
650
+ /// @dev Calls `afterTransfer` on every `TRACKER` module, once the positions have been updated.
651
+ function _callAfterTransfer(IModule.TransferContext memory ctx) internal virtual;
652
+
653
+ /// @dev Sends one leg of a validation toward `chainKey`, through the token.
654
+ function _dispatchLeg(bytes32 chainKey, uint256 validationId, bytes memory body) internal virtual;
655
+
656
+ /// @dev Applies a settled validation to the ledger, through the token, once.
657
+ function _settleOnToken(bytes memory from, bytes memory to, uint256 amount, uint256 validationId) internal virtual;
658
+
659
+ /// @dev Takes the burned amount out of `from`'s position and holds it in transit, through the token, when
660
+ /// the burn leg of a two-leg validation lands before the mint leg. `reserved` is what this validation had
661
+ /// reserved against the wallet, which the token gives back as it takes the hold.
662
+ function _holdOnToken(bytes memory from, uint256 amount, uint256 validationId, uint256 reserved) internal virtual;
663
+
664
+ /// @dev Gives back what this validation reserved against a satellite wallet, on the token.
665
+ function _releaseOnToken(bytes memory wallet, uint256 amount) internal virtual;
666
+
667
+ /// @dev Puts the amount held for `validationId` back on the wallet it was burned from, on the token.
668
+ function _returnHeldOnToken(bytes memory to, uint256 validationId) internal virtual;
669
+
670
+ /* ----- Shared by the three flows ----- */
671
+
672
+ /* ----- The lifecycle, in one place ----- */
673
+
674
+ /// @dev Whether the burn leg has arrived in this state.
675
+ function _burnArrived(ValidationStatus status) internal pure returns (bool) {
676
+ return status == ValidationStatus.AwaitingMint || status == ValidationStatus.DiscardedAwaitingMint
677
+ || status == ValidationStatus.Settled || status == ValidationStatus.LateReconciled;
678
+ }
679
+
680
+ /// @dev Whether the mint leg has arrived in this state.
681
+ function _mintArrived(ValidationStatus status) internal pure returns (bool) {
682
+ return status == ValidationStatus.AwaitingBurn || status == ValidationStatus.DiscardedAwaitingBurn
683
+ || status == ValidationStatus.Settled || status == ValidationStatus.LateReconciled;
684
+ }
685
+
686
+ /// @dev Whether this leg has nothing left to do: either its own half already arrived, or the keeper gave up
687
+ /// on the pair and sent the tokens back. Both mean the amount it carries is already accounted for
688
+ /// somewhere else, so applying it again would move tokens twice. The caller treats it as an emergency.
689
+ function _alreadyAccountedFor(ValidationStatus status, Leg leg) internal pure returns (bool) {
690
+ if (status == ValidationStatus.Resolved) return true;
691
+ if (leg == Leg.Mint) return _mintArrived(status);
692
+ return _burnArrived(status);
693
+ }
694
+
695
+ /// @dev Whether the keeper has given up on this validation, in any of the states that say so.
696
+ function _wasDiscarded(ValidationStatus status) internal pure returns (bool) {
697
+ return status == ValidationStatus.Discarded || status == ValidationStatus.DiscardedAwaitingMint
698
+ || status == ValidationStatus.DiscardedAwaitingBurn;
699
+ }
700
+
701
+ /// @dev Whether exactly one leg of a pair has arrived and the other is still owed. Such a validation can be
702
+ /// neither discarded nor derived `Expired`: one satellite has already executed irreversibly.
703
+ function _awaitingOtherLeg(ValidationStatus status) internal pure returns (bool) {
704
+ return status == ValidationStatus.AwaitingMint || status == ValidationStatus.AwaitingBurn
705
+ || status == ValidationStatus.DiscardedAwaitingMint || status == ValidationStatus.DiscardedAwaitingBurn;
706
+ }
707
+
708
+ /// @dev Whether the validation still counts in the ledger's pending amounts in this state: the total for
709
+ /// every validation, the identities' rows for every one that is not a relocation.
710
+ function _stillReservesPending(ValidationStatus status) internal pure returns (bool) {
711
+ return status == ValidationStatus.Pending || status == ValidationStatus.AwaitingMint
712
+ || status == ValidationStatus.AwaitingBurn;
713
+ }
714
+
715
+ /// @dev Whether the sender wallet's pending amount is still reserved in this state. Once the burn leg has
716
+ /// arrived the tokens are in transit, so the wallet's cap must stop counting them.
717
+ function _stillReservesWallet(ValidationStatus status) internal pure returns (bool) {
718
+ return status == ValidationStatus.Pending || status == ValidationStatus.AwaitingBurn;
719
+ }
720
+
721
+ /// @dev Where a validation goes when a leg lands on it, and whether that leg completes the movement.
722
+ ///
723
+ /// A single-leg validation settles outright. A pair in `Pending` records the first leg and waits for the
724
+ /// other; the same pair already discarded waits in the matching `Discarded*` state instead, so that when
725
+ /// the second leg lands it reconciles late rather than settling. Any state this is not called for is a leg
726
+ /// for a leg already arrived, which the caller has refused as a replay.
727
+ ///
728
+ /// @param status where the validation stands
729
+ /// @param burnLeg the leg that landed is the one from the sender's chain
730
+ /// @param singleLeg the validation expects one leg, not two
731
+ /// @return next where it stands afterwards
732
+ /// @return completesMovement whether the movement is now complete, so positions move and rules are asked
733
+ function _stateAfterLeg(ValidationStatus status, bool burnLeg, bool singleLeg)
734
+ internal
735
+ pure
736
+ returns (ValidationStatus next, bool completesMovement)
737
+ {
738
+ bool discarded = _wasDiscarded(status);
739
+
740
+ if (singleLeg) {
741
+ return (discarded ? ValidationStatus.LateReconciled : ValidationStatus.Settled, true);
742
+ }
743
+ if (status == ValidationStatus.Pending) {
744
+ return (burnLeg ? ValidationStatus.AwaitingMint : ValidationStatus.AwaitingBurn, false);
745
+ }
746
+ if (status == ValidationStatus.Discarded) {
747
+ return (burnLeg ? ValidationStatus.DiscardedAwaitingMint : ValidationStatus.DiscardedAwaitingBurn, false);
748
+ }
749
+ // One leg was already in and the other has now landed: the pair is complete.
750
+ return (discarded ? ValidationStatus.LateReconciled : ValidationStatus.Settled, true);
751
+ }
752
+
753
+ /// @dev Moves a validation to `next` and gives back whatever `next` no longer reserves. Because the
754
+ /// reservations are read off the status, a release happens exactly when the move stops reserving something,
755
+ /// and arriving twice at the same state gives nothing back twice.
756
+ /// @param heldByTheHold true when the caller is about to take an in-transit hold, which gives the wallet's
757
+ /// reservation back itself; this function then leaves that half alone rather than releasing it twice.
758
+ function _moveTo(Validation storage validation, ValidationStatus next, bool heldByTheHold) private {
759
+ ValidationStatus current = validation.status;
760
+
761
+ if (_stillReservesPending(current) && !_stillReservesPending(next)) {
762
+ _releasePendingTotal(validation.amountMax);
763
+ // A relocation never wrote the identities' rows, so there is nothing to give back there.
764
+ bool identitiesWereReserved = !validation.relocation;
765
+ if (identitiesWereReserved) {
766
+ _releasePendingOfIdentities(validation.fromIdentity, validation.toIdentity, validation.amountMax);
767
+ }
768
+ }
769
+ if (_stillReservesWallet(current) && !_stillReservesWallet(next) && !heldByTheHold) {
770
+ _releaseOnToken(validation.fromWallet, validation.amountMax);
771
+ }
772
+
773
+ validation.status = next;
774
+ }
775
+
776
+ /// @dev Who a native wallet's tokens belong to now. An empty wallet follows the registry; a funded one is
777
+ /// refused until the registry names the identity the ledger credited again, since a balance changing identity
778
+ /// outside a movement is a position no rule ever checked. `adjustment` is what this very movement already did
779
+ /// to the wallet's balance on the token, so that what is judged is what the wallet held before it: `+amount`
780
+ /// for a sender the token already debited, `-amount` for a recipient it already credited, zero otherwise.
781
+ function _currentOwner(address wallet, address resolved, int256 adjustment) internal returns (address owner) {
782
+ address previous;
783
+ (owner, previous) = _followOwner(wallet, resolved);
784
+ if (previous == address(0)) return owner;
785
+ uint256 balance = uint256(int256(_boundToken().balanceOf(wallet)) + adjustment);
786
+ // A balance never changes identity without the rules seeing it, and the rules only see movements. So a
787
+ // funded wallet keeps the owner the ledger credited until that owner is named again or the wallet is empty.
788
+ bool walletIsEmpty = balance == 0;
789
+ require(walletIsEmpty, ErrorsLib.WalletBoundToAnotherIdentity(wallet, previous));
790
+ _recordOwnerChange(wallet, previous, owner);
791
+ }
792
+
793
+ /// @dev The reconciliation window of a chain that is open and configured.
794
+ function _openChainWindow(TransferValidationLib.ValidationStorage storage store, bytes32 chainKey)
795
+ private
796
+ view
797
+ returns (uint64 window)
798
+ {
799
+ require(!store.issuancePaused[chainKey], ErrorsLib.ValidationIssuancePaused(chainKey));
800
+ window = store.reconciliationWindows[chainKey];
801
+ require(window != 0, ErrorsLib.ReconciliationWindowNotSet(chainKey));
802
+ }
803
+
804
+ function _chainKeyOf(bytes calldata wallet) private pure returns (bytes32) {
805
+ (bytes2 chainType, bytes memory chainReference,) = WalletKeyLib.parse(wallet);
806
+ return MessageTypesLib.chainKey(chainType, chainReference);
807
+ }
808
+
809
+ /// @dev Ids start at 1 and `lastValidationId` is the last one issued.
810
+ function _isIssued(TransferValidationLib.ValidationStorage storage store, uint256 validationId)
811
+ private
812
+ view
813
+ returns (bool)
814
+ {
815
+ return validationId != 0 && validationId <= store.lastValidationId;
816
+ }
817
+
818
+ function _validationStorage() internal pure returns (TransferValidationLib.ValidationStorage storage) {
819
+ return TransferValidationLib.layout();
820
+ }
821
+
822
+ }