@unicitylabs/bridge-plugin 0.2.0-dev.1

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 (102) hide show
  1. package/DEMO.md +255 -0
  2. package/README.md +89 -0
  3. package/lib/BridgeLockJustification.d.ts +31 -0
  4. package/lib/BridgeLockJustification.js +41 -0
  5. package/lib/BridgeMintJustificationVerifier.d.ts +12 -0
  6. package/lib/BridgeMintJustificationVerifier.js +35 -0
  7. package/lib/LockMintJustificationVerifier.d.ts +42 -0
  8. package/lib/LockMintJustificationVerifier.js +108 -0
  9. package/lib/address.d.ts +13 -0
  10. package/lib/address.js +71 -0
  11. package/lib/bridge-back/abi.d.ts +30 -0
  12. package/lib/bridge-back/abi.js +89 -0
  13. package/lib/bridge-back/burn.d.ts +79 -0
  14. package/lib/bridge-back/burn.js +78 -0
  15. package/lib/bridge-back/burned-blob.d.ts +6 -0
  16. package/lib/bridge-back/burned-blob.js +16 -0
  17. package/lib/bridge-back/cbor.d.ts +32 -0
  18. package/lib/bridge-back/cbor.js +146 -0
  19. package/lib/bridge-back/derivations.d.ts +112 -0
  20. package/lib/bridge-back/derivations.js +201 -0
  21. package/lib/bridge-back/index.d.ts +11 -0
  22. package/lib/bridge-back/index.js +11 -0
  23. package/lib/cli/main.d.ts +2 -0
  24. package/lib/cli/main.js +149 -0
  25. package/lib/cli/scenario.d.ts +36 -0
  26. package/lib/cli/scenario.js +94 -0
  27. package/lib/config.d.ts +25 -0
  28. package/lib/config.js +1 -0
  29. package/lib/contract-call.d.ts +13 -0
  30. package/lib/contract-call.js +19 -0
  31. package/lib/evm/EvmRpcClient.d.ts +25 -0
  32. package/lib/evm/EvmRpcClient.js +54 -0
  33. package/lib/evm/config.d.ts +6 -0
  34. package/lib/evm/config.js +6 -0
  35. package/lib/evm/family.d.ts +3 -0
  36. package/lib/evm/family.js +21 -0
  37. package/lib/evm/index.d.ts +6 -0
  38. package/lib/evm/index.js +6 -0
  39. package/lib/evm/presentation.d.ts +4 -0
  40. package/lib/evm/presentation.js +21 -0
  41. package/lib/evm/providers.d.ts +3 -0
  42. package/lib/evm/providers.js +9 -0
  43. package/lib/evm/signer.d.ts +43 -0
  44. package/lib/evm/signer.js +91 -0
  45. package/lib/families.d.ts +4 -0
  46. package/lib/families.js +11 -0
  47. package/lib/family.d.ts +16 -0
  48. package/lib/family.js +1 -0
  49. package/lib/hex.d.ts +4 -0
  50. package/lib/hex.js +33 -0
  51. package/lib/identifiers.d.ts +14 -0
  52. package/lib/identifiers.js +22 -0
  53. package/lib/index.d.ts +48 -0
  54. package/lib/index.js +55 -0
  55. package/lib/lock-event.d.ts +28 -0
  56. package/lib/lock-event.js +48 -0
  57. package/lib/source-chain.d.ts +29 -0
  58. package/lib/source-chain.js +1 -0
  59. package/lib/tron/TronRpcClient.d.ts +39 -0
  60. package/lib/tron/TronRpcClient.js +100 -0
  61. package/lib/tron/config.d.ts +11 -0
  62. package/lib/tron/config.js +31 -0
  63. package/lib/tron/family.d.ts +4 -0
  64. package/lib/tron/family.js +18 -0
  65. package/lib/tron/presentation.d.ts +10 -0
  66. package/lib/tron/presentation.js +18 -0
  67. package/lib/tron/providers.d.ts +46 -0
  68. package/lib/tron/providers.js +32 -0
  69. package/lib/tron/signer.d.ts +166 -0
  70. package/lib/tron/signer.js +238 -0
  71. package/lib/value.d.ts +23 -0
  72. package/lib/value.js +58 -0
  73. package/lib/wallet/allowance.d.ts +20 -0
  74. package/lib/wallet/allowance.js +27 -0
  75. package/lib/wallet/backing.d.ts +2 -0
  76. package/lib/wallet/backing.js +15 -0
  77. package/lib/wallet/facade.d.ts +97 -0
  78. package/lib/wallet/facade.js +100 -0
  79. package/lib/wallet/finality.d.ts +8 -0
  80. package/lib/wallet/finality.js +28 -0
  81. package/lib/wallet/index.d.ts +25 -0
  82. package/lib/wallet/index.js +25 -0
  83. package/lib/wallet/manifest.d.ts +69 -0
  84. package/lib/wallet/manifest.js +81 -0
  85. package/lib/wallet/manifests.d.ts +31 -0
  86. package/lib/wallet/manifests.js +70 -0
  87. package/lib/wallet/payout.d.ts +5 -0
  88. package/lib/wallet/payout.js +14 -0
  89. package/lib/wallet/registry.d.ts +40 -0
  90. package/lib/wallet/registry.js +34 -0
  91. package/lib/wallet/return-client.d.ts +91 -0
  92. package/lib/wallet/return-client.js +100 -0
  93. package/lib/wallet/self-mint-verifier.d.ts +10 -0
  94. package/lib/wallet/self-mint-verifier.js +5 -0
  95. package/lib/wallet/signer.d.ts +18 -0
  96. package/lib/wallet/signer.js +1 -0
  97. package/lib/wallet/source-adapter.d.ts +33 -0
  98. package/lib/wallet/source-adapter.js +99 -0
  99. package/lib/wallet/token-plugin.d.ts +4 -0
  100. package/lib/wallet/token-plugin.js +20 -0
  101. package/manifests/bridges.nile.json +23 -0
  102. package/package.json +71 -0
@@ -0,0 +1,99 @@
1
+ import { BridgeLockJustification } from '../BridgeLockJustification.js';
2
+ import { fromHex } from '../hex.js';
3
+ import { decodeLockEvent } from '../lock-event.js';
4
+ import { encodeBridgePaymentData } from '../value.js';
5
+ import { queryAllowance } from './allowance.js';
6
+ import { buildBridgeInPlan } from './facade.js';
7
+ import { chainName } from './manifest.js';
8
+ import { selfMintVerifier } from './self-mint-verifier.js';
9
+ /**
10
+ * Build the {BridgeSourceAdapter} for a loaded bridge. Closes over the wallet
11
+ * (for the deposit steps) and a node client (for the allowance read); the mint
12
+ * verifier override is built with `deps`; by default it reads the wallet's value
13
+ * payload, which is the production bridge token value format.
14
+ */
15
+ export function createSourceAdapter(bridge, wallet, rpc, deps = {}) {
16
+ const cfg = bridge.plugin.resolvedConfig;
17
+ const vaultHex = cfg.lockContractHex;
18
+ const where = `${bridge.manifest.symbol} on ${chainName(bridge)}`;
19
+ return {
20
+ async prepareDeposit(params) {
21
+ const plan = await buildBridgeInPlan({
22
+ plugin: bridge.plugin,
23
+ amount: params.amount,
24
+ networkId: params.networkId,
25
+ recipientPubkey: params.recipientPubkey,
26
+ ownerPredicateCbor: params.ownerPredicateCbor,
27
+ approveAmount: params.approveAmount,
28
+ });
29
+ const recovery = {
30
+ tokenIdHex: plan.tokenIdHex,
31
+ saltHex: plan.saltHex,
32
+ recipientCommitmentHex: plan.recipientCommitmentHex,
33
+ coinIdHex: bridge.plugin.coinIdHex,
34
+ tokenTypeHex: bridge.plugin.tokenTypeHex,
35
+ chainId: bridge.manifest.chainId,
36
+ };
37
+ const lockStep = {
38
+ label: `Lock ${where}…`,
39
+ awaitReceipt: false, // the orchestrator waits for the commit receipt to decode it
40
+ send: () => wallet.sendCall(plan.lock),
41
+ };
42
+ // Skip the approval when the vault's allowance already covers the amount
43
+ // (08 §1.1). A read failure is treated as "approve" — a redundant approval
44
+ // is safe, a skipped-but-needed one is not.
45
+ let needApprove = true;
46
+ try {
47
+ const owner = await wallet.getAddress();
48
+ const allowance = await queryAllowance(rpc, {
49
+ assetAddress: cfg.assetContractHex,
50
+ owner,
51
+ spender: cfg.lockContractHex,
52
+ });
53
+ needApprove = allowance < params.amount;
54
+ }
55
+ catch {
56
+ needApprove = true;
57
+ }
58
+ if (!needApprove) {
59
+ return { recovery, steps: [lockStep], commitIndex: 0 };
60
+ }
61
+ const approveStep = {
62
+ label: `Approve ${where}…`,
63
+ awaitReceipt: true,
64
+ send: () => wallet.sendCall(plan.approve),
65
+ };
66
+ return { recovery, steps: [approveStep, lockStep], commitIndex: 1 };
67
+ },
68
+ decodeCommit(rawReceipt) {
69
+ const info = rawReceipt;
70
+ if (!info)
71
+ return null;
72
+ const logIndex = info.logs.findIndex((l) => l.address.toLowerCase() === vaultHex);
73
+ const decoded = logIndex >= 0 ? decodeLockEvent(info.logs[logIndex]) : null;
74
+ if (!decoded)
75
+ return null;
76
+ return { nonce: decoded.nonce, blockNumber: info.blockNumber, logIndex };
77
+ },
78
+ buildMintRequest({ saltHex, amount, commit, commitTxid }) {
79
+ const genesisReason = new BridgeLockJustification({
80
+ chainId: bridge.manifest.chainId,
81
+ lockContract: fromHex(cfg.lockContractHex),
82
+ assetContract: fromHex(cfg.assetContractHex),
83
+ txid: fromHex(commitTxid),
84
+ logIndex: commit.logIndex,
85
+ amount,
86
+ nonce: commit.nonce,
87
+ }).toCBOR();
88
+ return {
89
+ coinIdHex: bridge.plugin.coinIdHex,
90
+ amount,
91
+ mintData: encodeBridgePaymentData(cfg.coinId, amount),
92
+ tokenType: cfg.tokenType,
93
+ salt: fromHex(saltHex),
94
+ genesisReason,
95
+ mintJustificationVerifiers: [selfMintVerifier(bridge, deps)],
96
+ };
97
+ },
98
+ };
99
+ }
@@ -0,0 +1,4 @@
1
+ import type { WalletTokenPlugin } from '@unicitylabs/bridge-core';
2
+ import type { LoadedBridge } from './manifest.js';
3
+ export declare function bridgeTokenPlugin(bridge: LoadedBridge): WalletTokenPlugin;
4
+ export declare function mergeBridgeTokenPlugins(plugins: readonly WalletTokenPlugin[]): WalletTokenPlugin;
@@ -0,0 +1,20 @@
1
+ import { BridgeMintJustificationVerifier } from '../BridgeMintJustificationVerifier.js';
2
+ import { LockMintJustificationVerifier } from '../LockMintJustificationVerifier.js';
3
+ export function bridgeTokenPlugin(bridge) {
4
+ return {
5
+ id: `bridge:${bridge.manifest.chainRef}:${bridge.manifest.symbol.toLowerCase()}`,
6
+ mintJustificationVerifiers: [bridge.plugin.verifier],
7
+ };
8
+ }
9
+ export function mergeBridgeTokenPlugins(plugins) {
10
+ const verifiers = plugins.flatMap((p) => p.mintJustificationVerifiers);
11
+ for (const v of verifiers) {
12
+ if (!(v instanceof LockMintJustificationVerifier)) {
13
+ throw new Error(`mergeBridgeTokenPlugins: verifier for tag ${v.tag} is not a bridge lock verifier`);
14
+ }
15
+ }
16
+ return {
17
+ id: 'bridge',
18
+ mintJustificationVerifiers: [new BridgeMintJustificationVerifier(verifiers)],
19
+ };
20
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "description": "Sphere bridge manifest — Tron Nile USDT (M3/M4). Frozen from deployments/nile/nile-usdt.json; configHash equals the deployed vault CONFIG_HASH. returnServiceUrl is environment-specific.",
3
+ "bridges": [
4
+ {
5
+ "label": "USDT (bridged · Tron)",
6
+ "symbol": "USDT",
7
+ "chainId": 3448148188,
8
+ "vault": "TTKKLyhnRRQ7XV5vsRarV8xWWEvF9225mY",
9
+ "asset": "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf",
10
+ "confirmations": 20,
11
+ "decimals": 6,
12
+ "rpcUrl": "https://nile.trongrid.io",
13
+ "returnServiceUrl": "http://localhost:8787",
14
+ "reasonTag": 39048,
15
+ "lockDomain": "158b847f78b3910a5f5f42820de61abba1bf5ae1fbb29dabfba09118f393f932",
16
+ "nullifierDomain": "d4530e4ea58fc8e38f84506e62b421476c3eeec70f4cbebefc32688a510e2d5d",
17
+ "vkey": "0x00c34ae0ebb63e86218a754892813f4744b2f6c9ed613c085ea40999b16ce3ad",
18
+ "configHash": "7f376b16b3bff3455f375e7cf30b9d29d2a14332912f0ffb69d78e1b31d5193f",
19
+ "tokenTypeHex": "6f2d10d27abeb4960a7ef19370c965ec090bb4da1f17752be77334e2dde19c74",
20
+ "coinIdHex": "f1634862e1b932acd1c791a1860c62f69c7f55aa6c6115ba631d3bf4a9d8ddbb"
21
+ }
22
+ ]
23
+ }
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@unicitylabs/bridge-plugin",
3
+ "version": "0.2.0-dev.1",
4
+ "description": "Unicity bridge plugin: verifies bridged tokens against their source-chain lock (Tron and Ethereum-family chains) and drives bridge-in and bridge-out for a wallet.",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "files": [
9
+ "lib",
10
+ "manifests",
11
+ "README.md",
12
+ "DEMO.md",
13
+ "LICENSE"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./lib/index.d.ts",
18
+ "default": "./lib/index.js"
19
+ },
20
+ "./wallet": {
21
+ "types": "./lib/wallet/index.d.ts",
22
+ "default": "./lib/wallet/index.js"
23
+ },
24
+ "./bridge-back": {
25
+ "types": "./lib/bridge-back/index.d.ts",
26
+ "default": "./lib/bridge-back/index.js"
27
+ },
28
+ "./lib/*.js": {
29
+ "types": "./lib/*.d.ts",
30
+ "default": "./lib/*.js"
31
+ },
32
+ "./lib/wallet/*.js": {
33
+ "types": "./lib/wallet/*.d.ts",
34
+ "default": "./lib/wallet/*.js"
35
+ },
36
+ "./lib/bridge-back/*.js": {
37
+ "types": "./lib/bridge-back/*.d.ts",
38
+ "default": "./lib/bridge-back/*.js"
39
+ }
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/unicitynetwork/unicity-bridge.git",
44
+ "directory": "packages/bridge-plugin"
45
+ },
46
+ "bin": {
47
+ "tron-usdt-bridge": "./lib/cli/main.js"
48
+ },
49
+ "scripts": {
50
+ "build": "tsc --project tsconfig.json",
51
+ "typecheck": "tsc --noEmit",
52
+ "test": "tsx --test test/*.test.ts",
53
+ "cli": "tsx src/cli/main.ts",
54
+ "e2e": "tsx demo/e2e.ts",
55
+ "e2e:back": "tsx demo/bridge-back-e2e.ts"
56
+ },
57
+ "engines": {
58
+ "node": ">=22"
59
+ },
60
+ "dependencies": {
61
+ "@noble/hashes": "2.2.0",
62
+ "@unicitylabs/bridge-core": "0.2.0-dev.1",
63
+ "@unicitylabs/state-transition-sdk": "3.0.1"
64
+ },
65
+ "devDependencies": {
66
+ "@types/node": "^22.0.0",
67
+ "tronweb": "^6.3.0",
68
+ "tsx": "^4.19.0",
69
+ "typescript": "^5.6.0"
70
+ }
71
+ }