@tonconnect/sdk 2.0.7 → 2.1.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.
- package/README.md +81 -9
- package/dist/tonconnect-sdk.min.js +1 -1
- package/dist/tonconnect-sdk.min.js.map +1 -1
- package/lib/cjs/index.cjs +1627 -0
- package/lib/cjs/index.cjs.map +1 -0
- package/lib/esm/index.mjs +1597 -0
- package/lib/esm/index.mjs.map +1 -0
- package/lib/types/index.d.ts +601 -8
- package/package.json +15 -15
- package/lib/cjs/errors/address/index.js +0 -6
- package/lib/cjs/errors/address/index.js.map +0 -1
- package/lib/cjs/errors/address/wrong-address.error.js +0 -15
- package/lib/cjs/errors/address/wrong-address.error.js.map +0 -1
- package/lib/cjs/errors/binary/index.js +0 -6
- package/lib/cjs/errors/binary/index.js.map +0 -1
- package/lib/cjs/errors/binary/parse-hex.error.js +0 -15
- package/lib/cjs/errors/binary/parse-hex.error.js.map +0 -1
- package/lib/cjs/errors/dapp/dapp-metadata.error.js +0 -15
- package/lib/cjs/errors/dapp/dapp-metadata.error.js.map +0 -1
- package/lib/cjs/errors/index.js +0 -28
- package/lib/cjs/errors/index.js.map +0 -1
- package/lib/cjs/errors/protocol/events/connect/index.js +0 -6
- package/lib/cjs/errors/protocol/events/connect/index.js.map +0 -1
- package/lib/cjs/errors/protocol/events/connect/manifest-content-error.error.js +0 -16
- package/lib/cjs/errors/protocol/events/connect/manifest-content-error.error.js.map +0 -1
- package/lib/cjs/errors/protocol/events/connect/manifest-not-found.error.js +0 -16
- package/lib/cjs/errors/protocol/events/connect/manifest-not-found.error.js.map +0 -1
- package/lib/cjs/errors/protocol/events/connect/user-rejects.error.js +0 -15
- package/lib/cjs/errors/protocol/events/connect/user-rejects.error.js.map +0 -1
- package/lib/cjs/errors/protocol/events/index.js +0 -18
- package/lib/cjs/errors/protocol/events/index.js.map +0 -1
- package/lib/cjs/errors/protocol/index.js +0 -19
- package/lib/cjs/errors/protocol/index.js.map +0 -1
- package/lib/cjs/errors/protocol/responses/bad-request.error.js +0 -15
- package/lib/cjs/errors/protocol/responses/bad-request.error.js.map +0 -1
- package/lib/cjs/errors/protocol/responses/index.js +0 -8
- package/lib/cjs/errors/protocol/responses/index.js.map +0 -1
- package/lib/cjs/errors/protocol/responses/unknown-app.error.js +0 -15
- package/lib/cjs/errors/protocol/responses/unknown-app.error.js.map +0 -1
- package/lib/cjs/errors/storage/index.js +0 -6
- package/lib/cjs/errors/storage/index.js.map +0 -1
- package/lib/cjs/errors/storage/localstorage-not-found.error.js +0 -15
- package/lib/cjs/errors/storage/localstorage-not-found.error.js.map +0 -1
- package/lib/cjs/errors/ton-connect.error.js +0 -18
- package/lib/cjs/errors/ton-connect.error.js.map +0 -1
- package/lib/cjs/errors/unknown.error.js +0 -15
- package/lib/cjs/errors/unknown.error.js.map +0 -1
- package/lib/cjs/errors/wallet/index.js +0 -10
- package/lib/cjs/errors/wallet/index.js.map +0 -1
- package/lib/cjs/errors/wallet/wallet-already-connected.error.js +0 -15
- package/lib/cjs/errors/wallet/wallet-already-connected.error.js.map +0 -1
- package/lib/cjs/errors/wallet/wallet-not-connected.error.js +0 -15
- package/lib/cjs/errors/wallet/wallet-not-connected.error.js.map +0 -1
- package/lib/cjs/errors/wallet/wallet-not-injected.error.js +0 -15
- package/lib/cjs/errors/wallet/wallet-not-injected.error.js.map +0 -1
- package/lib/cjs/errors/wallet/wallet-not-support-feature.error.js +0 -15
- package/lib/cjs/errors/wallet/wallet-not-support-feature.error.js.map +0 -1
- package/lib/cjs/errors/wallets-manager/fetch-wallets.error.js +0 -15
- package/lib/cjs/errors/wallets-manager/fetch-wallets.error.js.map +0 -1
- package/lib/cjs/errors/wallets-manager/index.js +0 -6
- package/lib/cjs/errors/wallets-manager/index.js.map +0 -1
- package/lib/cjs/index.js +0 -27
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/models/dapp/dapp-metadata.js +0 -3
- package/lib/cjs/models/dapp/dapp-metadata.js.map +0 -1
- package/lib/cjs/models/index.js +0 -19
- package/lib/cjs/models/index.js.map +0 -1
- package/lib/cjs/models/methods/connect/connect-additional-request.js +0 -3
- package/lib/cjs/models/methods/connect/connect-additional-request.js.map +0 -1
- package/lib/cjs/models/methods/connect/index.js +0 -3
- package/lib/cjs/models/methods/connect/index.js.map +0 -1
- package/lib/cjs/models/methods/index.js +0 -19
- package/lib/cjs/models/methods/index.js.map +0 -1
- package/lib/cjs/models/methods/send-transaction/index.js +0 -3
- package/lib/cjs/models/methods/send-transaction/index.js.map +0 -1
- package/lib/cjs/models/methods/send-transaction/send-transaction-request.js +0 -3
- package/lib/cjs/models/methods/send-transaction/send-transaction-request.js.map +0 -1
- package/lib/cjs/models/methods/send-transaction/send-transaction-response.js +0 -3
- package/lib/cjs/models/methods/send-transaction/send-transaction-response.js.map +0 -1
- package/lib/cjs/models/ton-connect-options.js +0 -3
- package/lib/cjs/models/ton-connect-options.js.map +0 -1
- package/lib/cjs/models/wallet/account.js +0 -3
- package/lib/cjs/models/wallet/account.js.map +0 -1
- package/lib/cjs/models/wallet/index.js +0 -6
- package/lib/cjs/models/wallet/index.js.map +0 -1
- package/lib/cjs/models/wallet/wallet-connection-source.js +0 -8
- package/lib/cjs/models/wallet/wallet-connection-source.js.map +0 -1
- package/lib/cjs/models/wallet/wallet-info.js +0 -8
- package/lib/cjs/models/wallet/wallet-info.js.map +0 -1
- package/lib/cjs/models/wallet/wallet.js +0 -3
- package/lib/cjs/models/wallet/wallet.js.map +0 -1
- package/lib/cjs/parsers/connect-errors-parser.js +0 -27
- package/lib/cjs/parsers/connect-errors-parser.js.map +0 -1
- package/lib/cjs/parsers/rpc-parser.js +0 -10
- package/lib/cjs/parsers/rpc-parser.js.map +0 -1
- package/lib/cjs/parsers/send-transaction-parser.js +0 -35
- package/lib/cjs/parsers/send-transaction-parser.js.map +0 -1
- package/lib/cjs/provider/bridge/bridge-gateway.js +0 -110
- package/lib/cjs/provider/bridge/bridge-gateway.js.map +0 -1
- package/lib/cjs/provider/bridge/bridge-provider.js +0 -147
- package/lib/cjs/provider/bridge/bridge-provider.js.map +0 -1
- package/lib/cjs/provider/bridge/models/bridge-connection.js +0 -3
- package/lib/cjs/provider/bridge/models/bridge-connection.js.map +0 -1
- package/lib/cjs/provider/bridge/models/bridge-incomming-message.js +0 -3
- package/lib/cjs/provider/bridge/models/bridge-incomming-message.js.map +0 -1
- package/lib/cjs/provider/bridge/models/bridge-session-raw.js +0 -3
- package/lib/cjs/provider/bridge/models/bridge-session-raw.js.map +0 -1
- package/lib/cjs/provider/bridge/models/bridge-session.js +0 -3
- package/lib/cjs/provider/bridge/models/bridge-session.js.map +0 -1
- package/lib/cjs/provider/injected/injected-provider.js +0 -143
- package/lib/cjs/provider/injected/injected-provider.js.map +0 -1
- package/lib/cjs/provider/injected/models/injected-wallet-api.js +0 -3
- package/lib/cjs/provider/injected/models/injected-wallet-api.js.map +0 -1
- package/lib/cjs/provider/provider.js +0 -3
- package/lib/cjs/provider/provider.js.map +0 -1
- package/lib/cjs/resources/protocol.js +0 -5
- package/lib/cjs/resources/protocol.js.map +0 -1
- package/lib/cjs/storage/bridge-connection-storage.js +0 -101
- package/lib/cjs/storage/bridge-connection-storage.js.map +0 -1
- package/lib/cjs/storage/default-storage.js +0 -40
- package/lib/cjs/storage/default-storage.js.map +0 -1
- package/lib/cjs/storage/http-bridge-gateway-storage.js +0 -39
- package/lib/cjs/storage/http-bridge-gateway-storage.js.map +0 -1
- package/lib/cjs/storage/models/storage.interface.js +0 -3
- package/lib/cjs/storage/models/storage.interface.js.map +0 -1
- package/lib/cjs/ton-connect.interface.js +0 -3
- package/lib/cjs/ton-connect.interface.js.map +0 -1
- package/lib/cjs/ton-connect.js +0 -275
- package/lib/cjs/ton-connect.js.map +0 -1
- package/lib/cjs/utils/address.js +0 -98
- package/lib/cjs/utils/address.js.map +0 -1
- package/lib/cjs/utils/options.js +0 -16
- package/lib/cjs/utils/options.js.map +0 -1
- package/lib/cjs/utils/types.js +0 -3
- package/lib/cjs/utils/types.js.map +0 -1
- package/lib/cjs/utils/url.js +0 -15
- package/lib/cjs/utils/url.js.map +0 -1
- package/lib/cjs/utils/web-api.js +0 -27
- package/lib/cjs/utils/web-api.js.map +0 -1
- package/lib/cjs/wallets-list-manager.js +0 -123
- package/lib/cjs/wallets-list-manager.js.map +0 -1
- package/lib/esm/errors/address/index.js +0 -2
- package/lib/esm/errors/address/index.js.map +0 -1
- package/lib/esm/errors/address/wrong-address.error.js +0 -11
- package/lib/esm/errors/address/wrong-address.error.js.map +0 -1
- package/lib/esm/errors/binary/index.js +0 -2
- package/lib/esm/errors/binary/index.js.map +0 -1
- package/lib/esm/errors/binary/parse-hex.error.js +0 -11
- package/lib/esm/errors/binary/parse-hex.error.js.map +0 -1
- package/lib/esm/errors/dapp/dapp-metadata.error.js +0 -11
- package/lib/esm/errors/dapp/dapp-metadata.error.js.map +0 -1
- package/lib/esm/errors/index.js +0 -9
- package/lib/esm/errors/index.js.map +0 -1
- package/lib/esm/errors/protocol/events/connect/index.js +0 -2
- package/lib/esm/errors/protocol/events/connect/index.js.map +0 -1
- package/lib/esm/errors/protocol/events/connect/manifest-content-error.error.js +0 -12
- package/lib/esm/errors/protocol/events/connect/manifest-content-error.error.js.map +0 -1
- package/lib/esm/errors/protocol/events/connect/manifest-not-found.error.js +0 -12
- package/lib/esm/errors/protocol/events/connect/manifest-not-found.error.js.map +0 -1
- package/lib/esm/errors/protocol/events/connect/user-rejects.error.js +0 -11
- package/lib/esm/errors/protocol/events/connect/user-rejects.error.js.map +0 -1
- package/lib/esm/errors/protocol/events/index.js +0 -2
- package/lib/esm/errors/protocol/events/index.js.map +0 -1
- package/lib/esm/errors/protocol/index.js +0 -3
- package/lib/esm/errors/protocol/index.js.map +0 -1
- package/lib/esm/errors/protocol/responses/bad-request.error.js +0 -11
- package/lib/esm/errors/protocol/responses/bad-request.error.js.map +0 -1
- package/lib/esm/errors/protocol/responses/index.js +0 -3
- package/lib/esm/errors/protocol/responses/index.js.map +0 -1
- package/lib/esm/errors/protocol/responses/unknown-app.error.js +0 -11
- package/lib/esm/errors/protocol/responses/unknown-app.error.js.map +0 -1
- package/lib/esm/errors/storage/index.js +0 -2
- package/lib/esm/errors/storage/index.js.map +0 -1
- package/lib/esm/errors/storage/localstorage-not-found.error.js +0 -11
- package/lib/esm/errors/storage/localstorage-not-found.error.js.map +0 -1
- package/lib/esm/errors/ton-connect.error.js +0 -14
- package/lib/esm/errors/ton-connect.error.js.map +0 -1
- package/lib/esm/errors/unknown.error.js +0 -11
- package/lib/esm/errors/unknown.error.js.map +0 -1
- package/lib/esm/errors/wallet/index.js +0 -4
- package/lib/esm/errors/wallet/index.js.map +0 -1
- package/lib/esm/errors/wallet/wallet-already-connected.error.js +0 -11
- package/lib/esm/errors/wallet/wallet-already-connected.error.js.map +0 -1
- package/lib/esm/errors/wallet/wallet-not-connected.error.js +0 -11
- package/lib/esm/errors/wallet/wallet-not-connected.error.js.map +0 -1
- package/lib/esm/errors/wallet/wallet-not-injected.error.js +0 -11
- package/lib/esm/errors/wallet/wallet-not-injected.error.js.map +0 -1
- package/lib/esm/errors/wallet/wallet-not-support-feature.error.js +0 -11
- package/lib/esm/errors/wallet/wallet-not-support-feature.error.js.map +0 -1
- package/lib/esm/errors/wallets-manager/fetch-wallets.error.js +0 -11
- package/lib/esm/errors/wallets-manager/fetch-wallets.error.js.map +0 -1
- package/lib/esm/errors/wallets-manager/index.js +0 -2
- package/lib/esm/errors/wallets-manager/index.js.map +0 -1
- package/lib/esm/index.js +0 -7
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/models/dapp/dapp-metadata.js +0 -2
- package/lib/esm/models/dapp/dapp-metadata.js.map +0 -1
- package/lib/esm/models/index.js +0 -3
- package/lib/esm/models/index.js.map +0 -1
- package/lib/esm/models/methods/connect/connect-additional-request.js +0 -2
- package/lib/esm/models/methods/connect/connect-additional-request.js.map +0 -1
- package/lib/esm/models/methods/connect/index.js +0 -2
- package/lib/esm/models/methods/connect/index.js.map +0 -1
- package/lib/esm/models/methods/index.js +0 -3
- package/lib/esm/models/methods/index.js.map +0 -1
- package/lib/esm/models/methods/send-transaction/index.js +0 -2
- package/lib/esm/models/methods/send-transaction/index.js.map +0 -1
- package/lib/esm/models/methods/send-transaction/send-transaction-request.js +0 -2
- package/lib/esm/models/methods/send-transaction/send-transaction-request.js.map +0 -1
- package/lib/esm/models/methods/send-transaction/send-transaction-response.js +0 -2
- package/lib/esm/models/methods/send-transaction/send-transaction-response.js.map +0 -1
- package/lib/esm/models/ton-connect-options.js +0 -2
- package/lib/esm/models/ton-connect-options.js.map +0 -1
- package/lib/esm/models/wallet/account.js +0 -2
- package/lib/esm/models/wallet/account.js.map +0 -1
- package/lib/esm/models/wallet/index.js +0 -2
- package/lib/esm/models/wallet/index.js.map +0 -1
- package/lib/esm/models/wallet/wallet-connection-source.js +0 -4
- package/lib/esm/models/wallet/wallet-connection-source.js.map +0 -1
- package/lib/esm/models/wallet/wallet-info.js +0 -4
- package/lib/esm/models/wallet/wallet-info.js.map +0 -1
- package/lib/esm/models/wallet/wallet.js +0 -2
- package/lib/esm/models/wallet/wallet.js.map +0 -1
- package/lib/esm/parsers/connect-errors-parser.js +0 -24
- package/lib/esm/parsers/connect-errors-parser.js.map +0 -1
- package/lib/esm/parsers/rpc-parser.js +0 -6
- package/lib/esm/parsers/rpc-parser.js.map +0 -1
- package/lib/esm/parsers/send-transaction-parser.js +0 -32
- package/lib/esm/parsers/send-transaction-parser.js.map +0 -1
- package/lib/esm/provider/bridge/bridge-gateway.js +0 -106
- package/lib/esm/provider/bridge/bridge-gateway.js.map +0 -1
- package/lib/esm/provider/bridge/bridge-provider.js +0 -143
- package/lib/esm/provider/bridge/bridge-provider.js.map +0 -1
- package/lib/esm/provider/bridge/models/bridge-connection.js +0 -2
- package/lib/esm/provider/bridge/models/bridge-connection.js.map +0 -1
- package/lib/esm/provider/bridge/models/bridge-incomming-message.js +0 -2
- package/lib/esm/provider/bridge/models/bridge-incomming-message.js.map +0 -1
- package/lib/esm/provider/bridge/models/bridge-session-raw.js +0 -2
- package/lib/esm/provider/bridge/models/bridge-session-raw.js.map +0 -1
- package/lib/esm/provider/bridge/models/bridge-session.js +0 -2
- package/lib/esm/provider/bridge/models/bridge-session.js.map +0 -1
- package/lib/esm/provider/injected/injected-provider.js +0 -139
- package/lib/esm/provider/injected/injected-provider.js.map +0 -1
- package/lib/esm/provider/injected/models/injected-wallet-api.js +0 -2
- package/lib/esm/provider/injected/models/injected-wallet-api.js.map +0 -1
- package/lib/esm/provider/provider.js +0 -2
- package/lib/esm/provider/provider.js.map +0 -1
- package/lib/esm/resources/protocol.js +0 -2
- package/lib/esm/resources/protocol.js.map +0 -1
- package/lib/esm/storage/bridge-connection-storage.js +0 -97
- package/lib/esm/storage/bridge-connection-storage.js.map +0 -1
- package/lib/esm/storage/default-storage.js +0 -36
- package/lib/esm/storage/default-storage.js.map +0 -1
- package/lib/esm/storage/http-bridge-gateway-storage.js +0 -35
- package/lib/esm/storage/http-bridge-gateway-storage.js.map +0 -1
- package/lib/esm/storage/models/storage.interface.js +0 -2
- package/lib/esm/storage/models/storage.interface.js.map +0 -1
- package/lib/esm/ton-connect.interface.js +0 -2
- package/lib/esm/ton-connect.interface.js.map +0 -1
- package/lib/esm/ton-connect.js +0 -271
- package/lib/esm/ton-connect.js.map +0 -1
- package/lib/esm/utils/address.js +0 -94
- package/lib/esm/utils/address.js.map +0 -1
- package/lib/esm/utils/options.js +0 -9
- package/lib/esm/utils/options.js.map +0 -1
- package/lib/esm/utils/types.js +0 -2
- package/lib/esm/utils/types.js.map +0 -1
- package/lib/esm/utils/url.js +0 -10
- package/lib/esm/utils/url.js.map +0 -1
- package/lib/esm/utils/web-api.js +0 -21
- package/lib/esm/utils/web-api.js.map +0 -1
- package/lib/esm/wallets-list-manager.js +0 -119
- package/lib/esm/wallets-list-manager.js.map +0 -1
- package/lib/types/errors/address/index.d.ts +0 -1
- package/lib/types/errors/address/wrong-address.error.d.ts +0 -7
- package/lib/types/errors/binary/index.d.ts +0 -1
- package/lib/types/errors/binary/parse-hex.error.d.ts +0 -7
- package/lib/types/errors/dapp/dapp-metadata.error.d.ts +0 -7
- package/lib/types/errors/index.d.ts +0 -8
- package/lib/types/errors/protocol/events/connect/index.d.ts +0 -1
- package/lib/types/errors/protocol/events/connect/manifest-content-error.error.d.ts +0 -8
- package/lib/types/errors/protocol/events/connect/manifest-not-found.error.d.ts +0 -8
- package/lib/types/errors/protocol/events/connect/user-rejects.error.d.ts +0 -7
- package/lib/types/errors/protocol/events/index.d.ts +0 -1
- package/lib/types/errors/protocol/index.d.ts +0 -2
- package/lib/types/errors/protocol/responses/bad-request.error.d.ts +0 -7
- package/lib/types/errors/protocol/responses/index.d.ts +0 -2
- package/lib/types/errors/protocol/responses/unknown-app.error.d.ts +0 -7
- package/lib/types/errors/storage/index.d.ts +0 -1
- package/lib/types/errors/storage/localstorage-not-found.error.d.ts +0 -7
- package/lib/types/errors/ton-connect.error.d.ts +0 -7
- package/lib/types/errors/unknown.error.d.ts +0 -7
- package/lib/types/errors/wallet/index.d.ts +0 -3
- package/lib/types/errors/wallet/wallet-already-connected.error.d.ts +0 -7
- package/lib/types/errors/wallet/wallet-not-connected.error.d.ts +0 -7
- package/lib/types/errors/wallet/wallet-not-injected.error.d.ts +0 -7
- package/lib/types/errors/wallet/wallet-not-support-feature.error.d.ts +0 -7
- package/lib/types/errors/wallets-manager/fetch-wallets.error.d.ts +0 -7
- package/lib/types/errors/wallets-manager/index.d.ts +0 -1
- package/lib/types/models/dapp/dapp-metadata.d.ts +0 -18
- package/lib/types/models/index.d.ts +0 -4
- package/lib/types/models/methods/connect/connect-additional-request.d.ts +0 -6
- package/lib/types/models/methods/connect/index.d.ts +0 -1
- package/lib/types/models/methods/index.d.ts +0 -2
- package/lib/types/models/methods/send-transaction/index.d.ts +0 -2
- package/lib/types/models/methods/send-transaction/send-transaction-request.d.ts +0 -27
- package/lib/types/models/methods/send-transaction/send-transaction-response.d.ts +0 -6
- package/lib/types/models/ton-connect-options.d.ts +0 -21
- package/lib/types/models/wallet/account.d.ts +0 -16
- package/lib/types/models/wallet/index.d.ts +0 -4
- package/lib/types/models/wallet/wallet-connection-source.d.ts +0 -18
- package/lib/types/models/wallet/wallet-info.d.ts +0 -65
- package/lib/types/models/wallet/wallet.d.ts +0 -22
- package/lib/types/parsers/connect-errors-parser.d.ts +0 -7
- package/lib/types/parsers/rpc-parser.d.ts +0 -8
- package/lib/types/parsers/send-transaction-parser.d.ts +0 -13
- package/lib/types/provider/bridge/bridge-gateway.d.ts +0 -21
- package/lib/types/provider/bridge/bridge-provider.d.ts +0 -29
- package/lib/types/provider/bridge/models/bridge-connection.d.ts +0 -23
- package/lib/types/provider/bridge/models/bridge-incomming-message.d.ts +0 -4
- package/lib/types/provider/bridge/models/bridge-session-raw.d.ts +0 -5
- package/lib/types/provider/bridge/models/bridge-session.d.ts +0 -8
- package/lib/types/provider/injected/injected-provider.d.ts +0 -29
- package/lib/types/provider/injected/models/injected-wallet-api.d.ts +0 -11
- package/lib/types/provider/provider.d.ts +0 -19
- package/lib/types/resources/protocol.d.ts +0 -1
- package/lib/types/storage/bridge-connection-storage.d.ts +0 -13
- package/lib/types/storage/default-storage.d.ts +0 -8
- package/lib/types/storage/http-bridge-gateway-storage.d.ts +0 -9
- package/lib/types/storage/models/storage.interface.d.ts +0 -21
- package/lib/types/ton-connect.d.ts +0 -86
- package/lib/types/ton-connect.interface.d.ts +0 -53
- package/lib/types/utils/address.d.ts +0 -6
- package/lib/types/utils/options.d.ts +0 -1
- package/lib/types/utils/types.d.ts +0 -6
- package/lib/types/utils/url.d.ts +0 -2
- package/lib/types/utils/web-api.d.ts +0 -3
- package/lib/types/wallets-list-manager.d.ts +0 -11
|
@@ -0,0 +1,1627 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var protocol = require('@tonconnect/protocol');
|
|
6
|
+
require('@tonconnect/isomorphic-eventsource');
|
|
7
|
+
require('@tonconnect/isomorphic-fetch');
|
|
8
|
+
|
|
9
|
+
/*! *****************************************************************************
|
|
10
|
+
Copyright (c) Microsoft Corporation.
|
|
11
|
+
|
|
12
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
13
|
+
purpose with or without fee is hereby granted.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
16
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
17
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
18
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
19
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
20
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
21
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
22
|
+
***************************************************************************** */
|
|
23
|
+
|
|
24
|
+
function __rest(s, e) {
|
|
25
|
+
var t = {};
|
|
26
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
27
|
+
t[p] = s[p];
|
|
28
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
29
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
30
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
31
|
+
t[p[i]] = s[p[i]];
|
|
32
|
+
}
|
|
33
|
+
return t;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
37
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
38
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
39
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
40
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
41
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
42
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Base class for TonConnect errors. You can check if the error was triggered by the @tonconnect/sdk using `err instanceof TonConnectError`.
|
|
48
|
+
*/
|
|
49
|
+
class TonConnectError extends Error {
|
|
50
|
+
constructor(message, options) {
|
|
51
|
+
super(message, options);
|
|
52
|
+
this.message = `${TonConnectError.prefix} ${this.constructor.name}${this.info ? ': ' + this.info : ''}${message ? '\n' + message : ''}`;
|
|
53
|
+
Object.setPrototypeOf(this, TonConnectError.prototype);
|
|
54
|
+
}
|
|
55
|
+
get info() {
|
|
56
|
+
return '';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
TonConnectError.prefix = '[TON_CONNECT_SDK_ERROR]';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Thrown when passed DappMetadata is in incorrect format.
|
|
63
|
+
*/
|
|
64
|
+
class DappMetadataError extends TonConnectError {
|
|
65
|
+
get info() {
|
|
66
|
+
return 'Passed DappMetadata is in incorrect format.';
|
|
67
|
+
}
|
|
68
|
+
constructor(...args) {
|
|
69
|
+
super(...args);
|
|
70
|
+
Object.setPrototypeOf(this, DappMetadataError.prototype);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Thrown when passed manifest contains errors.
|
|
76
|
+
*/
|
|
77
|
+
class ManifestContentErrorError extends TonConnectError {
|
|
78
|
+
get info() {
|
|
79
|
+
return 'Passed `tonconnect-manifest.json` contains errors. Check format of your manifest. See more https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest';
|
|
80
|
+
}
|
|
81
|
+
constructor(...args) {
|
|
82
|
+
super(...args);
|
|
83
|
+
Object.setPrototypeOf(this, ManifestContentErrorError.prototype);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Thrown when wallet can't get manifest by passed manifestUrl.
|
|
89
|
+
*/
|
|
90
|
+
class ManifestNotFoundError extends TonConnectError {
|
|
91
|
+
get info() {
|
|
92
|
+
return 'Manifest not found. Make sure you added `tonconnect-manifest.json` to the root of your app or passed correct manifestUrl. See more https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest';
|
|
93
|
+
}
|
|
94
|
+
constructor(...args) {
|
|
95
|
+
super(...args);
|
|
96
|
+
Object.setPrototypeOf(this, ManifestNotFoundError.prototype);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Thrown when wallet connection called but wallet already connected. To avoid the error, disconnect the wallet before doing a new connection.
|
|
102
|
+
*/
|
|
103
|
+
class WalletAlreadyConnectedError extends TonConnectError {
|
|
104
|
+
get info() {
|
|
105
|
+
return 'Wallet connection called but wallet already connected. To avoid the error, disconnect the wallet before doing a new connection.';
|
|
106
|
+
}
|
|
107
|
+
constructor(...args) {
|
|
108
|
+
super(...args);
|
|
109
|
+
Object.setPrototypeOf(this, WalletAlreadyConnectedError.prototype);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Thrown when send transaction or other protocol methods called while wallet is not connected.
|
|
115
|
+
*/
|
|
116
|
+
class WalletNotConnectedError extends TonConnectError {
|
|
117
|
+
get info() {
|
|
118
|
+
return 'Send transaction or other protocol methods called while wallet is not connected.';
|
|
119
|
+
}
|
|
120
|
+
constructor(...args) {
|
|
121
|
+
super(...args);
|
|
122
|
+
Object.setPrototypeOf(this, WalletNotConnectedError.prototype);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isWalletConnectionSourceJS(value) {
|
|
127
|
+
return 'jsBridgeKey' in value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Thrown when user rejects the action in the wallet.
|
|
132
|
+
*/
|
|
133
|
+
class UserRejectsError extends TonConnectError {
|
|
134
|
+
get info() {
|
|
135
|
+
return 'User rejects the action in the wallet.';
|
|
136
|
+
}
|
|
137
|
+
constructor(...args) {
|
|
138
|
+
super(...args);
|
|
139
|
+
Object.setPrototypeOf(this, UserRejectsError.prototype);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Thrown when request to the wallet contains errors.
|
|
145
|
+
*/
|
|
146
|
+
class BadRequestError extends TonConnectError {
|
|
147
|
+
get info() {
|
|
148
|
+
return 'Request to the wallet contains errors.';
|
|
149
|
+
}
|
|
150
|
+
constructor(...args) {
|
|
151
|
+
super(...args);
|
|
152
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Thrown when app tries to send rpc request to the injected wallet while not connected.
|
|
158
|
+
*/
|
|
159
|
+
class UnknownAppError extends TonConnectError {
|
|
160
|
+
get info() {
|
|
161
|
+
return 'App tries to send rpc request to the injected wallet while not connected.';
|
|
162
|
+
}
|
|
163
|
+
constructor(...args) {
|
|
164
|
+
super(...args);
|
|
165
|
+
Object.setPrototypeOf(this, UnknownAppError.prototype);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Thrown when there is an attempt to connect to the injected wallet while it is not exists in the webpage.
|
|
171
|
+
*/
|
|
172
|
+
class WalletNotInjectedError extends TonConnectError {
|
|
173
|
+
get info() {
|
|
174
|
+
return 'There is an attempt to connect to the injected wallet while it is not exists in the webpage.';
|
|
175
|
+
}
|
|
176
|
+
constructor(...args) {
|
|
177
|
+
super(...args);
|
|
178
|
+
Object.setPrototypeOf(this, WalletNotInjectedError.prototype);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Thrown when `Storage` was not specified in the `DappMetadata` and default `localStorage` was not detected in the environment.
|
|
184
|
+
*/
|
|
185
|
+
class LocalstorageNotFoundError extends TonConnectError {
|
|
186
|
+
get info() {
|
|
187
|
+
return 'Storage was not specified in the `DappMetadata` and default `localStorage` was not detected in the environment.';
|
|
188
|
+
}
|
|
189
|
+
constructor(...args) {
|
|
190
|
+
super(...args);
|
|
191
|
+
Object.setPrototypeOf(this, LocalstorageNotFoundError.prototype);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Thrown when an error occurred while fetching the wallets list.
|
|
197
|
+
*/
|
|
198
|
+
class FetchWalletsError extends TonConnectError {
|
|
199
|
+
get info() {
|
|
200
|
+
return 'An error occurred while fetching the wallets list.';
|
|
201
|
+
}
|
|
202
|
+
constructor(...args) {
|
|
203
|
+
super(...args);
|
|
204
|
+
Object.setPrototypeOf(this, FetchWalletsError.prototype);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Thrown when passed address is in incorrect format.
|
|
210
|
+
*/
|
|
211
|
+
class WrongAddressError extends TonConnectError {
|
|
212
|
+
get info() {
|
|
213
|
+
return 'Passed address is in incorrect format.';
|
|
214
|
+
}
|
|
215
|
+
constructor(...args) {
|
|
216
|
+
super(...args);
|
|
217
|
+
Object.setPrototypeOf(this, WrongAddressError.prototype);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Thrown when passed hex is in incorrect format.
|
|
223
|
+
*/
|
|
224
|
+
class ParseHexError extends TonConnectError {
|
|
225
|
+
get info() {
|
|
226
|
+
return 'Passed hex is in incorrect format.';
|
|
227
|
+
}
|
|
228
|
+
constructor(...args) {
|
|
229
|
+
super(...args);
|
|
230
|
+
Object.setPrototypeOf(this, ParseHexError.prototype);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Unhanded unknown error.
|
|
236
|
+
*/
|
|
237
|
+
class UnknownError extends TonConnectError {
|
|
238
|
+
constructor(...args) {
|
|
239
|
+
super(...args);
|
|
240
|
+
Object.setPrototypeOf(this, UnknownError.prototype);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const connectEventErrorsCodes = {
|
|
245
|
+
[protocol.CONNECT_EVENT_ERROR_CODES.UNKNOWN_ERROR]: UnknownError,
|
|
246
|
+
[protocol.CONNECT_EVENT_ERROR_CODES.USER_REJECTS_ERROR]: UserRejectsError,
|
|
247
|
+
[protocol.CONNECT_EVENT_ERROR_CODES.BAD_REQUEST_ERROR]: BadRequestError,
|
|
248
|
+
[protocol.CONNECT_EVENT_ERROR_CODES.UNKNOWN_APP_ERROR]: UnknownAppError,
|
|
249
|
+
[protocol.CONNECT_EVENT_ERROR_CODES.MANIFEST_NOT_FOUND_ERROR]: ManifestNotFoundError,
|
|
250
|
+
[protocol.CONNECT_EVENT_ERROR_CODES.MANIFEST_CONTENT_ERROR]: ManifestContentErrorError
|
|
251
|
+
};
|
|
252
|
+
class ConnectErrorsParser {
|
|
253
|
+
parseError(error) {
|
|
254
|
+
let ErrorConstructor = UnknownError;
|
|
255
|
+
if (error.code in connectEventErrorsCodes) {
|
|
256
|
+
ErrorConstructor = connectEventErrorsCodes[error.code] || UnknownError;
|
|
257
|
+
}
|
|
258
|
+
return new ErrorConstructor(error.message);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const connectErrorsParser = new ConnectErrorsParser();
|
|
262
|
+
|
|
263
|
+
class RpcParser {
|
|
264
|
+
isError(response) {
|
|
265
|
+
return 'error' in response;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const sendTransactionErrors = {
|
|
270
|
+
[protocol.SEND_TRANSACTION_ERROR_CODES.UNKNOWN_ERROR]: UnknownError,
|
|
271
|
+
[protocol.SEND_TRANSACTION_ERROR_CODES.USER_REJECTS_ERROR]: UserRejectsError,
|
|
272
|
+
[protocol.SEND_TRANSACTION_ERROR_CODES.BAD_REQUEST_ERROR]: BadRequestError,
|
|
273
|
+
[protocol.SEND_TRANSACTION_ERROR_CODES.UNKNOWN_APP_ERROR]: UnknownAppError
|
|
274
|
+
};
|
|
275
|
+
class SendTransactionParser extends RpcParser {
|
|
276
|
+
convertToRpcRequest(request) {
|
|
277
|
+
return {
|
|
278
|
+
method: 'sendTransaction',
|
|
279
|
+
params: [JSON.stringify(request)]
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
parseAndThrowError(response) {
|
|
283
|
+
let ErrorConstructor = UnknownError;
|
|
284
|
+
if (response.error.code in sendTransactionErrors) {
|
|
285
|
+
ErrorConstructor = sendTransactionErrors[response.error.code] || UnknownError;
|
|
286
|
+
}
|
|
287
|
+
throw new ErrorConstructor(response.error.message);
|
|
288
|
+
}
|
|
289
|
+
convertFromRpcResponse(rpcResponse) {
|
|
290
|
+
return {
|
|
291
|
+
boc: rpcResponse.result
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const sendTransactionParser = new SendTransactionParser();
|
|
296
|
+
|
|
297
|
+
class HttpBridgeGatewayStorage {
|
|
298
|
+
constructor(storage, bridgeUrl) {
|
|
299
|
+
this.storage = storage;
|
|
300
|
+
this.storeKey = 'ton-connect-storage_http-bridge-gateway::' + bridgeUrl;
|
|
301
|
+
}
|
|
302
|
+
storeLastEventId(lastEventId) {
|
|
303
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
return this.storage.setItem(this.storeKey, lastEventId);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
removeLastEventId() {
|
|
308
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
309
|
+
return this.storage.removeItem(this.storeKey);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
getLastEventId() {
|
|
313
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
314
|
+
const stored = yield this.storage.getItem(this.storeKey);
|
|
315
|
+
if (!stored) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
return stored;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function removeUrlLastSlash(url) {
|
|
324
|
+
if (url.slice(-1) === '/') {
|
|
325
|
+
return url.slice(0, -1);
|
|
326
|
+
}
|
|
327
|
+
return url;
|
|
328
|
+
}
|
|
329
|
+
function addPathToUrl(url, path) {
|
|
330
|
+
return removeUrlLastSlash(url) + '/' + path;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
class BridgeGateway {
|
|
334
|
+
constructor(storage, bridgeUrl, sessionId, listener, errorsListener) {
|
|
335
|
+
this.bridgeUrl = bridgeUrl;
|
|
336
|
+
this.sessionId = sessionId;
|
|
337
|
+
this.listener = listener;
|
|
338
|
+
this.errorsListener = errorsListener;
|
|
339
|
+
this.ssePath = 'events';
|
|
340
|
+
this.postPath = 'message';
|
|
341
|
+
this.heartbeatMessage = 'heartbeat';
|
|
342
|
+
this.defaultTtl = 300;
|
|
343
|
+
this.isClosed = false;
|
|
344
|
+
this.bridgeGatewayStorage = new HttpBridgeGatewayStorage(storage, bridgeUrl);
|
|
345
|
+
}
|
|
346
|
+
registerSession() {
|
|
347
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
348
|
+
const url = new URL(addPathToUrl(this.bridgeUrl, this.ssePath));
|
|
349
|
+
url.searchParams.append('client_id', this.sessionId);
|
|
350
|
+
const lastEventId = yield this.bridgeGatewayStorage.getLastEventId();
|
|
351
|
+
if (this.isClosed) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (lastEventId) {
|
|
355
|
+
url.searchParams.append('last_event_id', lastEventId);
|
|
356
|
+
}
|
|
357
|
+
this.eventSource = new EventSource(url.toString());
|
|
358
|
+
return new Promise((resolve, reject) => {
|
|
359
|
+
this.eventSource.onerror = reject;
|
|
360
|
+
this.eventSource.onopen = () => {
|
|
361
|
+
this.eventSource.onerror = this.errorsHandler.bind(this);
|
|
362
|
+
this.eventSource.onmessage = this.messagesHandler.bind(this);
|
|
363
|
+
resolve();
|
|
364
|
+
};
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
send(message, receiver, topic, ttl) {
|
|
369
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
370
|
+
const url = new URL(addPathToUrl(this.bridgeUrl, this.postPath));
|
|
371
|
+
url.searchParams.append('client_id', this.sessionId);
|
|
372
|
+
url.searchParams.append('to', receiver);
|
|
373
|
+
url.searchParams.append('ttl', (ttl || this.defaultTtl).toString());
|
|
374
|
+
url.searchParams.append('topic', topic);
|
|
375
|
+
yield fetch(url, {
|
|
376
|
+
method: 'post',
|
|
377
|
+
body: protocol.Base64.encode(message)
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
pause() {
|
|
382
|
+
var _a;
|
|
383
|
+
(_a = this.eventSource) === null || _a === void 0 ? void 0 : _a.close();
|
|
384
|
+
}
|
|
385
|
+
unPause() {
|
|
386
|
+
return this.registerSession();
|
|
387
|
+
}
|
|
388
|
+
close() {
|
|
389
|
+
var _a;
|
|
390
|
+
this.isClosed = true;
|
|
391
|
+
(_a = this.eventSource) === null || _a === void 0 ? void 0 : _a.close();
|
|
392
|
+
}
|
|
393
|
+
setListener(listener) {
|
|
394
|
+
this.listener = listener;
|
|
395
|
+
}
|
|
396
|
+
setErrorsListener(errorsListener) {
|
|
397
|
+
this.errorsListener = errorsListener;
|
|
398
|
+
}
|
|
399
|
+
errorsHandler(e) {
|
|
400
|
+
var _a, _b;
|
|
401
|
+
if (!this.isClosed) {
|
|
402
|
+
if (((_a = this.eventSource) === null || _a === void 0 ? void 0 : _a.readyState) === EventSource.CLOSED) {
|
|
403
|
+
this.eventSource.close();
|
|
404
|
+
this.registerSession();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (((_b = this.eventSource) === null || _b === void 0 ? void 0 : _b.readyState) === EventSource.CONNECTING) {
|
|
408
|
+
console.debug('[TON_CONNET_SDK_ERROR]: Bridge error', JSON.stringify(e));
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
this.errorsListener(e);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
messagesHandler(e) {
|
|
415
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
416
|
+
if (e.data === this.heartbeatMessage) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
yield this.bridgeGatewayStorage.storeLastEventId(e.lastEventId);
|
|
420
|
+
if (!this.isClosed) {
|
|
421
|
+
let bridgeIncomingMessage;
|
|
422
|
+
try {
|
|
423
|
+
bridgeIncomingMessage = JSON.parse(e.data);
|
|
424
|
+
}
|
|
425
|
+
catch (e) {
|
|
426
|
+
throw new TonConnectError(`Bridge message parse failed, message ${e.data}`);
|
|
427
|
+
}
|
|
428
|
+
this.listener(bridgeIncomingMessage);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
class BridgeConnectionStorage {
|
|
435
|
+
constructor(storage) {
|
|
436
|
+
this.storage = storage;
|
|
437
|
+
this.storeKey = 'ton-connect-storage_bridge-connection';
|
|
438
|
+
}
|
|
439
|
+
storeConnection(connection) {
|
|
440
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
441
|
+
if (connection.type === 'injected') {
|
|
442
|
+
return this.storage.setItem(this.storeKey, JSON.stringify(connection));
|
|
443
|
+
}
|
|
444
|
+
const rawSession = {
|
|
445
|
+
sessionKeyPair: connection.session.sessionCrypto.stringifyKeypair(),
|
|
446
|
+
walletPublicKey: connection.session.walletPublicKey,
|
|
447
|
+
bridgeUrl: connection.session.bridgeUrl
|
|
448
|
+
};
|
|
449
|
+
const rawConnection = {
|
|
450
|
+
type: 'http',
|
|
451
|
+
connectEvent: connection.connectEvent,
|
|
452
|
+
session: rawSession,
|
|
453
|
+
lastWalletEventId: connection.lastWalletEventId,
|
|
454
|
+
nextRpcRequestId: connection.nextRpcRequestId
|
|
455
|
+
};
|
|
456
|
+
return this.storage.setItem(this.storeKey, JSON.stringify(rawConnection));
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
removeConnection() {
|
|
460
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
461
|
+
return this.storage.removeItem(this.storeKey);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
getConnection() {
|
|
465
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
466
|
+
const stored = yield this.storage.getItem(this.storeKey);
|
|
467
|
+
if (!stored) {
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
const connection = JSON.parse(stored);
|
|
471
|
+
if (connection.type === 'injected') {
|
|
472
|
+
return connection;
|
|
473
|
+
}
|
|
474
|
+
const sessionCrypto = new protocol.SessionCrypto(connection.session.sessionKeyPair);
|
|
475
|
+
return {
|
|
476
|
+
type: 'http',
|
|
477
|
+
connectEvent: connection.connectEvent,
|
|
478
|
+
lastWalletEventId: connection.lastWalletEventId,
|
|
479
|
+
nextRpcRequestId: connection.nextRpcRequestId,
|
|
480
|
+
session: {
|
|
481
|
+
sessionCrypto,
|
|
482
|
+
bridgeUrl: connection.session.bridgeUrl,
|
|
483
|
+
walletPublicKey: connection.session.walletPublicKey
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
getHttpConnection() {
|
|
489
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
490
|
+
const connection = yield this.getConnection();
|
|
491
|
+
if (!connection) {
|
|
492
|
+
throw new TonConnectError('Trying to read HTTP connection source while nothing is stored');
|
|
493
|
+
}
|
|
494
|
+
if (connection.type === 'injected') {
|
|
495
|
+
throw new TonConnectError('Trying to read HTTP connection source while injected connection is stored');
|
|
496
|
+
}
|
|
497
|
+
return connection;
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
getInjectedConnection() {
|
|
501
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
502
|
+
const connection = yield this.getConnection();
|
|
503
|
+
if (!connection) {
|
|
504
|
+
throw new TonConnectError('Trying to read Injected bridge connection source while nothing is stored');
|
|
505
|
+
}
|
|
506
|
+
if ((connection === null || connection === void 0 ? void 0 : connection.type) === 'http') {
|
|
507
|
+
throw new TonConnectError('Trying to read Injected bridge connection source while HTTP connection is stored');
|
|
508
|
+
}
|
|
509
|
+
return connection;
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
storedConnectionType() {
|
|
513
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
514
|
+
const stored = yield this.storage.getItem(this.storeKey);
|
|
515
|
+
if (!stored) {
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
const connection = JSON.parse(stored);
|
|
519
|
+
return connection.type;
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
storeLastWalletEventId(id) {
|
|
523
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
524
|
+
const connection = yield this.getConnection();
|
|
525
|
+
if (connection && connection.type === 'http') {
|
|
526
|
+
connection.lastWalletEventId = id;
|
|
527
|
+
return this.storeConnection(connection);
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
getLastWalletEventId() {
|
|
532
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
533
|
+
const connection = yield this.getConnection();
|
|
534
|
+
if (connection && 'lastWalletEventId' in connection) {
|
|
535
|
+
return connection.lastWalletEventId;
|
|
536
|
+
}
|
|
537
|
+
return undefined;
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
increaseNextRpcRequestId() {
|
|
541
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
542
|
+
const connection = yield this.getConnection();
|
|
543
|
+
if (connection) {
|
|
544
|
+
const lastId = connection.nextRpcRequestId || 0;
|
|
545
|
+
connection.nextRpcRequestId = lastId + 1;
|
|
546
|
+
return this.storeConnection(connection);
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
getNextRpcRequestId() {
|
|
551
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
552
|
+
const connection = yield this.getConnection();
|
|
553
|
+
if (connection) {
|
|
554
|
+
return connection.nextRpcRequestId || 0;
|
|
555
|
+
}
|
|
556
|
+
return 0;
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
const PROTOCOL_VERSION = 2;
|
|
562
|
+
|
|
563
|
+
function logDebug(...args) {
|
|
564
|
+
{
|
|
565
|
+
try {
|
|
566
|
+
console.debug('[TON_CONNECT_SDK]', ...args);
|
|
567
|
+
}
|
|
568
|
+
catch (_a) { }
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function logError(...args) {
|
|
572
|
+
{
|
|
573
|
+
try {
|
|
574
|
+
console.error('[TON_CONNECT_SDK]', ...args);
|
|
575
|
+
}
|
|
576
|
+
catch (_a) { }
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
function logWarning(...args) {
|
|
580
|
+
{
|
|
581
|
+
try {
|
|
582
|
+
console.warn('[TON_CONNECT_SDK]', ...args);
|
|
583
|
+
}
|
|
584
|
+
catch (_a) { }
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
class BridgeProvider {
|
|
589
|
+
constructor(storage, walletConnectionSource) {
|
|
590
|
+
this.storage = storage;
|
|
591
|
+
this.walletConnectionSource = walletConnectionSource;
|
|
592
|
+
this.type = 'http';
|
|
593
|
+
this.standardUniversalLink = 'tc://';
|
|
594
|
+
this.pendingRequests = new Map();
|
|
595
|
+
this.session = null;
|
|
596
|
+
this.gateway = null;
|
|
597
|
+
this.pendingGateways = [];
|
|
598
|
+
this.listeners = [];
|
|
599
|
+
this.connectionStorage = new BridgeConnectionStorage(storage);
|
|
600
|
+
}
|
|
601
|
+
static fromStorage(storage) {
|
|
602
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
603
|
+
const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
|
|
604
|
+
const connection = yield bridgeConnectionStorage.getHttpConnection();
|
|
605
|
+
return new BridgeProvider(storage, { bridgeUrl: connection.session.bridgeUrl });
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
connect(message) {
|
|
609
|
+
this.closeGateways();
|
|
610
|
+
const sessionCrypto = new protocol.SessionCrypto();
|
|
611
|
+
let bridgeUrl = '';
|
|
612
|
+
let universalLink = this.standardUniversalLink;
|
|
613
|
+
if (Array.isArray(this.walletConnectionSource)) {
|
|
614
|
+
this.pendingGateways = this.walletConnectionSource.map(source => {
|
|
615
|
+
const gateway = new BridgeGateway(this.storage, source.bridgeUrl, sessionCrypto.sessionId, () => { }, e => {
|
|
616
|
+
console.error(e);
|
|
617
|
+
});
|
|
618
|
+
gateway.setListener(message => this.pendingGatewaysListener(gateway, source.bridgeUrl, message));
|
|
619
|
+
return gateway;
|
|
620
|
+
});
|
|
621
|
+
this.pendingGateways.forEach(bridge => bridge.registerSession());
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
bridgeUrl = this.walletConnectionSource.bridgeUrl;
|
|
625
|
+
if (this.walletConnectionSource.universalLink) {
|
|
626
|
+
universalLink = this.walletConnectionSource.universalLink;
|
|
627
|
+
}
|
|
628
|
+
this.gateway = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
|
|
629
|
+
this.gateway.registerSession();
|
|
630
|
+
}
|
|
631
|
+
this.session = {
|
|
632
|
+
sessionCrypto,
|
|
633
|
+
bridgeUrl
|
|
634
|
+
};
|
|
635
|
+
return this.generateUniversalLink(universalLink, message);
|
|
636
|
+
}
|
|
637
|
+
restoreConnection() {
|
|
638
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
639
|
+
if (Array.isArray(this.walletConnectionSource)) {
|
|
640
|
+
throw new TonConnectError('Internal error. Connection source is array while WalletConnectionSourceHTTP was expected.');
|
|
641
|
+
}
|
|
642
|
+
this.closeGateways();
|
|
643
|
+
const storedConnection = yield this.connectionStorage.getHttpConnection();
|
|
644
|
+
if (!storedConnection) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
this.session = storedConnection.session;
|
|
648
|
+
this.gateway = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, storedConnection.session.sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
|
|
649
|
+
yield this.gateway.registerSession();
|
|
650
|
+
this.listeners.forEach(listener => listener(storedConnection.connectEvent));
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
sendRequest(request, onRequestSent) {
|
|
654
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
655
|
+
if (!this.gateway || !this.session || !('walletPublicKey' in this.session)) {
|
|
656
|
+
throw new TonConnectError('Trying to send bridge request without session');
|
|
657
|
+
}
|
|
658
|
+
const id = (yield this.connectionStorage.getNextRpcRequestId()).toString();
|
|
659
|
+
yield this.connectionStorage.increaseNextRpcRequestId();
|
|
660
|
+
logDebug('Send http-bridge request:', Object.assign(Object.assign({}, request), { id }));
|
|
661
|
+
const encodedRequest = this.session.sessionCrypto.encrypt(JSON.stringify(Object.assign(Object.assign({}, request), { id })), protocol.hexToByteArray(this.session.walletPublicKey));
|
|
662
|
+
this.gateway
|
|
663
|
+
.send(encodedRequest, this.session.walletPublicKey, request.method)
|
|
664
|
+
.catch(reject);
|
|
665
|
+
this.pendingRequests.set(id.toString(), resolve);
|
|
666
|
+
onRequestSent === null || onRequestSent === void 0 ? void 0 : onRequestSent();
|
|
667
|
+
}));
|
|
668
|
+
}
|
|
669
|
+
closeConnection() {
|
|
670
|
+
this.closeGateways();
|
|
671
|
+
this.listeners = [];
|
|
672
|
+
this.session = null;
|
|
673
|
+
this.gateway = null;
|
|
674
|
+
}
|
|
675
|
+
disconnect() {
|
|
676
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
677
|
+
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
678
|
+
let called = false;
|
|
679
|
+
const onRequestSent = () => {
|
|
680
|
+
called = true;
|
|
681
|
+
this.removeBridgeAndSession().then(resolve);
|
|
682
|
+
};
|
|
683
|
+
try {
|
|
684
|
+
yield this.sendRequest({ method: 'disconnect', params: [] }, onRequestSent);
|
|
685
|
+
}
|
|
686
|
+
catch (e) {
|
|
687
|
+
console.debug(e);
|
|
688
|
+
if (!called) {
|
|
689
|
+
this.removeBridgeAndSession().then(resolve);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}));
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
listen(callback) {
|
|
696
|
+
this.listeners.push(callback);
|
|
697
|
+
return () => (this.listeners = this.listeners.filter(listener => listener !== callback));
|
|
698
|
+
}
|
|
699
|
+
pause() {
|
|
700
|
+
var _a;
|
|
701
|
+
(_a = this.gateway) === null || _a === void 0 ? void 0 : _a.pause();
|
|
702
|
+
this.pendingGateways.forEach(bridge => bridge.pause());
|
|
703
|
+
}
|
|
704
|
+
unPause() {
|
|
705
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
706
|
+
const promises = this.pendingGateways.map(bridge => bridge.unPause());
|
|
707
|
+
if (this.gateway) {
|
|
708
|
+
promises.push(this.gateway.unPause());
|
|
709
|
+
}
|
|
710
|
+
yield Promise.all(promises);
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
pendingGatewaysListener(gateway, bridgeUrl, bridgeIncomingMessage) {
|
|
714
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
715
|
+
if (!this.pendingGateways.includes(gateway)) {
|
|
716
|
+
gateway.close();
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
this.closeGateways({ except: gateway });
|
|
720
|
+
this.session.bridgeUrl = bridgeUrl;
|
|
721
|
+
this.gateway = gateway;
|
|
722
|
+
this.gateway.setErrorsListener(this.gatewayErrorsListener.bind(this));
|
|
723
|
+
this.gateway.setListener(this.gatewayListener.bind(this));
|
|
724
|
+
return this.gatewayListener(bridgeIncomingMessage);
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
gatewayListener(bridgeIncomingMessage) {
|
|
728
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
729
|
+
const walletMessage = JSON.parse(this.session.sessionCrypto.decrypt(protocol.Base64.decode(bridgeIncomingMessage.message).toUint8Array(), protocol.hexToByteArray(bridgeIncomingMessage.from)));
|
|
730
|
+
logDebug('Wallet message received:', walletMessage);
|
|
731
|
+
if (!('event' in walletMessage)) {
|
|
732
|
+
const id = walletMessage.id.toString();
|
|
733
|
+
const resolve = this.pendingRequests.get(id);
|
|
734
|
+
if (!resolve) {
|
|
735
|
+
logDebug(`Response id ${id} doesn't match any request's id`);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
resolve(walletMessage);
|
|
739
|
+
this.pendingRequests.delete(id);
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
if (walletMessage.id !== undefined) {
|
|
743
|
+
const lastId = yield this.connectionStorage.getLastWalletEventId();
|
|
744
|
+
if (lastId !== undefined && walletMessage.id <= lastId) {
|
|
745
|
+
logError(`Received event id (=${walletMessage.id}) must be greater than stored last wallet event id (=${lastId}) `);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
if (walletMessage.event !== 'connect') {
|
|
749
|
+
yield this.connectionStorage.storeLastWalletEventId(walletMessage.id);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
// `this.listeners` might be modified in the event handler
|
|
753
|
+
const listeners = this.listeners;
|
|
754
|
+
if (walletMessage.event === 'connect') {
|
|
755
|
+
yield this.updateSession(walletMessage, bridgeIncomingMessage.from);
|
|
756
|
+
}
|
|
757
|
+
if (walletMessage.event === 'disconnect') {
|
|
758
|
+
yield this.removeBridgeAndSession();
|
|
759
|
+
}
|
|
760
|
+
listeners.forEach(listener => listener(walletMessage));
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
gatewayErrorsListener(e) {
|
|
764
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
765
|
+
throw new TonConnectError(`Bridge error ${JSON.stringify(e)}`);
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
updateSession(connectEvent, walletPublicKey) {
|
|
769
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
770
|
+
this.session = Object.assign(Object.assign({}, this.session), { walletPublicKey });
|
|
771
|
+
const tonAddrItem = connectEvent.payload.items.find(item => item.name === 'ton_addr');
|
|
772
|
+
const connectEventToSave = Object.assign(Object.assign({}, connectEvent), { payload: Object.assign(Object.assign({}, connectEvent.payload), { items: [tonAddrItem] }) });
|
|
773
|
+
yield this.connectionStorage.storeConnection({
|
|
774
|
+
type: 'http',
|
|
775
|
+
session: this.session,
|
|
776
|
+
lastWalletEventId: connectEvent.id,
|
|
777
|
+
connectEvent: connectEventToSave,
|
|
778
|
+
nextRpcRequestId: 0
|
|
779
|
+
});
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
removeBridgeAndSession() {
|
|
783
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
784
|
+
this.closeConnection();
|
|
785
|
+
yield this.connectionStorage.removeConnection();
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
generateUniversalLink(universalLink, message) {
|
|
789
|
+
const url = new URL(universalLink);
|
|
790
|
+
url.searchParams.append('v', PROTOCOL_VERSION.toString());
|
|
791
|
+
url.searchParams.append('id', this.session.sessionCrypto.sessionId);
|
|
792
|
+
url.searchParams.append('r', JSON.stringify(message));
|
|
793
|
+
return url.toString();
|
|
794
|
+
}
|
|
795
|
+
closeGateways(options) {
|
|
796
|
+
var _a;
|
|
797
|
+
(_a = this.gateway) === null || _a === void 0 ? void 0 : _a.close();
|
|
798
|
+
this.pendingGateways
|
|
799
|
+
.filter(item => item !== (options === null || options === void 0 ? void 0 : options.except))
|
|
800
|
+
.forEach(bridge => bridge.close());
|
|
801
|
+
this.pendingGateways = [];
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function hasProperty(value, propertyKey) {
|
|
806
|
+
return hasProperties(value, [propertyKey]);
|
|
807
|
+
}
|
|
808
|
+
function hasProperties(value, propertyKeys) {
|
|
809
|
+
if (!value || typeof value !== 'object') {
|
|
810
|
+
return false;
|
|
811
|
+
}
|
|
812
|
+
return propertyKeys.every(propertyKey => propertyKey in value);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function isJSBridgeWithMetadata(value) {
|
|
816
|
+
try {
|
|
817
|
+
if (!hasProperty(value, 'tonconnect') || !hasProperty(value.tonconnect, 'walletInfo')) {
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
return hasProperties(value.tonconnect.walletInfo, ['name', 'image', 'about_url']);
|
|
821
|
+
}
|
|
822
|
+
catch (_a) {
|
|
823
|
+
return false;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
function getWindow() {
|
|
828
|
+
if (typeof window === 'undefined') {
|
|
829
|
+
return undefined;
|
|
830
|
+
}
|
|
831
|
+
return window;
|
|
832
|
+
}
|
|
833
|
+
function getDocument() {
|
|
834
|
+
if (typeof document === 'undefined') {
|
|
835
|
+
return undefined;
|
|
836
|
+
}
|
|
837
|
+
return document;
|
|
838
|
+
}
|
|
839
|
+
function getWebPageManifest() {
|
|
840
|
+
var _a;
|
|
841
|
+
const origin = (_a = getWindow()) === null || _a === void 0 ? void 0 : _a.location.origin;
|
|
842
|
+
if (origin) {
|
|
843
|
+
return origin + '/tonconnect-manifest.json';
|
|
844
|
+
}
|
|
845
|
+
return '';
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
class InjectedProvider {
|
|
849
|
+
constructor(storage, injectedWalletKey) {
|
|
850
|
+
this.injectedWalletKey = injectedWalletKey;
|
|
851
|
+
this.type = 'injected';
|
|
852
|
+
this.unsubscribeCallback = null;
|
|
853
|
+
this.listenSubscriptions = false;
|
|
854
|
+
this.listeners = [];
|
|
855
|
+
const window = InjectedProvider.window;
|
|
856
|
+
if (!InjectedProvider.isWindowContainsWallet(window, injectedWalletKey)) {
|
|
857
|
+
throw new WalletNotInjectedError();
|
|
858
|
+
}
|
|
859
|
+
this.connectionStorage = new BridgeConnectionStorage(storage);
|
|
860
|
+
this.injectedWallet = window[injectedWalletKey].tonconnect;
|
|
861
|
+
}
|
|
862
|
+
static fromStorage(storage) {
|
|
863
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
864
|
+
const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
|
|
865
|
+
const connection = yield bridgeConnectionStorage.getInjectedConnection();
|
|
866
|
+
return new InjectedProvider(storage, connection.jsBridgeKey);
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
static isWalletInjected(injectedWalletKey) {
|
|
870
|
+
return InjectedProvider.isWindowContainsWallet(this.window, injectedWalletKey);
|
|
871
|
+
}
|
|
872
|
+
static isInsideWalletBrowser(injectedWalletKey) {
|
|
873
|
+
if (InjectedProvider.isWindowContainsWallet(this.window, injectedWalletKey)) {
|
|
874
|
+
return this.window[injectedWalletKey].tonconnect.isWalletBrowser;
|
|
875
|
+
}
|
|
876
|
+
return false;
|
|
877
|
+
}
|
|
878
|
+
static getCurrentlyInjectedWallets() {
|
|
879
|
+
if (!this.window) {
|
|
880
|
+
return [];
|
|
881
|
+
}
|
|
882
|
+
const wallets = Object.entries(this.window).filter(([_, value]) => isJSBridgeWithMetadata(value));
|
|
883
|
+
return wallets.map(([jsBridgeKey, wallet]) => ({
|
|
884
|
+
name: wallet.tonconnect.walletInfo.name,
|
|
885
|
+
aboutUrl: wallet.tonconnect.walletInfo.about_url,
|
|
886
|
+
imageUrl: wallet.tonconnect.walletInfo.image,
|
|
887
|
+
tondns: wallet.tonconnect.walletInfo.tondns,
|
|
888
|
+
jsBridgeKey,
|
|
889
|
+
injected: true,
|
|
890
|
+
embedded: wallet.tonconnect.isWalletBrowser
|
|
891
|
+
}));
|
|
892
|
+
}
|
|
893
|
+
static isWindowContainsWallet(window, injectedWalletKey) {
|
|
894
|
+
return (!!window &&
|
|
895
|
+
injectedWalletKey in window &&
|
|
896
|
+
typeof window[injectedWalletKey] === 'object' &&
|
|
897
|
+
'tonconnect' in window[injectedWalletKey]);
|
|
898
|
+
}
|
|
899
|
+
connect(message) {
|
|
900
|
+
this._connect(PROTOCOL_VERSION, message);
|
|
901
|
+
}
|
|
902
|
+
restoreConnection() {
|
|
903
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
904
|
+
try {
|
|
905
|
+
logDebug(`Injected Provider restoring connection...`);
|
|
906
|
+
const connectEvent = yield this.injectedWallet.restoreConnection();
|
|
907
|
+
logDebug('Injected Provider restoring connection response', connectEvent);
|
|
908
|
+
if (connectEvent.event === 'connect') {
|
|
909
|
+
this.makeSubscriptions();
|
|
910
|
+
this.listeners.forEach(listener => listener(connectEvent));
|
|
911
|
+
}
|
|
912
|
+
else {
|
|
913
|
+
yield this.connectionStorage.removeConnection();
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
catch (e) {
|
|
917
|
+
yield this.connectionStorage.removeConnection();
|
|
918
|
+
console.error(e);
|
|
919
|
+
}
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
closeConnection() {
|
|
923
|
+
if (this.listenSubscriptions) {
|
|
924
|
+
this.injectedWallet.disconnect();
|
|
925
|
+
}
|
|
926
|
+
this.closeAllListeners();
|
|
927
|
+
}
|
|
928
|
+
disconnect() {
|
|
929
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
930
|
+
return new Promise(resolve => {
|
|
931
|
+
const onRequestSent = () => {
|
|
932
|
+
this.closeAllListeners();
|
|
933
|
+
this.connectionStorage.removeConnection().then(resolve);
|
|
934
|
+
};
|
|
935
|
+
try {
|
|
936
|
+
this.injectedWallet.disconnect();
|
|
937
|
+
onRequestSent();
|
|
938
|
+
}
|
|
939
|
+
catch (e) {
|
|
940
|
+
logDebug(e);
|
|
941
|
+
this.sendRequest({
|
|
942
|
+
method: 'disconnect',
|
|
943
|
+
params: []
|
|
944
|
+
}, onRequestSent);
|
|
945
|
+
}
|
|
946
|
+
});
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
closeAllListeners() {
|
|
950
|
+
var _a;
|
|
951
|
+
this.listenSubscriptions = false;
|
|
952
|
+
this.listeners = [];
|
|
953
|
+
(_a = this.unsubscribeCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
954
|
+
}
|
|
955
|
+
listen(eventsCallback) {
|
|
956
|
+
this.listeners.push(eventsCallback);
|
|
957
|
+
return () => (this.listeners = this.listeners.filter(listener => listener !== eventsCallback));
|
|
958
|
+
}
|
|
959
|
+
sendRequest(request, onRequestSent) {
|
|
960
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
961
|
+
const id = (yield this.connectionStorage.getNextRpcRequestId()).toString();
|
|
962
|
+
yield this.connectionStorage.increaseNextRpcRequestId();
|
|
963
|
+
logDebug('Send injected-bridge request:', Object.assign(Object.assign({}, request), { id }));
|
|
964
|
+
const result = this.injectedWallet.send(Object.assign(Object.assign({}, request), { id }));
|
|
965
|
+
result.then(response => logDebug('Wallet message received:', response));
|
|
966
|
+
onRequestSent === null || onRequestSent === void 0 ? void 0 : onRequestSent();
|
|
967
|
+
return result;
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
_connect(protocolVersion, message) {
|
|
971
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
972
|
+
try {
|
|
973
|
+
logDebug(`Injected Provider connect request: protocolVersion: ${protocolVersion}, message:`, message);
|
|
974
|
+
const connectEvent = yield this.injectedWallet.connect(protocolVersion, message);
|
|
975
|
+
logDebug('Injected Provider connect response:', connectEvent);
|
|
976
|
+
if (connectEvent.event === 'connect') {
|
|
977
|
+
yield this.updateSession();
|
|
978
|
+
this.makeSubscriptions();
|
|
979
|
+
}
|
|
980
|
+
this.listeners.forEach(listener => listener(connectEvent));
|
|
981
|
+
}
|
|
982
|
+
catch (e) {
|
|
983
|
+
logDebug(e);
|
|
984
|
+
const connectEventError = {
|
|
985
|
+
event: 'connect_error',
|
|
986
|
+
payload: {
|
|
987
|
+
code: 0,
|
|
988
|
+
message: e === null || e === void 0 ? void 0 : e.toString()
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
this.listeners.forEach(listener => listener(connectEventError));
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
makeSubscriptions() {
|
|
996
|
+
this.listenSubscriptions = true;
|
|
997
|
+
this.unsubscribeCallback = this.injectedWallet.listen(e => {
|
|
998
|
+
logDebug('Wallet message received:', e);
|
|
999
|
+
if (this.listenSubscriptions) {
|
|
1000
|
+
this.listeners.forEach(listener => listener(e));
|
|
1001
|
+
}
|
|
1002
|
+
if (e.event === 'disconnect') {
|
|
1003
|
+
this.disconnect();
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
updateSession() {
|
|
1008
|
+
return this.connectionStorage.storeConnection({
|
|
1009
|
+
type: 'injected',
|
|
1010
|
+
jsBridgeKey: this.injectedWalletKey,
|
|
1011
|
+
nextRpcRequestId: 0
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
InjectedProvider.window = getWindow();
|
|
1016
|
+
|
|
1017
|
+
class DefaultStorage {
|
|
1018
|
+
constructor() {
|
|
1019
|
+
const window = getWindow();
|
|
1020
|
+
if (!(window === null || window === void 0 ? void 0 : window.localStorage)) {
|
|
1021
|
+
throw new LocalstorageNotFoundError();
|
|
1022
|
+
}
|
|
1023
|
+
this.window = window;
|
|
1024
|
+
}
|
|
1025
|
+
getItem(key) {
|
|
1026
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1027
|
+
return Promise.resolve(this.window.localStorage.getItem(key));
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
removeItem(key) {
|
|
1031
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1032
|
+
this.window.localStorage.removeItem(key);
|
|
1033
|
+
return Promise.resolve();
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
setItem(key, value) {
|
|
1037
|
+
this.window.localStorage.setItem(key, value);
|
|
1038
|
+
return Promise.resolve();
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/**
|
|
1043
|
+
* Checks if `WalletInfo` is `WalletInfoInjectable` and `WalletInfo` is injected to the current webpage (`walletInfo.injected === true`).
|
|
1044
|
+
* @param value WalletInfo to check.
|
|
1045
|
+
*/
|
|
1046
|
+
function isWalletInfoCurrentlyInjected(value) {
|
|
1047
|
+
return isWalletInfoInjectable(value) && value.injected;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Checks if `WalletInfo` is `WalletInfoInjectable` and dApp is opened inside this wallet's browser.
|
|
1051
|
+
* @param value WalletInfo to check.
|
|
1052
|
+
*/
|
|
1053
|
+
function isWalletInfoCurrentlyEmbedded(value) {
|
|
1054
|
+
return isWalletInfoCurrentlyInjected(value) && value.embedded;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Checks if `WalletInfo` is `WalletInfoInjected`, but doesn't check if it is injected to the page or not.
|
|
1058
|
+
* @param value WalletInfo to check.
|
|
1059
|
+
*/
|
|
1060
|
+
function isWalletInfoInjectable(value) {
|
|
1061
|
+
return 'jsBridgeKey' in value;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Checks if `WalletInfo` is `WalletInfoRemote`.
|
|
1065
|
+
* @param value WalletInfo to check.
|
|
1066
|
+
*/
|
|
1067
|
+
function isWalletInfoRemote(value) {
|
|
1068
|
+
return 'bridgeUrl' in value;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* @deprecated use `isWalletInfoInjectable` or `isWalletInfoCurrentlyInjected` instead.
|
|
1072
|
+
* @param value WalletInfo to check.
|
|
1073
|
+
*/
|
|
1074
|
+
function isWalletInfoInjected(value) {
|
|
1075
|
+
return 'jsBridgeKey' in value;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
class WalletsListManager {
|
|
1079
|
+
constructor(walletsListSource) {
|
|
1080
|
+
this.walletsListCache = null;
|
|
1081
|
+
this.walletsListSource = 'https://raw.githubusercontent.com/ton-blockchain/wallets-list/main/wallets.json';
|
|
1082
|
+
if (walletsListSource) {
|
|
1083
|
+
this.walletsListSource = walletsListSource;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
getWallets() {
|
|
1087
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1088
|
+
if (!this.walletsListCache) {
|
|
1089
|
+
this.walletsListCache = this.fetchWalletsList();
|
|
1090
|
+
this.walletsListCache.catch(() => (this.walletsListCache = null));
|
|
1091
|
+
}
|
|
1092
|
+
return this.walletsListCache;
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
getEmbeddedWallet() {
|
|
1096
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1097
|
+
const walletsList = yield this.getWallets();
|
|
1098
|
+
const embeddedWallets = walletsList.filter(isWalletInfoCurrentlyEmbedded);
|
|
1099
|
+
if (embeddedWallets.length !== 1) {
|
|
1100
|
+
return null;
|
|
1101
|
+
}
|
|
1102
|
+
return embeddedWallets[0];
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
fetchWalletsList() {
|
|
1106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1107
|
+
try {
|
|
1108
|
+
const walletsResponse = yield fetch(this.walletsListSource);
|
|
1109
|
+
const walletsList = yield walletsResponse.json();
|
|
1110
|
+
if (!Array.isArray(walletsList) ||
|
|
1111
|
+
!walletsList.every(wallet => this.isCorrectWalletConfigDTO(wallet))) {
|
|
1112
|
+
throw new FetchWalletsError('Wrong wallets list format');
|
|
1113
|
+
}
|
|
1114
|
+
const currentlyInjectedWallets = InjectedProvider.getCurrentlyInjectedWallets();
|
|
1115
|
+
return this.mergeWalletsLists(this.walletConfigDTOListToWalletConfigList(walletsList), currentlyInjectedWallets);
|
|
1116
|
+
}
|
|
1117
|
+
catch (e) {
|
|
1118
|
+
throw new FetchWalletsError(e);
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
walletConfigDTOListToWalletConfigList(walletConfigDTO) {
|
|
1123
|
+
return walletConfigDTO.map(walletConfigDTO => {
|
|
1124
|
+
const walletConfig = {
|
|
1125
|
+
name: walletConfigDTO.name,
|
|
1126
|
+
imageUrl: walletConfigDTO.image,
|
|
1127
|
+
aboutUrl: walletConfigDTO.about_url,
|
|
1128
|
+
tondns: walletConfigDTO.tondns
|
|
1129
|
+
};
|
|
1130
|
+
walletConfigDTO.bridge.forEach(bridge => {
|
|
1131
|
+
if (bridge.type === 'sse') {
|
|
1132
|
+
walletConfig.bridgeUrl = bridge.url;
|
|
1133
|
+
walletConfig.universalLink =
|
|
1134
|
+
walletConfigDTO.universal_url;
|
|
1135
|
+
walletConfig.deepLink = walletConfigDTO.deepLink;
|
|
1136
|
+
}
|
|
1137
|
+
if (bridge.type === 'js') {
|
|
1138
|
+
const jsBridgeKey = bridge.key;
|
|
1139
|
+
walletConfig.jsBridgeKey = jsBridgeKey;
|
|
1140
|
+
walletConfig.injected =
|
|
1141
|
+
InjectedProvider.isWalletInjected(jsBridgeKey);
|
|
1142
|
+
walletConfig.embedded =
|
|
1143
|
+
InjectedProvider.isInsideWalletBrowser(jsBridgeKey);
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
return walletConfig;
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
mergeWalletsLists(list1, list2) {
|
|
1150
|
+
const names = new Set(list1.concat(list2).map(item => item.name));
|
|
1151
|
+
return [...names.values()].map(name => {
|
|
1152
|
+
const list1Item = list1.find(item => item.name === name);
|
|
1153
|
+
const list2Item = list2.find(item => item.name === name);
|
|
1154
|
+
return Object.assign(Object.assign({}, (list1Item && Object.assign({}, list1Item))), (list2Item && Object.assign({}, list2Item)));
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
isCorrectWalletConfigDTO(value) {
|
|
1158
|
+
if (!value || !(typeof value === 'object')) {
|
|
1159
|
+
return false;
|
|
1160
|
+
}
|
|
1161
|
+
const containsName = 'name' in value;
|
|
1162
|
+
const containsImage = 'image' in value;
|
|
1163
|
+
const containsAbout = 'about_url' in value;
|
|
1164
|
+
if (!containsName || !containsImage || !containsAbout) {
|
|
1165
|
+
return false;
|
|
1166
|
+
}
|
|
1167
|
+
if (!('bridge' in value) ||
|
|
1168
|
+
!Array.isArray(value.bridge) ||
|
|
1169
|
+
!value.bridge.length) {
|
|
1170
|
+
return false;
|
|
1171
|
+
}
|
|
1172
|
+
const bridge = value.bridge;
|
|
1173
|
+
if (bridge.some(item => !item || typeof item !== 'object' || !('type' in item))) {
|
|
1174
|
+
return false;
|
|
1175
|
+
}
|
|
1176
|
+
const sseBridge = bridge.find(item => item.type === 'sse');
|
|
1177
|
+
if (sseBridge) {
|
|
1178
|
+
if (!('url' in sseBridge) ||
|
|
1179
|
+
!sseBridge.url ||
|
|
1180
|
+
!value.universal_url) {
|
|
1181
|
+
return false;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
const jsBridge = bridge.find(item => item.type === 'js');
|
|
1185
|
+
if (jsBridge) {
|
|
1186
|
+
if (!('key' in jsBridge) || !jsBridge.key) {
|
|
1187
|
+
return false;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return true;
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* Thrown when wallet doesn't support requested feature method.
|
|
1196
|
+
*/
|
|
1197
|
+
class WalletNotSupportFeatureError extends TonConnectError {
|
|
1198
|
+
get info() {
|
|
1199
|
+
return "Wallet doesn't support requested feature method.";
|
|
1200
|
+
}
|
|
1201
|
+
constructor(...args) {
|
|
1202
|
+
super(...args);
|
|
1203
|
+
Object.setPrototypeOf(this, WalletNotSupportFeatureError.prototype);
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
function checkSendTransactionSupport(features, options) {
|
|
1208
|
+
const supportsDeprecatedSendTransactionFeature = features.includes('SendTransaction');
|
|
1209
|
+
const sendTransactionFeature = features.find(feature => feature && typeof feature === 'object' && feature.name === 'SendTransaction');
|
|
1210
|
+
if (!supportsDeprecatedSendTransactionFeature && !sendTransactionFeature) {
|
|
1211
|
+
throw new WalletNotSupportFeatureError("Wallet doesn't support SendTransaction feature.");
|
|
1212
|
+
}
|
|
1213
|
+
if (sendTransactionFeature && sendTransactionFeature.maxMessages !== undefined) {
|
|
1214
|
+
if (sendTransactionFeature.maxMessages < options.requiredMessagesNumber) {
|
|
1215
|
+
throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Max support messages number is ${sendTransactionFeature.maxMessages}, but ${options.requiredMessagesNumber} is required.`);
|
|
1216
|
+
}
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
logWarning("Connected wallet didn't provide information about max allowed messages in the SendTransaction request. Request may be rejected by the wallet.");
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
class TonConnect {
|
|
1223
|
+
constructor(options) {
|
|
1224
|
+
this.walletsList = new WalletsListManager();
|
|
1225
|
+
this._wallet = null;
|
|
1226
|
+
this.provider = null;
|
|
1227
|
+
this.statusChangeSubscriptions = [];
|
|
1228
|
+
this.statusChangeErrorSubscriptions = [];
|
|
1229
|
+
this.dappSettings = {
|
|
1230
|
+
manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
|
|
1231
|
+
storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
|
|
1232
|
+
};
|
|
1233
|
+
this.walletsList = new WalletsListManager(options === null || options === void 0 ? void 0 : options.walletsListSource);
|
|
1234
|
+
if (!this.dappSettings.manifestUrl) {
|
|
1235
|
+
throw new DappMetadataError('Dapp tonconnect-manifest.json must be specified if window.location.origin is undefined. See more https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest');
|
|
1236
|
+
}
|
|
1237
|
+
this.bridgeConnectionStorage = new BridgeConnectionStorage(this.dappSettings.storage);
|
|
1238
|
+
if (!(options === null || options === void 0 ? void 0 : options.disableAutoPauseConnection)) {
|
|
1239
|
+
this.addWindowFocusAndBlurSubscriptions();
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* Returns available wallets list.
|
|
1244
|
+
*/
|
|
1245
|
+
static getWallets() {
|
|
1246
|
+
return this.walletsList.getWallets();
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Shows if the wallet is connected right now.
|
|
1250
|
+
*/
|
|
1251
|
+
get connected() {
|
|
1252
|
+
return this._wallet !== null;
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Current connected account or null if no account is connected.
|
|
1256
|
+
*/
|
|
1257
|
+
get account() {
|
|
1258
|
+
var _a;
|
|
1259
|
+
return ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.account) || null;
|
|
1260
|
+
}
|
|
1261
|
+
/**
|
|
1262
|
+
* Current connected wallet or null if no account is connected.
|
|
1263
|
+
*/
|
|
1264
|
+
get wallet() {
|
|
1265
|
+
return this._wallet;
|
|
1266
|
+
}
|
|
1267
|
+
set wallet(value) {
|
|
1268
|
+
this._wallet = value;
|
|
1269
|
+
this.statusChangeSubscriptions.forEach(callback => callback(this._wallet));
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Returns available wallets list.
|
|
1273
|
+
*/
|
|
1274
|
+
getWallets() {
|
|
1275
|
+
return this.walletsList.getWallets();
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Allows to subscribe to connection status changes and handle connection errors.
|
|
1279
|
+
* @param callback will be called after connections status changes with actual wallet or null.
|
|
1280
|
+
* @param errorsHandler (optional) will be called with some instance of TonConnectError when connect error is received.
|
|
1281
|
+
* @returns unsubscribe callback.
|
|
1282
|
+
*/
|
|
1283
|
+
onStatusChange(callback, errorsHandler) {
|
|
1284
|
+
this.statusChangeSubscriptions.push(callback);
|
|
1285
|
+
if (errorsHandler) {
|
|
1286
|
+
this.statusChangeErrorSubscriptions.push(errorsHandler);
|
|
1287
|
+
}
|
|
1288
|
+
return () => {
|
|
1289
|
+
this.statusChangeSubscriptions = this.statusChangeSubscriptions.filter(item => item !== callback);
|
|
1290
|
+
if (errorsHandler) {
|
|
1291
|
+
this.statusChangeErrorSubscriptions = this.statusChangeErrorSubscriptions.filter(item => item !== errorsHandler);
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
connect(wallet, request) {
|
|
1296
|
+
var _a;
|
|
1297
|
+
if (this.connected) {
|
|
1298
|
+
throw new WalletAlreadyConnectedError();
|
|
1299
|
+
}
|
|
1300
|
+
(_a = this.provider) === null || _a === void 0 ? void 0 : _a.closeConnection();
|
|
1301
|
+
this.provider = this.createProvider(wallet);
|
|
1302
|
+
return this.provider.connect(this.createConnectRequest(request));
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
|
|
1306
|
+
*/
|
|
1307
|
+
restoreConnection() {
|
|
1308
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1309
|
+
const [bridgeConnectionType, embeddedWallet] = yield Promise.all([
|
|
1310
|
+
this.bridgeConnectionStorage.storedConnectionType(),
|
|
1311
|
+
this.walletsList.getEmbeddedWallet()
|
|
1312
|
+
]);
|
|
1313
|
+
try {
|
|
1314
|
+
switch (bridgeConnectionType) {
|
|
1315
|
+
case 'http':
|
|
1316
|
+
this.provider = yield BridgeProvider.fromStorage(this.dappSettings.storage);
|
|
1317
|
+
break;
|
|
1318
|
+
case 'injected':
|
|
1319
|
+
this.provider = yield InjectedProvider.fromStorage(this.dappSettings.storage);
|
|
1320
|
+
break;
|
|
1321
|
+
default:
|
|
1322
|
+
if (embeddedWallet) {
|
|
1323
|
+
this.provider = yield this.createProvider(embeddedWallet);
|
|
1324
|
+
}
|
|
1325
|
+
else {
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
catch (_a) {
|
|
1331
|
+
yield this.bridgeConnectionStorage.removeConnection();
|
|
1332
|
+
this.provider = null;
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
this.provider.listen(this.walletEventsListener.bind(this));
|
|
1336
|
+
return this.provider.restoreConnection();
|
|
1337
|
+
});
|
|
1338
|
+
}
|
|
1339
|
+
/**
|
|
1340
|
+
* Asks connected wallet to sign and send the transaction.
|
|
1341
|
+
* @param transaction transaction to send.
|
|
1342
|
+
* @returns signed transaction boc that allows you to find the transaction in the blockchain.
|
|
1343
|
+
* If user rejects transaction, method will throw the corresponding error.
|
|
1344
|
+
*/
|
|
1345
|
+
sendTransaction(transaction) {
|
|
1346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1347
|
+
this.checkConnection();
|
|
1348
|
+
checkSendTransactionSupport(this.wallet.device.features, {
|
|
1349
|
+
requiredMessagesNumber: transaction.messages.length
|
|
1350
|
+
});
|
|
1351
|
+
const { validUntil } = transaction, tx = __rest(transaction, ["validUntil"]);
|
|
1352
|
+
const from = transaction.from || this.account.address;
|
|
1353
|
+
const network = transaction.network || this.account.chain;
|
|
1354
|
+
const response = yield this.provider.sendRequest(sendTransactionParser.convertToRpcRequest(Object.assign(Object.assign({}, tx), { valid_until: validUntil, from,
|
|
1355
|
+
network })));
|
|
1356
|
+
if (sendTransactionParser.isError(response)) {
|
|
1357
|
+
return sendTransactionParser.parseAndThrowError(response);
|
|
1358
|
+
}
|
|
1359
|
+
return sendTransactionParser.convertFromRpcResponse(response);
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Disconnect form thw connected wallet and drop current session.
|
|
1364
|
+
*/
|
|
1365
|
+
disconnect() {
|
|
1366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1367
|
+
if (!this.connected) {
|
|
1368
|
+
throw new WalletNotConnectedError();
|
|
1369
|
+
}
|
|
1370
|
+
yield this.provider.disconnect();
|
|
1371
|
+
this.onWalletDisconnected();
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
/**
|
|
1375
|
+
* Pause bridge HTTP connection. Might be helpful, if you want to pause connections while browser tab is unfocused,
|
|
1376
|
+
* or if you use SDK with NodeJS and want to save server resources.
|
|
1377
|
+
*/
|
|
1378
|
+
pauseConnection() {
|
|
1379
|
+
var _a;
|
|
1380
|
+
if (((_a = this.provider) === null || _a === void 0 ? void 0 : _a.type) !== 'http') {
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
this.provider.pause();
|
|
1384
|
+
}
|
|
1385
|
+
/**
|
|
1386
|
+
* Unpause bridge HTTP connection if it is paused.
|
|
1387
|
+
*/
|
|
1388
|
+
unPauseConnection() {
|
|
1389
|
+
var _a;
|
|
1390
|
+
if (((_a = this.provider) === null || _a === void 0 ? void 0 : _a.type) !== 'http') {
|
|
1391
|
+
return Promise.resolve();
|
|
1392
|
+
}
|
|
1393
|
+
return this.provider.unPause();
|
|
1394
|
+
}
|
|
1395
|
+
addWindowFocusAndBlurSubscriptions() {
|
|
1396
|
+
const document = getDocument();
|
|
1397
|
+
if (!document) {
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
try {
|
|
1401
|
+
document.addEventListener('visibilitychange', () => {
|
|
1402
|
+
if (document.hidden) {
|
|
1403
|
+
this.pauseConnection();
|
|
1404
|
+
}
|
|
1405
|
+
else {
|
|
1406
|
+
this.unPauseConnection();
|
|
1407
|
+
}
|
|
1408
|
+
});
|
|
1409
|
+
}
|
|
1410
|
+
catch (e) {
|
|
1411
|
+
console.error('Cannot subscribe to the document.visibilitychange: ', e);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
createProvider(wallet) {
|
|
1415
|
+
let provider;
|
|
1416
|
+
if (!Array.isArray(wallet) && isWalletConnectionSourceJS(wallet)) {
|
|
1417
|
+
provider = new InjectedProvider(this.dappSettings.storage, wallet.jsBridgeKey);
|
|
1418
|
+
}
|
|
1419
|
+
else {
|
|
1420
|
+
provider = new BridgeProvider(this.dappSettings.storage, wallet);
|
|
1421
|
+
}
|
|
1422
|
+
provider.listen(this.walletEventsListener.bind(this));
|
|
1423
|
+
return provider;
|
|
1424
|
+
}
|
|
1425
|
+
walletEventsListener(e) {
|
|
1426
|
+
switch (e.event) {
|
|
1427
|
+
case 'connect':
|
|
1428
|
+
this.onWalletConnected(e.payload);
|
|
1429
|
+
break;
|
|
1430
|
+
case 'connect_error':
|
|
1431
|
+
this.onWalletConnectError(e.payload);
|
|
1432
|
+
break;
|
|
1433
|
+
case 'disconnect':
|
|
1434
|
+
this.onWalletDisconnected();
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
onWalletConnected(connectEvent) {
|
|
1438
|
+
const tonAccountItem = connectEvent.items.find(item => item.name === 'ton_addr');
|
|
1439
|
+
const tonProofItem = connectEvent.items.find(item => item.name === 'ton_proof');
|
|
1440
|
+
if (!tonAccountItem) {
|
|
1441
|
+
throw new TonConnectError('ton_addr connection item was not found');
|
|
1442
|
+
}
|
|
1443
|
+
const wallet = {
|
|
1444
|
+
device: connectEvent.device,
|
|
1445
|
+
provider: this.provider.type,
|
|
1446
|
+
account: {
|
|
1447
|
+
address: tonAccountItem.address,
|
|
1448
|
+
chain: tonAccountItem.network,
|
|
1449
|
+
walletStateInit: tonAccountItem.walletStateInit,
|
|
1450
|
+
publicKey: tonAccountItem.publicKey
|
|
1451
|
+
}
|
|
1452
|
+
};
|
|
1453
|
+
if (tonProofItem) {
|
|
1454
|
+
wallet.connectItems = {
|
|
1455
|
+
tonProof: tonProofItem
|
|
1456
|
+
};
|
|
1457
|
+
}
|
|
1458
|
+
this.wallet = wallet;
|
|
1459
|
+
}
|
|
1460
|
+
onWalletConnectError(connectEventError) {
|
|
1461
|
+
const error = connectErrorsParser.parseError(connectEventError);
|
|
1462
|
+
this.statusChangeErrorSubscriptions.forEach(errorsHandler => errorsHandler(error));
|
|
1463
|
+
console.debug(error);
|
|
1464
|
+
if (error instanceof ManifestNotFoundError || error instanceof ManifestContentErrorError) {
|
|
1465
|
+
console.error(error);
|
|
1466
|
+
throw error;
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
onWalletDisconnected() {
|
|
1470
|
+
this.wallet = null;
|
|
1471
|
+
}
|
|
1472
|
+
checkConnection() {
|
|
1473
|
+
if (!this.connected) {
|
|
1474
|
+
throw new WalletNotConnectedError();
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
createConnectRequest(request) {
|
|
1478
|
+
const items = [
|
|
1479
|
+
{
|
|
1480
|
+
name: 'ton_addr'
|
|
1481
|
+
}
|
|
1482
|
+
];
|
|
1483
|
+
if (request === null || request === void 0 ? void 0 : request.tonProof) {
|
|
1484
|
+
items.push({
|
|
1485
|
+
name: 'ton_proof',
|
|
1486
|
+
payload: request.tonProof
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1489
|
+
return {
|
|
1490
|
+
manifestUrl: this.dappSettings.manifestUrl,
|
|
1491
|
+
items
|
|
1492
|
+
};
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
TonConnect.walletsList = new WalletsListManager();
|
|
1496
|
+
/**
|
|
1497
|
+
* Check if specified wallet is injected and available to use with the app.
|
|
1498
|
+
* @param walletJSKey target wallet's js bridge key.
|
|
1499
|
+
*/
|
|
1500
|
+
TonConnect.isWalletInjected = (walletJSKey) => InjectedProvider.isWalletInjected(walletJSKey);
|
|
1501
|
+
/**
|
|
1502
|
+
* Check if the app is opened inside specified wallet's browser.
|
|
1503
|
+
* @param walletJSKey target wallet's js bridge key.
|
|
1504
|
+
*/
|
|
1505
|
+
TonConnect.isInsideWalletBrowser = (walletJSKey) => InjectedProvider.isInsideWalletBrowser(walletJSKey);
|
|
1506
|
+
|
|
1507
|
+
const bounceableTag = 0x11;
|
|
1508
|
+
const testOnlyTag = 0x80;
|
|
1509
|
+
/**
|
|
1510
|
+
* Converts raw TON address to bounceable user-friendly format. [See details]{@link https://ton.org/docs/learn/overviews/addresses#user-friendly-address}
|
|
1511
|
+
* @param hexAddress raw TON address formatted as "0:<hex string without 0x>".
|
|
1512
|
+
* @param [testOnly=false] convert address to test-only form. [See details]{@link https://ton.org/docs/learn/overviews/addresses#user-friendly-address}
|
|
1513
|
+
*/
|
|
1514
|
+
function toUserFriendlyAddress(hexAddress, testOnly = false) {
|
|
1515
|
+
const { wc, hex } = parseHexAddress(hexAddress);
|
|
1516
|
+
let tag = bounceableTag;
|
|
1517
|
+
if (testOnly) {
|
|
1518
|
+
tag |= testOnlyTag;
|
|
1519
|
+
}
|
|
1520
|
+
const addr = new Int8Array(34);
|
|
1521
|
+
addr[0] = tag;
|
|
1522
|
+
addr[1] = wc;
|
|
1523
|
+
addr.set(hex, 2);
|
|
1524
|
+
const addressWithChecksum = new Uint8Array(36);
|
|
1525
|
+
addressWithChecksum.set(addr);
|
|
1526
|
+
addressWithChecksum.set(crc16(addr), 34);
|
|
1527
|
+
let addressBase64 = protocol.Base64.encode(addressWithChecksum);
|
|
1528
|
+
return addressBase64.replace(/\+/g, '-').replace(/\//g, '_');
|
|
1529
|
+
}
|
|
1530
|
+
function parseHexAddress(hexAddress) {
|
|
1531
|
+
if (!hexAddress.includes(':')) {
|
|
1532
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. Address must include ":".`);
|
|
1533
|
+
}
|
|
1534
|
+
const parts = hexAddress.split(':');
|
|
1535
|
+
if (parts.length !== 2) {
|
|
1536
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. Address must include ":" only once.`);
|
|
1537
|
+
}
|
|
1538
|
+
const wc = parseInt(parts[0]);
|
|
1539
|
+
if (wc !== 0 && wc !== -1) {
|
|
1540
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. WC must be eq 0 or -1, but ${wc} received.`);
|
|
1541
|
+
}
|
|
1542
|
+
const hex = parts[1];
|
|
1543
|
+
if ((hex === null || hex === void 0 ? void 0 : hex.length) !== 64) {
|
|
1544
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. Hex part must be 64bytes length, but ${hex === null || hex === void 0 ? void 0 : hex.length} received.`);
|
|
1545
|
+
}
|
|
1546
|
+
return {
|
|
1547
|
+
wc,
|
|
1548
|
+
hex: hexToBytes(hex)
|
|
1549
|
+
};
|
|
1550
|
+
}
|
|
1551
|
+
function crc16(data) {
|
|
1552
|
+
const poly = 0x1021;
|
|
1553
|
+
let reg = 0;
|
|
1554
|
+
const message = new Uint8Array(data.length + 2);
|
|
1555
|
+
message.set(data);
|
|
1556
|
+
for (let byte of message) {
|
|
1557
|
+
let mask = 0x80;
|
|
1558
|
+
while (mask > 0) {
|
|
1559
|
+
reg <<= 1;
|
|
1560
|
+
if (byte & mask) {
|
|
1561
|
+
reg += 1;
|
|
1562
|
+
}
|
|
1563
|
+
mask >>= 1;
|
|
1564
|
+
if (reg > 0xffff) {
|
|
1565
|
+
reg &= 0xffff;
|
|
1566
|
+
reg ^= poly;
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return new Uint8Array([Math.floor(reg / 256), reg % 256]);
|
|
1571
|
+
}
|
|
1572
|
+
const toByteMap = {};
|
|
1573
|
+
for (let ord = 0; ord <= 0xff; ord++) {
|
|
1574
|
+
let s = ord.toString(16);
|
|
1575
|
+
if (s.length < 2) {
|
|
1576
|
+
s = '0' + s;
|
|
1577
|
+
}
|
|
1578
|
+
toByteMap[s] = ord;
|
|
1579
|
+
}
|
|
1580
|
+
function hexToBytes(hex) {
|
|
1581
|
+
hex = hex.toLowerCase();
|
|
1582
|
+
const length2 = hex.length;
|
|
1583
|
+
if (length2 % 2 !== 0) {
|
|
1584
|
+
throw new ParseHexError('Hex string must have length a multiple of 2: ' + hex);
|
|
1585
|
+
}
|
|
1586
|
+
const length = length2 / 2;
|
|
1587
|
+
const result = new Uint8Array(length);
|
|
1588
|
+
for (let i = 0; i < length; i++) {
|
|
1589
|
+
const doubled = i * 2;
|
|
1590
|
+
const hexSubstring = hex.substring(doubled, doubled + 2);
|
|
1591
|
+
if (!toByteMap.hasOwnProperty(hexSubstring)) {
|
|
1592
|
+
throw new ParseHexError('Invalid hex character: ' + hexSubstring);
|
|
1593
|
+
}
|
|
1594
|
+
result[i] = toByteMap[hexSubstring];
|
|
1595
|
+
}
|
|
1596
|
+
return result;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
Object.defineProperty(exports, 'CHAIN', {
|
|
1600
|
+
enumerable: true,
|
|
1601
|
+
get: function () { return protocol.CHAIN; }
|
|
1602
|
+
});
|
|
1603
|
+
Object.defineProperty(exports, 'CONNECT_ITEM_ERROR_CODES', {
|
|
1604
|
+
enumerable: true,
|
|
1605
|
+
get: function () { return protocol.CONNECT_ITEM_ERROR_CODES; }
|
|
1606
|
+
});
|
|
1607
|
+
exports.BadRequestError = BadRequestError;
|
|
1608
|
+
exports.FetchWalletsError = FetchWalletsError;
|
|
1609
|
+
exports.LocalstorageNotFoundError = LocalstorageNotFoundError;
|
|
1610
|
+
exports.ParseHexError = ParseHexError;
|
|
1611
|
+
exports.TonConnect = TonConnect;
|
|
1612
|
+
exports.TonConnectError = TonConnectError;
|
|
1613
|
+
exports.UnknownAppError = UnknownAppError;
|
|
1614
|
+
exports.UnknownError = UnknownError;
|
|
1615
|
+
exports.UserRejectsError = UserRejectsError;
|
|
1616
|
+
exports.WalletAlreadyConnectedError = WalletAlreadyConnectedError;
|
|
1617
|
+
exports.WalletNotConnectedError = WalletNotConnectedError;
|
|
1618
|
+
exports.WalletNotInjectedError = WalletNotInjectedError;
|
|
1619
|
+
exports.WrongAddressError = WrongAddressError;
|
|
1620
|
+
exports.default = TonConnect;
|
|
1621
|
+
exports.isWalletInfoCurrentlyEmbedded = isWalletInfoCurrentlyEmbedded;
|
|
1622
|
+
exports.isWalletInfoCurrentlyInjected = isWalletInfoCurrentlyInjected;
|
|
1623
|
+
exports.isWalletInfoInjectable = isWalletInfoInjectable;
|
|
1624
|
+
exports.isWalletInfoInjected = isWalletInfoInjected;
|
|
1625
|
+
exports.isWalletInfoRemote = isWalletInfoRemote;
|
|
1626
|
+
exports.toUserFriendlyAddress = toUserFriendlyAddress;
|
|
1627
|
+
//# sourceMappingURL=index.cjs.map
|