├── .gitattributes ├── .github ├── dependabot.yml └── workflows │ └── release.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── TESTS.md ├── adnl ├── api │ └── ton-kotlin-adnl.api ├── build.gradle.kts ├── src │ ├── connection │ │ ├── AdnlClient.kt │ │ ├── AdnlClientImpl.kt │ │ ├── AdnlConnection.kt │ │ ├── AdnlConnectionFactory.kt │ │ ├── AdnlConnectionPool.kt │ │ └── AdnlRequestData.kt │ ├── engine │ │ └── AdnlNetworkEngine.kt │ ├── exception │ │ ├── AdnlOldPacketSeqnoException.kt │ │ ├── AdnlOldPeerReinitDateException.kt │ │ ├── AdnlTooNewAckSeqnoException.kt │ │ ├── AdnlTooNewAddressListReinitDateException.kt │ │ ├── AdnlTooNewLocalReinitDateException.kt │ │ ├── AdnlTooNewRemoteReinitDateException.kt │ │ └── UnknownAdnlDestinationException.kt │ ├── network │ │ ├── IPAddress.kt │ │ ├── TcpClient.kt │ │ ├── TcpClientImpl.kt │ │ ├── UdpServer.kt │ │ └── UdpServerImpl.kt │ └── utils.kt ├── src@apple │ └── network │ │ ├── TcpClientImpl.kt │ │ └── UdpServerImpl.kt ├── src@jvm │ ├── engine │ │ └── CIOAdnlNetworkEngine.kt │ ├── network │ │ ├── TcpClientImpl.kt │ │ └── UdpServerImpl.kt │ └── utils │ │ └── ip.kt ├── src@linux │ └── network │ │ ├── TcpClientImpl.kt │ │ └── UdpServerImpl.kt └── src@mingw │ └── network │ ├── TcpClientImpl.kt │ └── UdpServerImpl.kt ├── bigint ├── api │ └── ton-kotlin-bigint.api ├── build.gradle.kts ├── src │ └── BigInt.kt ├── src@jvm │ └── BigIntJvm.kt └── src@native │ └── BigInt.kt ├── bitstring ├── api │ └── ton-kotlin-bitstring.api ├── build.gradle.kts ├── src │ ├── BitString.kt │ ├── ByteBackedBitString.kt │ ├── ByteBackedMutableBitString.kt │ ├── EmptyBitString.kt │ ├── FiftHexBitStringSerializer.kt │ ├── MutableBitString.kt │ └── exception │ │ └── exceptions.kt └── test │ └── BitStringTest.kt ├── block-tlb ├── api │ └── ton-kotlin-block-tlb.api ├── build.gradle.kts ├── src │ ├── AccStatusChange.kt │ ├── AccountActive.kt │ ├── AccountBlock.kt │ ├── AccountFrozen.kt │ ├── AccountState.kt │ ├── AccountStatus.kt │ ├── AccountStorage.kt │ ├── AccountUninit.kt │ ├── ActionChangeLibrary.kt │ ├── ActionReserveCurrency.kt │ ├── ActionSendMsg.kt │ ├── ActionSetCode.kt │ ├── AddrExtern.kt │ ├── AddrNone.kt │ ├── AddrStd.kt │ ├── AddrVar.kt │ ├── Anycast.kt │ ├── BinTree.kt │ ├── BinTreeFork.kt │ ├── BinTreeLeaf.kt │ ├── BlkMasterInfo.kt │ ├── BlkPrevInfo.kt │ ├── Block.kt │ ├── BlockCreateStats.kt │ ├── BlockCreateStatsExt.kt │ ├── BlockCreateStatsRegular.kt │ ├── BlockExtra.kt │ ├── BlockInfo.kt │ ├── Certificate.kt │ ├── CertificateEnv.kt │ ├── ChainedSignature.kt │ ├── ChunkRef.kt │ ├── ChunkRefEmpty.kt │ ├── Coins.kt │ ├── CommonMsgInfo.kt │ ├── CommonMsgInfoRelaxed.kt │ ├── ConfigParams.kt │ ├── Counters.kt │ ├── CreatorStats.kt │ ├── CryptoSignature.kt │ ├── CryptoSignaturePair.kt │ ├── CryptoSignatureSimple.kt │ ├── CurrencyCollection.kt │ ├── DepthBalanceInfo.kt │ ├── DnsAdnlAddress.kt │ ├── DnsNextResolver.kt │ ├── DnsRecord.kt │ ├── DnsSmcAddress.kt │ ├── DnsText.kt │ ├── Either.kt │ ├── EnqueuedMsg.kt │ ├── ExtBlkRef.kt │ ├── ExtInMsgInfo.kt │ ├── ExtOutMsgInfo.kt │ ├── ExtraCurrencyCollection.kt │ ├── FutureSplitMerge.kt │ ├── FutureSplitMergeMerge.kt │ ├── FutureSplitMergeNone.kt │ ├── FutureSplitMergeSplit.kt │ ├── GlobalVersion.kt │ ├── HashUpdate.kt │ ├── IhrPendingSince.kt │ ├── ImportFees.kt │ ├── InMsg.kt │ ├── IntMsgInfo.kt │ ├── IntermediateAddress.kt │ ├── IntermediateAddressExt.kt │ ├── IntermediateAddressRegular.kt │ ├── IntermediateAddressSimple.kt │ ├── KeyExtBlkRef.kt │ ├── KeyMaxLt.kt │ ├── LibDescr.kt │ ├── LibRef.kt │ ├── LibRefHash.kt │ ├── LibRefRef.kt │ ├── Maybe.kt │ ├── McBlockExtra.kt │ ├── McStateExtra.kt │ ├── MerkleProof.kt │ ├── MerkleUpdate.kt │ ├── Message.kt │ ├── MessageRelaxed.kt │ ├── MsgAddress.kt │ ├── MsgAddressExt.kt │ ├── MsgAddressInt.kt │ ├── MsgDiscardFin.kt │ ├── MsgDiscardTr.kt │ ├── MsgEnvelope.kt │ ├── MsgExportDeq.kt │ ├── MsgExportDeqImm.kt │ ├── MsgExportDeqShort.kt │ ├── MsgExportExt.kt │ ├── MsgExportImm.kt │ ├── MsgExportNew.kt │ ├── MsgExportTr.kt │ ├── MsgExportTrReq.kt │ ├── MsgImportExt.kt │ ├── MsgImportFin.kt │ ├── MsgImportIhr.kt │ ├── MsgImportImm.kt │ ├── MsgImportTr.kt │ ├── OldMcBlocksInfo.kt │ ├── OutAction.kt │ ├── OutList.kt │ ├── OutListEmpty.kt │ ├── OutListLink.kt │ ├── OutListNode.kt │ ├── OutMsg.kt │ ├── OutMsgQueueInfo.kt │ ├── PrevBlkInfo.kt │ ├── PrevBlksInfo.kt │ ├── ProcessedUpto.kt │ ├── ProtoHttp.kt │ ├── ProtoList.kt │ ├── ProtoListNext.kt │ ├── ProtoListNil.kt │ ├── Protocol.kt │ ├── ShardAccounts.kt │ ├── ShardDescr.kt │ ├── ShardFeeCreated.kt │ ├── ShardHashes.kt │ ├── ShardIdent.kt │ ├── ShardState.kt │ ├── ShardStateUnsplit.kt │ ├── SigPubKey.kt │ ├── SignedCertificate.kt │ ├── SimpleLib.kt │ ├── SmartContractInfo.kt │ ├── SmcCapList.kt │ ├── SmcCapability.kt │ ├── SplitMergeInfo.kt │ ├── SplitState.kt │ ├── StateInit.kt │ ├── StorageInfo.kt │ ├── StorageUsed.kt │ ├── StorageUsedShort.kt │ ├── Text.kt │ ├── TextChunk.kt │ ├── TextChunkEmpty.kt │ ├── TextChunkRef.kt │ ├── TextChunks.kt │ ├── TickTock.kt │ ├── ValidatorBaseInfo.kt │ ├── ValidatorInfo.kt │ ├── ValueFlow.kt │ ├── VarInteger.kt │ ├── VarUInteger.kt │ ├── VmCellSlice.kt │ ├── VmCont.kt │ ├── VmContEnvelope.kt │ ├── VmContQuit.kt │ ├── VmContQuitExc.kt │ ├── VmContRepeat.kt │ ├── VmContStd.kt │ ├── VmControlData.kt │ ├── VmGasLimits.kt │ ├── VmLibraries.kt │ ├── VmSaveList.kt │ ├── VmStack.kt │ ├── VmStackBuilder.kt │ ├── VmStackCell.kt │ ├── VmStackCont.kt │ ├── VmStackInt.kt │ ├── VmStackList.kt │ ├── VmStackNan.kt │ ├── VmStackNull.kt │ ├── VmStackNumber.kt │ ├── VmStackSlice.kt │ ├── VmStackTinyInt.kt │ ├── VmStackTuple.kt │ ├── VmStackValue.kt │ ├── VmTuple.kt │ ├── VmTupleNil.kt │ ├── VmTupleRef.kt │ ├── VmTupleTcons.kt │ ├── account │ │ ├── Account.kt │ │ └── ShardAccount.kt │ ├── blocktlb.kt │ ├── config │ │ ├── BurningConfig.kt │ │ └── StoragePrices.kt │ ├── currency │ │ └── VarUInt248.kt │ ├── message │ │ └── MessageLayout.kt │ └── transaction │ │ ├── Transaction.kt │ │ ├── TransactionInfo.kt │ │ └── phase │ │ ├── ActionPhase.kt │ │ ├── BouncePhase.kt │ │ ├── ComputePhase.kt │ │ ├── CreditPhase.kt │ │ └── StoragePhase.kt └── test │ ├── AddrStdTest.kt │ ├── MsgAddressIntTest.kt │ ├── StateInitTest.kt │ ├── TextTest.kt │ ├── VmStackListTest.kt │ ├── VmStackTest.kt │ ├── VmStackValue.kt │ └── util.kt ├── build-logic ├── build.gradle.kts └── src │ └── main │ └── kotlin │ ├── multiplatform.gradle.kts │ └── publish.gradle.kts ├── build.gradle.kts ├── contract ├── api │ └── ton-kotlin-contract.api ├── build.gradle.kts ├── src │ ├── CellString.kt │ ├── ChunkedData.kt │ ├── ContentData.kt │ ├── FullContent.kt │ ├── SmartContract.kt │ ├── SnakeData.kt │ ├── Text.kt │ ├── exception │ │ └── AccountNotInitializedException.kt │ └── wallet │ │ ├── HighLoadWalletV2Contract.kt │ │ ├── MessageText.kt │ │ ├── WalletContract.kt │ │ ├── WalletMessage.kt │ │ ├── WalletTransfer.kt │ │ ├── WalletV3Contract.kt │ │ └── WalletV4R2Contract.kt └── test │ ├── SnakeDataTest.kt │ └── wallet │ ├── GlobalConfig.kt │ ├── LiteClient.kt │ └── WalletV4Example.kt ├── crypto ├── api │ └── ton-kotlin-crypto.api ├── build.gradle.kts ├── src │ ├── AesCtr.kt │ ├── Decryptor.kt │ ├── Ed25519.kt │ ├── Encryptor.kt │ ├── HexByteArraySerializer.kt │ ├── SecureRandom.kt │ ├── crc16.kt │ ├── encoding │ │ └── base64.kt │ └── mnemonic │ │ ├── Mnemonic.kt │ │ └── bip39.kt ├── src@apple │ └── SecureRandom.kt ├── src@jvm │ ├── AesCtr.kt │ └── SecureRandom.kt ├── src@linux │ └── SecureRandom.kt ├── src@mingw │ └── SecureRandom.kt ├── src@native │ ├── Aes256.kt │ └── AesCtr.kt └── test │ ├── Crc16Test.kt │ └── HexTest.kt ├── examples ├── build.gradle.kts └── src │ ├── ConfigContractExample.kt │ ├── ExtraCurrencyExample.kt │ ├── ExtraCurrencyMonitor.kt │ ├── TestSer.kt │ ├── WalletV4R2Example.kt │ ├── contract │ ├── BaseWalletContract.kt │ ├── WalletMessage.kt │ ├── WalletV1R3Contract.kt │ └── config │ │ └── ConfigContract.kt │ ├── faucet │ └── TestnetFaucet.kt │ └── provider │ ├── GlobalConfig.kt │ ├── LiteClient.kt │ └── Provider.kt ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── hashmap-tlb ├── api │ └── ton-kotlin-hashmap-tlb.api ├── build.gradle.kts ├── src │ ├── AugmentedDictionary.kt │ ├── Dictionary.kt │ ├── DictionaryKeyCodec.kt │ ├── DictionarySet.kt │ ├── HashMapE.kt │ ├── HashMapNode.kt │ ├── HashmapAug.kt │ ├── HashmapAugE.kt │ ├── HashmapAugNode.kt │ ├── HmEdge.kt │ ├── HmLabel.kt │ ├── HmeEmpty.kt │ ├── HmeRoot.kt │ ├── HmlLong.kt │ ├── HmlSame.kt │ ├── HmlShort.kt │ ├── HmnFork.kt │ ├── HmnLeaf.kt │ ├── Label.kt │ ├── RawDictionary.kt │ ├── Unary.kt │ ├── UnarySuccess.kt │ └── UnaryZero.kt └── test │ ├── HashMapEdgeTest.kt │ ├── HashMapEditTest.kt │ ├── HashMapLabelTest.kt │ ├── UnaryTest.kt │ └── util.kt ├── libs.versions.toml ├── liteapi-tl ├── api │ └── ton-kotlin-liteapi-tl.api ├── build.gradle.kts └── src │ ├── LiteApi.kt │ ├── LiteApiClient.kt │ ├── exception │ └── exceptions.kt │ └── liteserver │ ├── LiteServerAccountId.kt │ ├── LiteServerAccountState.kt │ ├── LiteServerAllShardsInfo.kt │ ├── LiteServerBlockData.kt │ ├── LiteServerBlockHeader.kt │ ├── LiteServerBlockLink.kt │ ├── LiteServerBlockLinkBack.kt │ ├── LiteServerBlockLinkForward.kt │ ├── LiteServerBlockState.kt │ ├── LiteServerBlockTransactions.kt │ ├── LiteServerConfigInfo.kt │ ├── LiteServerCurrentTime.kt │ ├── LiteServerDebugVerbosity.kt │ ├── LiteServerError.kt │ ├── LiteServerMasterchainInfo.kt │ ├── LiteServerMasterchainInfoExt.kt │ ├── LiteServerPartialBlockProof.kt │ ├── LiteServerQueryPrefix.kt │ ├── LiteServerRunMethodResult.kt │ ├── LiteServerSendMsgStatus.kt │ ├── LiteServerShardInfo.kt │ ├── LiteServerSignature.kt │ ├── LiteServerSignatureSet.kt │ ├── LiteServerTransactionId.kt │ ├── LiteServerTransactionId3.kt │ ├── LiteServerTransactionInfo.kt │ ├── LiteServerTransactionList.kt │ ├── LiteServerValidatorStats.kt │ ├── LiteServerVersion.kt │ └── functions │ ├── LiteServerGetAccountState.kt │ ├── LiteServerGetAllShardsInfo.kt │ ├── LiteServerGetBlock.kt │ ├── LiteServerGetBlockHeader.kt │ ├── LiteServerGetBlockProof.kt │ ├── LiteServerGetConfigAll.kt │ ├── LiteServerGetConfigParams.kt │ ├── LiteServerGetMasterchainInfo.kt │ ├── LiteServerGetMasterchainInfoExt.kt │ ├── LiteServerGetOneTransaction.kt │ ├── LiteServerGetShardInfo.kt │ ├── LiteServerGetState.kt │ ├── LiteServerGetTime.kt │ ├── LiteServerGetTransactions.kt │ ├── LiteServerGetValidatorStats.kt │ ├── LiteServerGetVersion.kt │ ├── LiteServerListBlockTransactions.kt │ ├── LiteServerLookupBlock.kt │ ├── LiteServerQuery.kt │ ├── LiteServerRunSmcMethod.kt │ ├── LiteServerSendMessage.kt │ └── LiteServerWaitMasterchainSeqno.kt ├── liteclient ├── api │ └── ton-kotlin-liteclient.api ├── build.gradle.kts ├── src │ ├── CheckProofUtils.kt │ ├── LiteClient.kt │ ├── LiteClientApi.kt │ └── intetnal │ │ └── BlockHeaderResult.kt └── test@jvm │ ├── Config.kt │ └── LiteClientIntegrationTest.kt ├── settings.gradle.kts ├── tl ├── api │ └── ton-kotlin-tl.api ├── build.gradle.kts ├── src │ ├── AbstractTlCombinator.kt │ ├── ByteStringBase64Serializer.kt │ ├── TlCodec.kt │ ├── TlCombinator.kt │ ├── TlConstructor.kt │ ├── TlDecoder.kt │ ├── TlEncoder.kt │ ├── TlReader.kt │ ├── TlWriter.kt │ └── constructors │ │ ├── BoolTlConstructor.kt │ │ ├── BytesTlConstructor.kt │ │ └── EnumTlCombinator.kt └── test │ └── BytesTlConstructorTest.kt ├── tlb ├── api │ └── ton-kotlin-tlb.api ├── build.gradle.kts └── src │ ├── AbstractTlbCombinator.kt │ ├── CellRef.kt │ ├── TlbCodec.kt │ ├── TlbCombinator.kt │ ├── TlbConstructor.kt │ ├── TlbNegatedCombinator.kt │ ├── TlbObject.kt │ ├── TlbPrettyPrinter.kt │ ├── constructor │ ├── exception │ ├── ParseTlbException.kt │ └── UnknownTlbConstructorException.kt │ └── providers │ ├── TlbCombinatorProvider.kt │ ├── TlbConstructorProvider.kt │ └── TlbProvider.kt ├── ton-kotlin-dht ├── build.gradle.kts └── src │ └── commonMain │ └── kotlin │ └── org │ └── ton │ └── dht │ ├── Dht.kt │ ├── DhtBucket.kt │ ├── DhtMember.kt │ ├── DhtRemoteNode.kt │ └── storage │ ├── DhtStorage.kt │ └── HeapDhtStorage.kt ├── ton-kotlin-experimental ├── build.gradle.kts └── src │ ├── commonMain │ └── kotlin │ │ └── org │ │ └── ton │ │ └── proxy │ │ ├── ProxyClient.kt │ │ ├── ProxyServer.kt │ │ ├── adnl │ │ ├── Adnl.kt │ │ ├── AdnlMessageReceiver.kt │ │ ├── AdnlPacketReceiver.kt │ │ ├── AdnlPeer.kt │ │ ├── AdnlPeerSession.kt │ │ ├── AdnlSender.kt │ │ ├── PacketsHistory.kt │ │ ├── PeerState.kt │ │ ├── channel │ │ │ ├── AdnlChannel.kt │ │ │ ├── AdnlInputChannel.kt │ │ │ └── AdnlOutputChannel.kt │ │ ├── engine │ │ │ ├── AdnlNetworkEngine.kt │ │ │ └── CIOAdnlNetworkEngine.kt │ │ └── resolver │ │ │ ├── AdnlAddressResolver.kt │ │ │ ├── CachedAdnlAddressResolver.kt │ │ │ └── MapAdnlAddressResolver.kt │ │ ├── dht │ │ ├── Dht.kt │ │ ├── DhtBucket.kt │ │ ├── DhtConfig.kt │ │ ├── DhtPeer.kt │ │ ├── DhtValuesFlow.kt │ │ ├── state │ │ │ ├── AbstractDhtState.kt │ │ │ ├── DhtState.kt │ │ │ ├── FullDhtState.kt │ │ │ └── LiteDhtState.kt │ │ └── storage │ │ │ ├── AbstractDhtStorage.kt │ │ │ ├── DhtStorage.kt │ │ │ ├── DhtStorageConfig.kt │ │ │ ├── InMemoryDhtStorage.kt │ │ │ └── exception │ │ │ └── DhtStorageException.kt │ │ ├── dns │ │ ├── DnsCategory.kt │ │ └── DnsResolver.kt │ │ ├── http │ │ ├── HttpProxy.kt │ │ ├── HttpProxyPeerSession.kt │ │ └── headers.kt │ │ └── rldp │ │ ├── Rldp.kt │ │ ├── RldpInputTransfer.kt │ │ ├── RldpOutputTransfer.kt │ │ ├── RldpPeerSession.kt │ │ ├── RldpReceiver.kt │ │ └── fec │ │ ├── FecDecoder.kt │ │ ├── FecEncoder.kt │ │ └── raptorq │ │ ├── Deg.kt │ │ ├── InactivateDecoder.kt │ │ ├── Params.kt │ │ ├── Rand.kt │ │ ├── RaptorQFecDecoder.kt │ │ ├── RaptorQFecEncoder.kt │ │ ├── RawParams.kt │ │ ├── Solver.kt │ │ ├── Symbol.kt │ │ ├── Tuple.kt │ │ └── math │ │ ├── GF256.kt │ │ ├── MatrixGF2.kt │ │ ├── MatrixGF256.kt │ │ ├── Octet.kt │ │ └── SparseMatrixGF2.kt │ └── jvmTest │ └── kotlin │ └── org │ └── ton │ └── experimental │ ├── Proxy.kt │ ├── RldpTest.kt │ ├── Test.kt │ └── raptorq.kt ├── ton-kotlin-fift ├── build.gradle.kts └── src │ ├── commonMain │ ├── kotlin │ │ └── org │ │ │ └── ton │ │ │ └── fift │ │ │ ├── Box.kt │ │ │ ├── dictionary.kt │ │ │ ├── exceptions.kt │ │ │ ├── interpretator.kt │ │ │ ├── stack.kt │ │ │ ├── utils.kt │ │ │ └── words.kt │ └── resources │ │ └── Fift.fif │ └── commonTest │ └── kotlin │ └── org │ └── ton │ └── fift │ └── WordsTest.kt ├── ton-kotlin-rldp └── build.gradle.kts ├── tonapi-tl ├── api │ └── ton-kotlin-tonapi-tl.api ├── build.gradle.kts ├── src │ ├── SignedTlObject.kt │ ├── adnl │ │ ├── AdnlAddress.kt │ │ ├── AdnlAddressList.kt │ │ ├── AdnlIdShort.kt │ │ ├── AdnlNode.kt │ │ ├── AdnlNodes.kt │ │ ├── AdnlPacketContents.kt │ │ ├── AdnlPing.kt │ │ ├── AdnlPong.kt │ │ ├── AdnlProxy.kt │ │ ├── AdnlProxyTo.kt │ │ ├── AdnlProxyToSign.kt │ │ ├── config │ │ │ └── AdnlConfigGlobal.kt │ │ └── message │ │ │ ├── AdnlMessage.kt │ │ │ ├── AdnlMessageAnswer.kt │ │ │ ├── AdnlMessageConfirmChannel.kt │ │ │ ├── AdnlMessageCreateChannel.kt │ │ │ ├── AdnlMessageCustom.kt │ │ │ ├── AdnlMessageNop.kt │ │ │ ├── AdnlMessagePart.kt │ │ │ ├── AdnlMessageQuery.kt │ │ │ └── AdnlMessageReinit.kt │ ├── config │ │ ├── ConfigGlobal.kt │ │ └── ConfigLocal.kt │ ├── control │ │ └── ControlConfigLocal.kt │ ├── dht │ │ ├── DhtKey.kt │ │ ├── DhtKeyDescription.kt │ │ ├── DhtMessage.kt │ │ ├── DhtNode.kt │ │ ├── DhtNodes.kt │ │ ├── DhtPong.kt │ │ ├── DhtStore.kt │ │ ├── DhtStored.kt │ │ ├── DhtUpdateRule.kt │ │ ├── DhtValue.kt │ │ ├── DhtValueResult.kt │ │ ├── config │ │ │ ├── DhtConfigGlobal.kt │ │ │ ├── DhtConfigIdLocal.kt │ │ │ ├── DhtConfigLocal.kt │ │ │ └── DhtConfigRandomLocal.kt │ │ ├── db │ │ │ ├── DhtDbBucket.kt │ │ │ └── DhtDbKey.kt │ │ └── functions │ │ │ ├── DhtFindValue.kt │ │ │ ├── DhtPing.kt │ │ │ └── DhtQuery.kt │ ├── exception │ │ ├── TvmException.kt │ │ └── exceptions.kt │ ├── fec │ │ ├── FecRaptorQ.kt │ │ └── FecType.kt │ ├── http │ │ ├── HttpHeader.kt │ │ ├── HttpPayloadPart.kt │ │ ├── HttpResponse.kt │ │ ├── functions │ │ │ ├── HttpGetNextPayloadPart.kt │ │ │ └── HttpRequest.kt │ │ └── server │ │ │ ├── HttpServerDnsEntry.kt │ │ │ └── HttpServerHost.kt │ ├── id │ │ └── config │ │ │ └── IdConfigLocal.kt │ ├── liteclient │ │ └── config │ │ │ └── LiteClientConfigGlobal.kt │ ├── liteserver │ │ ├── LiteServerDesc.kt │ │ └── config │ │ │ ├── LiteServerConfigLocal.kt │ │ │ └── LiteServerConfigRandomLocal.kt │ ├── overlay │ │ ├── OverlayNode.kt │ │ ├── OverlayNodeToSign.kt │ │ └── OverlayNodes.kt │ ├── pk │ │ ├── aes.kt │ │ ├── ed25519.kt │ │ ├── overlay.kt │ │ ├── pk.kt │ │ └── unenc.kt │ ├── pub │ │ ├── ed25519.kt │ │ └── pub.kt │ ├── rldp │ │ ├── RldpAnswer.kt │ │ ├── RldpComplete.kt │ │ ├── RldpConfirm.kt │ │ ├── RldpMessage.kt │ │ ├── RldpMessageData.kt │ │ ├── RldpMessagePart.kt │ │ ├── RldpMessagePartData.kt │ │ └── RldpQuery.kt │ ├── tcp │ │ ├── TcpAuthentificate.kt │ │ ├── TcpAuthentificationComplete.kt │ │ ├── TcpAuthentificationNonce.kt │ │ ├── TcpMessage.kt │ │ ├── TcpPing.kt │ │ └── TcpPong.kt │ ├── tonnode │ │ ├── Shard.kt │ │ ├── TonNodeBlockId.kt │ │ ├── TonNodeBlockIdExt.kt │ │ ├── TonNodeZeroStateIdExt.kt │ │ └── Workchain.kt │ └── validator │ │ └── config │ │ ├── ValidatorConfigGlobal.kt │ │ ├── ValidatorConfigLocal.kt │ │ └── ValidatorConfigRandomLocal.kt └── test │ └── PrivateKeyTest.kt └── tvm ├── api └── ton-kotlin-tvm.api ├── build.gradle.kts ├── src ├── Tvm.kt ├── boc │ ├── BagOfCells.kt │ ├── BagOfCellsImpl.kt │ ├── BagOfCellsUtils.kt │ └── CachedBagOfCells.kt └── cell │ ├── Cell.kt │ ├── CellBuilder.kt │ ├── CellContext.kt │ ├── CellDescriptor.kt │ ├── CellSize.kt │ ├── CellSlice.kt │ ├── CellType.kt │ ├── DataCell.kt │ ├── EmptyCell.kt │ ├── LevelMask.kt │ ├── PrunedBranchCell.kt │ ├── VirtualCell.kt │ └── exception │ └── exceptions.kt └── test ├── BagOfCellsTest.kt ├── CellBuilderTest.kt ├── CellSliceTest.kt ├── CellTest.kt └── PrunedBranchTest.kt /.gitattributes: -------------------------------------------------------------------------------- 1 | # 2 | # https://help.github.com/articles/dealing-with-line-endings/ 3 | # 4 | # These are explicitly windows files and should use crlf 5 | *.bat text eol=crlf 6 | 7 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "gradle" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "daily" 12 | -------------------------------------------------------------------------------- /adnl/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinTonapiTl) 11 | implementation(libs.serialization.json) 12 | implementation(libs.atomicfu) 13 | implementation(libs.datetime) 14 | implementation(libs.coroutines.core) 15 | implementation(libs.ktor.utils) 16 | } 17 | } 18 | nativeMain { 19 | dependencies { 20 | implementation(libs.bignum) 21 | } 22 | } 23 | appleMain { 24 | dependencies { 25 | implementation(libs.ktor.network) 26 | } 27 | } 28 | jvmMain { 29 | dependencies { 30 | implementation(libs.ktor.network) 31 | } 32 | } 33 | linuxMain { 34 | dependencies { 35 | implementation(libs.ktor.network) 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /adnl/src/connection/AdnlClient.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.connection 2 | 3 | import kotlin.time.Duration 4 | 5 | public interface AdnlClient { 6 | public suspend fun sendQuery(data: ByteArray, timeout: Duration): ByteArray 7 | } 8 | -------------------------------------------------------------------------------- /adnl/src/connection/AdnlConnectionFactory.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.connection 2 | 3 | import org.ton.adnl.network.IPAddress 4 | import org.ton.adnl.network.TcpClient 5 | import org.ton.adnl.network.TcpClientImpl 6 | import org.ton.api.liteserver.LiteServerDesc 7 | 8 | public class AdnlConnectionFactory { 9 | public suspend fun connect( 10 | liteServerDesc: LiteServerDesc 11 | ): TcpClient { 12 | try { 13 | return TcpClientImpl().also { 14 | val address = IPAddress.ipv4(liteServerDesc.ip, liteServerDesc.port) 15 | it.connect(address.host, address.port) 16 | } 17 | } catch (cause: Throwable) { 18 | throw cause 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /adnl/src/connection/AdnlRequestData.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.connection 2 | 3 | import io.ktor.utils.io.core.* 4 | import kotlinx.coroutines.CompletableDeferred 5 | import kotlinx.coroutines.Job 6 | import kotlinx.datetime.Clock 7 | import kotlinx.datetime.Instant 8 | import org.ton.api.http.functions.HttpRequest 9 | import kotlin.coroutines.CoroutineContext 10 | 11 | public class AdnlRequestData( 12 | public val body: ByteArray, 13 | public val executionContext: Job 14 | ) 15 | 16 | public class AdnlResponseData( 17 | public val requestTime: Instant, 18 | public val body: ByteReadPacket, 19 | public val callContext: CoroutineContext 20 | ) { 21 | public val responseTime: Instant = Clock.System.now() 22 | } 23 | 24 | internal data class RequestTask( 25 | val request: HttpRequest, 26 | val response: CompletableDeferred, 27 | val context: CoroutineContext 28 | ) 29 | -------------------------------------------------------------------------------- /adnl/src/engine/AdnlNetworkEngine.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.engine 2 | 3 | import io.ktor.utils.io.core.* 4 | import org.ton.api.adnl.AdnlAddressUdp 5 | 6 | public interface AdnlNetworkEngine { 7 | public suspend fun sendDatagram(adnlAddress: AdnlAddressUdp, payload: ByteReadPacket) 8 | public suspend fun receiveDatagram(): Pair 9 | } 10 | -------------------------------------------------------------------------------- /adnl/src/exception/AdnlOldPacketSeqnoException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | public class AdnlOldPacketSeqnoException( 4 | public val packetSeqno: Long, 5 | public val localSeqno: Long 6 | ) : RuntimeException("Old packet seqno: $packetSeqno (current max $localSeqno)") 7 | -------------------------------------------------------------------------------- /adnl/src/exception/AdnlOldPeerReinitDateException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | import kotlinx.datetime.Instant 4 | 5 | public class AdnlOldPeerReinitDateException( 6 | public val reinitDate: Instant 7 | ) : RuntimeException("Too old remote peer reinit date: $reinitDate (${reinitDate.epochSeconds})") 8 | -------------------------------------------------------------------------------- /adnl/src/exception/AdnlTooNewAckSeqnoException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | public class AdnlTooNewAckSeqnoException( 4 | public val ackSeqno: Long, 5 | public val localSeqno: Long 6 | ) : RuntimeException("Too new ack seqno: $ackSeqno (current max sent $localSeqno)") 7 | -------------------------------------------------------------------------------- /adnl/src/exception/AdnlTooNewAddressListReinitDateException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | import kotlinx.datetime.Instant 4 | import org.ton.api.adnl.AdnlAddressList 5 | 6 | public class AdnlTooNewAddressListReinitDateException( 7 | public val reinitDate: Instant, 8 | public val addressList: AdnlAddressList 9 | ) : RuntimeException("Too new reinit date: $reinitDate (${reinitDate.epochSeconds}) in address list: $addressList") 10 | -------------------------------------------------------------------------------- /adnl/src/exception/AdnlTooNewLocalReinitDateException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | import kotlinx.datetime.Instant 4 | 5 | public class AdnlTooNewLocalReinitDateException( 6 | public val reinitDate: Instant 7 | ) : RuntimeException("Too new local peer reinit date: $reinitDate (${reinitDate.epochSeconds})") 8 | -------------------------------------------------------------------------------- /adnl/src/exception/AdnlTooNewRemoteReinitDateException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | import kotlinx.datetime.Instant 4 | 5 | public class AdnlTooNewRemoteReinitDateException( 6 | public val reinitDate: Instant 7 | ) : RuntimeException("Too new remote peer reinit date: $reinitDate (${reinitDate.epochSeconds})") 8 | -------------------------------------------------------------------------------- /adnl/src/exception/UnknownAdnlDestinationException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.exception 2 | 3 | import org.ton.api.adnl.AdnlIdShort 4 | 5 | public class UnknownAdnlDestinationException( 6 | public val destination: AdnlIdShort 7 | ) : RuntimeException("Unknown ADNL destination: $destination") 8 | -------------------------------------------------------------------------------- /adnl/src/network/TcpClient.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.network 2 | 3 | import io.ktor.utils.io.* 4 | import io.ktor.utils.io.core.* 5 | import kotlinx.coroutines.DisposableHandle 6 | 7 | public interface TcpClient : Closeable, DisposableHandle { 8 | public val input: ByteReadChannel 9 | public val output: ByteWriteChannel 10 | 11 | public suspend fun connect(host: String, port: Int) 12 | 13 | override fun dispose() { 14 | try { 15 | close() 16 | } catch (ignore: Throwable) { 17 | } 18 | } 19 | 20 | public fun close(cause: Throwable?) 21 | } 22 | -------------------------------------------------------------------------------- /adnl/src/network/TcpClientImpl.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.network 2 | 3 | public expect class TcpClientImpl() : TcpClient 4 | -------------------------------------------------------------------------------- /adnl/src/network/UdpServer.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.network 2 | 3 | import io.ktor.utils.io.core.* 4 | import kotlinx.coroutines.CoroutineScope 5 | import kotlin.coroutines.CoroutineContext 6 | 7 | public interface UdpServer : CoroutineScope { 8 | public suspend fun send(address: IPAddress, data: ByteReadPacket) 9 | 10 | public fun interface Callback { 11 | public fun receive(address: IPAddress, data: ByteReadPacket) 12 | } 13 | 14 | public companion object { 15 | public fun create( 16 | coroutineContext: CoroutineContext, 17 | port: Int, 18 | callback: Callback 19 | ): UdpServer { 20 | return UdpServerImpl(coroutineContext, port, callback) 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /adnl/src/network/UdpServerImpl.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.network 2 | 3 | import kotlin.coroutines.CoroutineContext 4 | 5 | internal expect class UdpServerImpl( 6 | coroutineContext: CoroutineContext, 7 | port: Int, 8 | callback: UdpServer.Callback 9 | ) : UdpServer { 10 | val port: Int 11 | } 12 | -------------------------------------------------------------------------------- /adnl/src/utils.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl 2 | 3 | internal fun ipv4(ipv4: Int): String = buildString { 4 | append((ipv4 shr 24) and 0xFF) 5 | append(".") 6 | append((ipv4 shr 16) and 0xFF) 7 | append(".") 8 | append((ipv4 shr 8) and 0xFF) 9 | append(".") 10 | append(ipv4 and 0xFF) 11 | } 12 | 13 | internal fun ipv4(host: String): Int { 14 | val bytes = host.split('.').reversed() 15 | require(bytes.size == 4) { "Invalid IPv4 address: $host" } 16 | var result = 0 17 | bytes.asReversed().forEach { byte -> 18 | result = (result shl 8) or (byte.toInt() and 0xFF) 19 | } 20 | return result 21 | } 22 | -------------------------------------------------------------------------------- /adnl/src@jvm/utils/ip.kt: -------------------------------------------------------------------------------- 1 | package org.ton.adnl.utils 2 | 3 | import io.ktor.network.sockets.* 4 | import org.ton.adnl.ipv4 5 | import org.ton.api.adnl.AdnlAddressUdp 6 | 7 | public fun AdnlAddressUdp.toSocketAddress(): InetSocketAddress = InetSocketAddress(ipv4(ip), port) 8 | public fun InetSocketAddress.toAdnlUdpAddress(): AdnlAddressUdp { 9 | return if (hostname.count { it == '.' } == 3) { 10 | AdnlAddressUdp(ipv4(hostname), port) 11 | } else { 12 | AdnlAddressUdp(0, port) 13 | } 14 | } 15 | 16 | public fun SocketAddress.toAdnlUdpAddress(): AdnlAddressUdp = (this as InetSocketAddress).toAdnlUdpAddress() 17 | -------------------------------------------------------------------------------- /bigint/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | implementation(libs.serialization.json) 11 | } 12 | } 13 | nativeMain { 14 | dependencies { 15 | implementation(libs.bignum) 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /bitstring/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinCrypto) 11 | implementation(libs.serialization.core) 12 | implementation(libs.kotlinx.io) 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /bitstring/src/FiftHexBitStringSerializer.kt: -------------------------------------------------------------------------------- 1 | package org.ton.bitstring 2 | 3 | import kotlinx.serialization.KSerializer 4 | import kotlinx.serialization.descriptors.PrimitiveKind 5 | import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor 6 | import kotlinx.serialization.descriptors.SerialDescriptor 7 | import kotlinx.serialization.encoding.Decoder 8 | import kotlinx.serialization.encoding.Encoder 9 | 10 | public object FiftHexBitStringSerializer : KSerializer { 11 | override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("BitString", PrimitiveKind.STRING) 12 | 13 | override fun deserialize(decoder: Decoder): BitString { 14 | val hex = decoder.decodeString() 15 | return BitString(hex) 16 | } 17 | 18 | override fun serialize(encoder: Encoder, value: BitString) { 19 | encoder.encodeString(value.toString()) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /bitstring/src/MutableBitString.kt: -------------------------------------------------------------------------------- 1 | package org.ton.bitstring 2 | 3 | import kotlin.jvm.JvmStatic 4 | 5 | public interface MutableBitString : BitString { 6 | public operator fun set(index: Int, bit: Int) 7 | public operator fun set(index: Int, element: Boolean): Boolean 8 | 9 | override operator fun plus(bits: BooleanArray): MutableBitString 10 | override operator fun plus(bits: Iterable): MutableBitString 11 | override operator fun plus(bits: Collection): MutableBitString 12 | override operator fun plus(bytes: ByteArray): MutableBitString 13 | override operator fun plus(bit: Boolean): MutableBitString 14 | override fun plus(bytes: ByteArray, bits: Int): MutableBitString 15 | 16 | public companion object { 17 | @JvmStatic 18 | public fun of(size: Int): MutableBitString = ByteBackedMutableBitString.of(size) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /bitstring/src/exception/exceptions.kt: -------------------------------------------------------------------------------- 1 | package org.ton.bitstring.exception 2 | 3 | public class BitStringOverflowException( 4 | override val message: String? = "BitString overflow" 5 | ) : RuntimeException() 6 | 7 | public class BitStringUnderflowException : RuntimeException("BitString underflow") 8 | -------------------------------------------------------------------------------- /block-tlb/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinHashmapTlb) 11 | api(projects.tonKotlinTlb) 12 | api(projects.tonKotlinTl) 13 | implementation(libs.serialization.core) 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /block-tlb/src/AccountState.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.TlbObject 8 | import org.ton.tlb.providers.TlbCombinatorProvider 9 | 10 | 11 | @JsonClassDiscriminator("@type") 12 | public sealed interface AccountState : TlbObject { 13 | /** 14 | * Account status. 15 | */ 16 | public val status: AccountStatus 17 | 18 | public companion object : TlbCombinatorProvider by AccountStateTlbCombinator 19 | } 20 | 21 | private object AccountStateTlbCombinator : TlbCombinator( 22 | AccountState::class, 23 | AccountUninit::class to AccountUninit, 24 | AccountActive::class to AccountActive, 25 | AccountFrozen::class to AccountFrozen 26 | ) 27 | -------------------------------------------------------------------------------- /block-tlb/src/AccountUninit.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | import org.ton.tlb.TlbPrettyPrinter 8 | import org.ton.tlb.providers.TlbConstructorProvider 9 | 10 | 11 | @SerialName("account_uninit") 12 | public object AccountUninit : AccountState, TlbConstructorProvider by AccountUninitTlbConstructor { 13 | override val status: AccountStatus get() = AccountStatus.UNINIT 14 | 15 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter { 16 | return printer.type("account_uninit") 17 | } 18 | 19 | override fun toString(): String = print().toString() 20 | } 21 | 22 | private object AccountUninitTlbConstructor : TlbConstructor( 23 | schema = "account_uninit\$00 = AccountState;" 24 | ) { 25 | override fun storeTlb(cellBuilder: CellBuilder, value: AccountUninit) { 26 | } 27 | 28 | override fun loadTlb(cellSlice: CellSlice): AccountUninit { 29 | return AccountUninit 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /block-tlb/src/ActionChangeLibrary.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | 5 | 6 | @SerialName("action_change_library") 7 | public data class ActionChangeLibrary( 8 | val mode: Int, 9 | @Suppress("SpellCheckingInspection") 10 | val libref: LibRef 11 | ) : OutAction { 12 | override fun toString(): String = buildString { 13 | append("(action_change_library\n") 14 | append("mode:") 15 | append(mode) 16 | append(" libref:") 17 | append(libref) 18 | append(")") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /block-tlb/src/ActionReserveCurrency.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | 5 | 6 | @SerialName("action_reserve_currency") 7 | public data class ActionReserveCurrency( 8 | val mode: Int, 9 | val currency: CurrencyCollection 10 | ) : OutAction 11 | -------------------------------------------------------------------------------- /block-tlb/src/ActionSendMsg.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.Cell 5 | 6 | 7 | @SerialName("action_send_msg") 8 | public data class ActionSendMsg( 9 | val mode: Int, 10 | val outMsg: MessageRelaxed 11 | ) : OutAction 12 | -------------------------------------------------------------------------------- /block-tlb/src/ActionSetCode.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.Cell 5 | 6 | 7 | @SerialName("action_set_code") 8 | public data class ActionSetCode( 9 | val newCode: Cell 10 | ) : OutAction 11 | -------------------------------------------------------------------------------- /block-tlb/src/AddrNone.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.kotlin.cell.CellSize 7 | import org.ton.tlb.TlbConstructor 8 | import org.ton.tlb.TlbPrettyPrinter 9 | import org.ton.tlb.providers.TlbConstructorProvider 10 | 11 | @SerialName("addr_none") 12 | 13 | public object AddrNone : MsgAddressExt, TlbConstructorProvider by AddrNoneTlbConstructor { 14 | override val cellSize: CellSize = CellSize(2, 0) 15 | 16 | override fun toString(): String = print().toString() 17 | 18 | override fun print(tlbPrettyPrinter: TlbPrettyPrinter): TlbPrettyPrinter = tlbPrettyPrinter { 19 | type("addr_none") 20 | } 21 | } 22 | 23 | private object AddrNoneTlbConstructor : TlbConstructor( 24 | schema = "addr_none\$00 = MsgAddressExt;" 25 | ) { 26 | override fun storeTlb( 27 | cellBuilder: CellBuilder, 28 | value: AddrNone 29 | ) { 30 | } 31 | 32 | override fun loadTlb(cellSlice: CellSlice): AddrNone { 33 | return AddrNone 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /block-tlb/src/BinTree.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCodec 7 | import org.ton.tlb.TlbCombinator 8 | import org.ton.tlb.TlbObject 9 | import kotlin.jvm.JvmStatic 10 | 11 | 12 | @JsonClassDiscriminator("@type") 13 | public sealed interface BinTree : Iterable, TlbObject { 14 | 15 | override fun iterator(): Iterator = nodes().iterator() 16 | public fun nodes(): Sequence 17 | 18 | public companion object { 19 | @Suppress("UNCHECKED_CAST") 20 | @JvmStatic 21 | public fun tlbCodec( 22 | x: TlbCodec 23 | ): TlbCombinator> = BinTreeTlbCombinator(x) as TlbCombinator> 24 | } 25 | } 26 | 27 | private class BinTreeTlbCombinator( 28 | x: TlbCodec<*> 29 | ) : TlbCombinator>( 30 | BinTree::class, 31 | BinTreeLeaf::class to BinTreeLeaf.tlbCodec(x), 32 | BinTreeFork::class to BinTreeFork.tlbCodec(x) 33 | ) 34 | -------------------------------------------------------------------------------- /block-tlb/src/BlkMasterInfo.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.cell.invoke 7 | import org.ton.tlb.* 8 | import org.ton.tlb.TlbConstructor 9 | 10 | @SerialName("master_info") 11 | 12 | public data class BlkMasterInfo( 13 | val master: ExtBlkRef // master : ExtBlkRef 14 | ) : TlbObject { 15 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter = printer.type("master_info") { 16 | field("master", master) 17 | } 18 | 19 | override fun toString(): String = print().toString() 20 | 21 | public companion object : TlbCodec by BlkMasterInfoTlbConstructor 22 | } 23 | 24 | private object BlkMasterInfoTlbConstructor : TlbConstructor( 25 | schema = "master_info\$_ master:ExtBlkRef = BlkMasterInfo;" 26 | ) { 27 | override fun storeTlb( 28 | cellBuilder: CellBuilder, 29 | value: BlkMasterInfo 30 | ) = cellBuilder { 31 | storeTlb(ExtBlkRef, value.master) 32 | } 33 | 34 | override fun loadTlb( 35 | cellSlice: CellSlice 36 | ): BlkMasterInfo = cellSlice { 37 | val master = loadTlb(ExtBlkRef) 38 | BlkMasterInfo(master) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /block-tlb/src/BlkPrevInfo.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCodec 7 | import org.ton.tlb.TlbObject 8 | import kotlin.jvm.JvmStatic 9 | 10 | @JsonClassDiscriminator("@type") 11 | 12 | public sealed interface BlkPrevInfo : TlbObject { 13 | public companion object { 14 | @Suppress("UNCHECKED_CAST") 15 | @JvmStatic 16 | public fun tlbCodec(multiple: Boolean): TlbCodec = 17 | (if (multiple) PrevBlksInfo else PrevBlkInfo) as TlbCodec 18 | 19 | @JvmStatic 20 | public fun tlbCodec(multiple: Int): TlbCodec = 21 | tlbCodec(multiple != 0) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /block-tlb/src/BlockCreateStats.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.TlbObject 8 | import org.ton.tlb.providers.TlbCombinatorProvider 9 | 10 | 11 | @JsonClassDiscriminator("@type") 12 | public sealed interface BlockCreateStats : TlbObject { 13 | public companion object : TlbCombinatorProvider by BlockCreateStatsTlbCombinator 14 | } 15 | 16 | private object BlockCreateStatsTlbCombinator : TlbCombinator( 17 | BlockCreateStats::class, 18 | BlockCreateStatsRegular::class to BlockCreateStatsRegular.tlbConstructor(), 19 | BlockCreateStatsExt::class to BlockCreateStatsExt.tlbConstructor() 20 | ) 21 | -------------------------------------------------------------------------------- /block-tlb/src/CertificateEnv.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | 5 | 6 | @SerialName("certificate_env") 7 | public data class CertificateEnv( 8 | val certificate: Certificate 9 | ) 10 | -------------------------------------------------------------------------------- /block-tlb/src/ChunkRef.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.cell.loadRef 6 | import org.ton.cell.storeRef 7 | import org.ton.tlb.TlbConstructor 8 | import org.ton.tlb.loadTlb 9 | import org.ton.tlb.storeTlb 10 | 11 | public data class ChunkRef( 12 | val ref: TextChunks 13 | ) : TextChunkRef { 14 | public companion object { 15 | public fun tlbConstructor(n: Int): TlbConstructor = ChunkRefTlbConstructor(n) 16 | } 17 | } 18 | 19 | private class ChunkRefTlbConstructor( 20 | n: Int 21 | ) : TlbConstructor( 22 | schema = "chunk_ref\$_ {n:#} ref:^(TextChunks (n + 1)) = TextChunkRef (n + 1);\n" 23 | ) { 24 | val n = n - 1 25 | 26 | override fun storeTlb(cellBuilder: CellBuilder, value: ChunkRef) { 27 | cellBuilder.storeRef { 28 | storeTlb(TextChunks.tlbCodec(n + 1), value.ref) 29 | } 30 | } 31 | 32 | override fun loadTlb(cellSlice: CellSlice): ChunkRef { 33 | val ref = cellSlice.loadRef { 34 | loadTlb(TextChunks.tlbCodec(n + 1)) 35 | } 36 | return ChunkRef(ref) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /block-tlb/src/ChunkRefEmpty.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.tlb.TlbConstructor 6 | import org.ton.tlb.providers.TlbConstructorProvider 7 | 8 | public object ChunkRefEmpty : TextChunkRef, TlbConstructorProvider by ChunkRefEmptyTlbConstructor 9 | 10 | private object ChunkRefEmptyTlbConstructor : TlbConstructor( 11 | schema = "chunk_ref_empty\$_ = TextChunkRef 0;" 12 | ) { 13 | override fun storeTlb(cellBuilder: CellBuilder, value: ChunkRefEmpty) { 14 | } 15 | 16 | override fun loadTlb(cellSlice: CellSlice): ChunkRefEmpty = ChunkRefEmpty 17 | } 18 | -------------------------------------------------------------------------------- /block-tlb/src/CommonMsgInfo.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCodec 7 | import org.ton.tlb.TlbCombinator 8 | import org.ton.tlb.TlbObject 9 | import kotlin.jvm.JvmStatic 10 | 11 | @JsonClassDiscriminator("@type") 12 | 13 | public sealed interface CommonMsgInfo : TlbObject { 14 | public companion object : TlbCodec by CommonMsgInfoTlbCombinator { 15 | @JvmStatic 16 | public fun tlbCodec(): TlbCombinator = CommonMsgInfoTlbCombinator 17 | } 18 | } 19 | 20 | private object CommonMsgInfoTlbCombinator : TlbCombinator( 21 | CommonMsgInfo::class, 22 | IntMsgInfo::class to IntMsgInfo, 23 | ExtInMsgInfo::class to ExtInMsgInfo, 24 | ExtOutMsgInfo::class to ExtOutMsgInfo 25 | ) 26 | 27 | public val CommonMsgInfo?.value: CurrencyCollection get() = (this as? IntMsgInfo)?.value ?: CurrencyCollection.ZERO 28 | -------------------------------------------------------------------------------- /block-tlb/src/CryptoSignature.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.providers.TlbCombinatorProvider 8 | 9 | 10 | @JsonClassDiscriminator("@type") 11 | public sealed interface CryptoSignature { 12 | public companion object : TlbCombinatorProvider by CryptoSignatureTlbCombinator 13 | } 14 | 15 | private object CryptoSignatureTlbCombinator : TlbCombinator( 16 | CryptoSignature::class, 17 | CryptoSignatureSimple::class to CryptoSignatureSimple, 18 | ChainedSignature::class to ChainedSignature 19 | ) 20 | -------------------------------------------------------------------------------- /block-tlb/src/CryptoSignatureSimple.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.bitstring.BitString 5 | import org.ton.cell.CellBuilder 6 | import org.ton.cell.CellSlice 7 | import org.ton.cell.invoke 8 | import org.ton.tlb.TlbConstructor 9 | import org.ton.tlb.providers.TlbConstructorProvider 10 | 11 | 12 | @SerialName("ed25519_signature") 13 | public data class CryptoSignatureSimple( 14 | val r: BitString, 15 | val s: BitString 16 | ) : CryptoSignature { 17 | public companion object : TlbConstructorProvider by CryptoSignatureSimpleTlbConstructor 18 | } 19 | 20 | private object CryptoSignatureSimpleTlbConstructor : TlbConstructor( 21 | schema = "ed25519_signature#5 R:bits256 s:bits256 = CryptoSignatureSimple;" 22 | ) { 23 | override fun storeTlb( 24 | cellBuilder: CellBuilder, 25 | value: CryptoSignatureSimple 26 | ) = cellBuilder { 27 | storeBits(value.r) 28 | storeBits(value.s) 29 | } 30 | 31 | override fun loadTlb( 32 | cellSlice: CellSlice 33 | ): CryptoSignatureSimple = cellSlice { 34 | val r = loadBits(256) 35 | val s = loadBits(256) 36 | CryptoSignatureSimple(r, s) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /block-tlb/src/DnsNextResolver.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | import org.ton.tlb.loadTlb 8 | import org.ton.tlb.providers.TlbConstructorProvider 9 | import org.ton.tlb.storeTlb 10 | 11 | @SerialName("dns_next_resolver") 12 | 13 | public data class DnsNextResolver( 14 | val resolver: MsgAddressInt 15 | ) : DnsRecord { 16 | public companion object : TlbConstructorProvider by DNSNextResolverTlbConstructor 17 | } 18 | 19 | private object DNSNextResolverTlbConstructor : TlbConstructor( 20 | schema = "dns_next_resolver#ba93 resolver:MsgAddressInt = DNSNextResolver;" 21 | ) { 22 | override fun storeTlb( 23 | cellBuilder: CellBuilder, 24 | value: DnsNextResolver 25 | ) { 26 | cellBuilder.storeTlb(MsgAddressInt, value.resolver) 27 | } 28 | 29 | override fun loadTlb( 30 | cellSlice: CellSlice 31 | ): DnsNextResolver { 32 | val resolver = cellSlice.loadTlb(MsgAddressInt) 33 | return DnsNextResolver(resolver) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /block-tlb/src/DnsRecord.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.tlb.TlbCombinator 4 | import org.ton.tlb.providers.TlbCombinatorProvider 5 | 6 | public sealed interface DnsRecord { 7 | public companion object : TlbCombinatorProvider by DnsRecordTlbCombinator 8 | } 9 | 10 | private object DnsRecordTlbCombinator : TlbCombinator( 11 | DnsRecord::class, 12 | DnsNextResolver::class to DnsNextResolver, 13 | DnsSmcAddress::class to DnsSmcAddress, 14 | DnsAdnlAddress::class to DnsAdnlAddress, 15 | DnsText::class to DnsText, 16 | ) 17 | -------------------------------------------------------------------------------- /block-tlb/src/DnsText.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | import org.ton.tlb.loadTlb 8 | import org.ton.tlb.providers.TlbConstructorProvider 9 | import org.ton.tlb.storeTlb 10 | 11 | 12 | public class DnsText( 13 | @SerialName("_") 14 | public val value: Text 15 | ) : DnsRecord { 16 | public companion object : TlbConstructorProvider by DnsTextTlbConstructor 17 | } 18 | 19 | private object DnsTextTlbConstructor : TlbConstructor( 20 | schema = "dns_text#1eda _:Text = DNSRecord;" 21 | ) { 22 | override fun storeTlb(cellBuilder: CellBuilder, value: DnsText) { 23 | cellBuilder.storeTlb(Text, value.value) 24 | } 25 | 26 | override fun loadTlb(cellSlice: CellSlice): DnsText { 27 | val value = cellSlice.loadTlb(Text) 28 | return DnsText(value) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /block-tlb/src/FutureSplitMergeMerge.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | -------------------------------------------------------------------------------- /block-tlb/src/FutureSplitMergeNone.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | -------------------------------------------------------------------------------- /block-tlb/src/FutureSplitMergeSplit.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | -------------------------------------------------------------------------------- /block-tlb/src/IhrPendingSince.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.cell.invoke 7 | import org.ton.tlb.TlbConstructor 8 | import org.ton.tlb.providers.TlbConstructorProvider 9 | 10 | 11 | @SerialName("ihr_pending") 12 | public data class IhrPendingSince( 13 | val import_lt: ULong 14 | ) { 15 | public companion object : TlbConstructorProvider by IhrPendingSinceTlbConstructor 16 | } 17 | 18 | private object IhrPendingSinceTlbConstructor : TlbConstructor( 19 | schema = "ihr_pending\$_ import_lt:uint64 = IhrPendingSince;" 20 | ) { 21 | override fun storeTlb( 22 | cellBuilder: CellBuilder, 23 | value: IhrPendingSince 24 | ) = cellBuilder { 25 | storeUInt64(value.import_lt) 26 | } 27 | 28 | override fun loadTlb( 29 | cellSlice: CellSlice 30 | ): IhrPendingSince = cellSlice { 31 | val importLt = loadUInt64() 32 | IhrPendingSince(importLt) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /block-tlb/src/InMsg.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.TlbObject 8 | import org.ton.tlb.providers.TlbCombinatorProvider 9 | 10 | @JsonClassDiscriminator("@type") 11 | 12 | public sealed interface InMsg : TlbObject { 13 | public companion object : TlbCombinatorProvider by InMsgTlbCombinator 14 | } 15 | 16 | private object InMsgTlbCombinator : TlbCombinator( 17 | InMsg::class, 18 | MsgImportExt::class to MsgImportExt, 19 | MsgImportIhr::class to MsgImportIhr, 20 | MsgImportImm::class to MsgImportImm, 21 | MsgImportFin::class to MsgImportFin, 22 | MsgImportTr::class to MsgImportTr, 23 | MsgDiscardFin::class to MsgDiscardFin, 24 | MsgDiscardTr::class to MsgDiscardTr, 25 | ) 26 | -------------------------------------------------------------------------------- /block-tlb/src/IntermediateAddress.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.TlbObject 8 | import org.ton.tlb.providers.TlbCombinatorProvider 9 | 10 | 11 | @JsonClassDiscriminator("@type") 12 | public sealed interface IntermediateAddress : TlbObject { 13 | public companion object : TlbCombinatorProvider by IntermediateAddressTlbCombinator 14 | } 15 | 16 | private object IntermediateAddressTlbCombinator : TlbCombinator( 17 | IntermediateAddress::class, 18 | IntermediateAddressExt::class to IntermediateAddressExt, 19 | IntermediateAddressRegular::class to IntermediateAddressRegular, 20 | IntermediateAddressSimple::class to IntermediateAddressSimple, 21 | ) 22 | -------------------------------------------------------------------------------- /block-tlb/src/LibRef.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | 7 | 8 | @JsonClassDiscriminator("@type") 9 | public sealed interface LibRef 10 | -------------------------------------------------------------------------------- /block-tlb/src/LibRefHash.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.bitstring.BitString 5 | 6 | 7 | @SerialName("libref_hash") 8 | public data class LibRefHash( 9 | val lib_hash: BitString 10 | ) : LibRef { 11 | init { 12 | require(lib_hash.size == 256) { "required: lib_hash.size == 256, actual: ${lib_hash.size}" } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /block-tlb/src/LibRefRef.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.Cell 5 | 6 | 7 | @SerialName("libref_ref") 8 | public data class LibRefRef( 9 | val library: Cell 10 | ) : LibRef 11 | -------------------------------------------------------------------------------- /block-tlb/src/MerkleProof.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.bitstring.BitString 5 | import org.ton.cell.Cell 6 | import org.ton.cell.CellType 7 | import org.ton.tlb.CellRef 8 | 9 | 10 | @SerialName("merkle_proof") 11 | public data class MerkleProof( 12 | val virtualHash: BitString, 13 | val depth: Int, 14 | val virtualRoot: CellRef 15 | ) { 16 | public companion object { 17 | public fun virtualize(cell: Cell, offset: Int = 1): Cell { 18 | require(cell.type == CellType.MERKLE_PROOF) { 19 | "Invalid cell type, expected: ${CellType.MERKLE_PROOF}, actual: ${cell.type}" 20 | } 21 | return cell.refs.first().virtualize(offset) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /block-tlb/src/MsgAddress.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.kotlin.cell.CellSizeable 4 | import org.ton.tlb.TlbCombinator 5 | import org.ton.tlb.TlbObject 6 | import org.ton.tlb.providers.TlbCombinatorProvider 7 | 8 | 9 | public sealed interface MsgAddress : TlbObject, CellSizeable { 10 | public companion object : TlbCombinatorProvider by MsgAddressTlbCombinator 11 | } 12 | 13 | private object MsgAddressTlbCombinator : TlbCombinator( 14 | MsgAddress::class, 15 | MsgAddressInt::class to MsgAddressInt.tlbCodec(), 16 | MsgAddressExt::class to MsgAddressExt.tlbCodec(), 17 | ) 18 | -------------------------------------------------------------------------------- /block-tlb/src/OldMcBlocksInfo.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.hashmap.HashmapAugE 6 | import org.ton.kotlin.cell.CellContext 7 | import org.ton.tlb.TlbCodec 8 | import org.ton.tlb.TlbObject 9 | import org.ton.tlb.TlbPrettyPrinter 10 | import kotlin.jvm.JvmInline 11 | 12 | 13 | @JvmInline 14 | public value class OldMcBlocksInfo( 15 | public val value: HashmapAugE 16 | ) : TlbObject { 17 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter { 18 | return value.print(printer) 19 | } 20 | 21 | override fun toString(): String = print().toString() 22 | 23 | public companion object : TlbCodec by OldMcBlocksInfoTlbCodec 24 | } 25 | 26 | private object OldMcBlocksInfoTlbCodec : TlbCodec { 27 | private val codec = HashmapAugE.tlbCodec(32, KeyExtBlkRef, KeyMaxLt) 28 | 29 | override fun storeTlb(cellBuilder: CellBuilder, value: OldMcBlocksInfo, context: CellContext) { 30 | codec.storeTlb(cellBuilder, value.value, context) 31 | } 32 | 33 | override fun loadTlb(cellSlice: CellSlice, context: CellContext): OldMcBlocksInfo { 34 | return OldMcBlocksInfo(codec.loadTlb(cellSlice, context)) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /block-tlb/src/OutAction.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | 7 | 8 | @JsonClassDiscriminator("@type") 9 | public sealed interface OutAction 10 | -------------------------------------------------------------------------------- /block-tlb/src/OutList.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | 7 | 8 | @JsonClassDiscriminator("@type") 9 | public sealed interface OutList 10 | -------------------------------------------------------------------------------- /block-tlb/src/OutListEmpty.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | 5 | 6 | @SerialName("out_list_empty") 7 | public object OutListEmpty : OutList 8 | -------------------------------------------------------------------------------- /block-tlb/src/OutListLink.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | 5 | 6 | @SerialName("out_list") 7 | public data class OutListLink( 8 | val prev: OutList, 9 | val action: OutAction 10 | ) : OutList 11 | -------------------------------------------------------------------------------- /block-tlb/src/OutListNode.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.Cell 5 | 6 | 7 | @SerialName("out_list_node") 8 | public data class OutListNode( 9 | val prev: Cell, 10 | val action: OutAction 11 | ) 12 | -------------------------------------------------------------------------------- /block-tlb/src/OutMsg.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.TlbObject 8 | import org.ton.tlb.providers.TlbCombinatorProvider 9 | 10 | 11 | @JsonClassDiscriminator("@type") 12 | public sealed interface OutMsg : TlbObject { 13 | public companion object : TlbCombinatorProvider by OutMsgTlbCombinator 14 | } 15 | 16 | private object OutMsgTlbCombinator : TlbCombinator( 17 | OutMsg::class, 18 | MsgExportExt::class to MsgExportExt, 19 | MsgExportImm::class to MsgExportImm, 20 | MsgExportNew::class to MsgExportNew, 21 | MsgExportTr::class to MsgExportTr, 22 | MsgExportDeq::class to MsgExportDeq, 23 | MsgExportDeqShort::class to MsgExportDeqShort, 24 | MsgExportTrReq::class to MsgExportTrReq, 25 | MsgExportDeqImm::class to MsgExportDeqImm, 26 | ) 27 | -------------------------------------------------------------------------------- /block-tlb/src/ProtoHttp.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | import org.ton.tlb.providers.TlbConstructorProvider 8 | 9 | @SerialName("proto_http") 10 | 11 | public object ProtoHttp : Protocol, TlbConstructorProvider by ProtoHttpTlbConstructor 12 | 13 | private object ProtoHttpTlbConstructor : TlbConstructor( 14 | schema = "proto_http#4854 = Protocol;" 15 | ) { 16 | override fun storeTlb( 17 | cellBuilder: CellBuilder, 18 | value: ProtoHttp 19 | ) { 20 | } 21 | 22 | override fun loadTlb( 23 | cellSlice: CellSlice 24 | ): ProtoHttp = ProtoHttp 25 | } 26 | -------------------------------------------------------------------------------- /block-tlb/src/ProtoList.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.tlb.providers.TlbCombinatorProvider 4 | 5 | public sealed interface ProtoList : Iterable { 6 | public companion object : TlbCombinatorProvider by ProtoListTlbCombinator 7 | } 8 | 9 | private object ProtoListTlbCombinator : org.ton.tlb.TlbCombinator( 10 | ProtoList::class, 11 | ProtoListNil::class to ProtoListNil.tlbConstructor(), 12 | ProtoListNext::class to ProtoListNext.tlbConstructor(), 13 | ) 14 | -------------------------------------------------------------------------------- /block-tlb/src/ProtoListNext.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.tlb.loadTlb 5 | import org.ton.tlb.providers.TlbConstructorProvider 6 | import org.ton.tlb.storeTlb 7 | 8 | @SerialName("proto_list_next") 9 | 10 | public data class ProtoListNext( 11 | val head: Protocol, 12 | val tail: ProtoList 13 | ) : ProtoList { 14 | override fun iterator(): Iterator = iterator { 15 | yield(head) 16 | yieldAll(tail) 17 | } 18 | 19 | public companion object : TlbConstructorProvider by ProtoListNextTlbConstructor 20 | } 21 | 22 | private object ProtoListNextTlbConstructor : org.ton.tlb.TlbConstructor( 23 | schema = "proto_list_next#1 head:Protocol tail:ProtoList = ProtoList;" 24 | ) { 25 | override fun storeTlb( 26 | cellBuilder: org.ton.cell.CellBuilder, 27 | value: ProtoListNext 28 | ) { 29 | cellBuilder.storeTlb(Protocol, value.head) 30 | cellBuilder.storeTlb(ProtoList, value.tail) 31 | } 32 | 33 | override fun loadTlb( 34 | cellSlice: org.ton.cell.CellSlice 35 | ): ProtoListNext { 36 | val head = cellSlice.loadTlb(Protocol) 37 | val tail = cellSlice.loadTlb(ProtoList) 38 | return ProtoListNext(head, tail) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /block-tlb/src/ProtoListNil.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.providers.TlbConstructorProvider 7 | 8 | @SerialName("proto_list_nil") 9 | 10 | public object ProtoListNil : ProtoList, TlbConstructorProvider by ProtoListNilTlbConstructor { 11 | override fun iterator(): Iterator = iterator {} 12 | } 13 | 14 | private object ProtoListNilTlbConstructor : org.ton.tlb.TlbConstructor( 15 | schema = "proto_list_nil#0 = ProtoList;" 16 | ) { 17 | override fun storeTlb( 18 | cellBuilder: CellBuilder, 19 | value: ProtoListNil 20 | ) { 21 | } 22 | 23 | override fun loadTlb( 24 | cellSlice: CellSlice 25 | ): ProtoListNil = ProtoListNil 26 | } 27 | -------------------------------------------------------------------------------- /block-tlb/src/Protocol.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.tlb.TlbCombinator 4 | import org.ton.tlb.providers.TlbCombinatorProvider 5 | 6 | public sealed interface Protocol { 7 | public companion object : TlbCombinatorProvider by ProtocolTlbCombinator 8 | } 9 | 10 | private object ProtocolTlbCombinator : TlbCombinator( 11 | Protocol::class, 12 | ProtoHttp::class to ProtoHttp.tlbConstructor(), 13 | ) { 14 | } 15 | -------------------------------------------------------------------------------- /block-tlb/src/ShardAccounts.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.hashmap.HashmapAugE 6 | import org.ton.kotlin.account.ShardAccount 7 | import org.ton.kotlin.cell.CellContext 8 | import org.ton.tlb.TlbCodec 9 | import org.ton.tlb.TlbObject 10 | import org.ton.tlb.TlbPrettyPrinter 11 | import kotlin.jvm.JvmInline 12 | 13 | 14 | @JvmInline 15 | public value class ShardAccounts( 16 | public val x: HashmapAugE 17 | ) : TlbObject { 18 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter { 19 | return x.print(printer) 20 | } 21 | 22 | override fun toString(): String = print().toString() 23 | 24 | public companion object : TlbCodec by ShardAccountsTlbCodec 25 | } 26 | 27 | private object ShardAccountsTlbCodec : TlbCodec { 28 | private val codec = HashmapAugE.tlbCodec(256, ShardAccount, DepthBalanceInfo) 29 | 30 | override fun storeTlb(cellBuilder: CellBuilder, value: ShardAccounts, context: CellContext) { 31 | codec.storeTlb(cellBuilder, value.x, context) 32 | } 33 | 34 | override fun loadTlb(cellSlice: CellSlice, context: CellContext): ShardAccounts { 35 | return ShardAccounts(codec.loadTlb(cellSlice, context)) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /block-tlb/src/ShardHashes.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.hashmap.HashMapE 6 | import org.ton.kotlin.cell.CellContext 7 | import org.ton.tlb.CellRef 8 | import org.ton.tlb.TlbCodec 9 | import org.ton.tlb.TlbObject 10 | import org.ton.tlb.TlbPrettyPrinter 11 | import kotlin.jvm.JvmInline 12 | 13 | 14 | @JvmInline 15 | public value class ShardHashes( 16 | public val value: HashMapE>> 17 | ) : TlbObject { 18 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter { 19 | return value.print(printer) 20 | } 21 | 22 | override fun toString(): String = print().toString() 23 | 24 | public companion object : TlbCodec by ShardHashesTlbCodec 25 | } 26 | 27 | private object ShardHashesTlbCodec : TlbCodec { 28 | private val codec = HashMapE.tlbCodec(32, CellRef.tlbCodec(BinTree.tlbCodec(ShardDescr))) 29 | override fun storeTlb(cellBuilder: CellBuilder, value: ShardHashes, context: CellContext) { 30 | codec.storeTlb(cellBuilder, value.value, context) 31 | } 32 | 33 | override fun loadTlb(cellSlice: CellSlice, context: CellContext): ShardHashes { 34 | return ShardHashes(codec.loadTlb(cellSlice, context)) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /block-tlb/src/ShardState.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCombinator 7 | import org.ton.tlb.TlbObject 8 | import org.ton.tlb.providers.TlbCombinatorProvider 9 | 10 | 11 | @JsonClassDiscriminator("@type") 12 | public sealed interface ShardState : TlbObject { 13 | public companion object : TlbCombinatorProvider by ShardStateTlbCombinator 14 | } 15 | 16 | private object ShardStateTlbCombinator : TlbCombinator( 17 | ShardState::class, 18 | SplitState::class to SplitState.tlbConstructor(), 19 | ShardStateUnsplit::class to ShardStateUnsplit.tlbConstructor(), 20 | ) 21 | -------------------------------------------------------------------------------- /block-tlb/src/SigPubKey.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.bitstring.BitString 5 | import org.ton.cell.CellBuilder 6 | import org.ton.cell.CellSlice 7 | import org.ton.cell.invoke 8 | import org.ton.tlb.TlbConstructor 9 | import org.ton.tlb.providers.TlbConstructorProvider 10 | 11 | 12 | @SerialName("ed25519_pubkey") 13 | public data class SigPubKey( 14 | val pubkey: BitString 15 | ) { 16 | init { 17 | require(pubkey.size == 256) { "required: pubkey.size == 256, actual: ${pubkey.size}" } 18 | } 19 | 20 | public companion object : TlbConstructorProvider by SigPubKeyTlbConstructor 21 | } 22 | 23 | private object SigPubKeyTlbConstructor : TlbConstructor( 24 | schema = "ed25519_pubkey#8e81278a pubkey:bits256 = SigPubKey;" 25 | ) { 26 | override fun storeTlb( 27 | cellBuilder: CellBuilder, 28 | value: SigPubKey 29 | ) = cellBuilder { 30 | storeBits(value.pubkey) 31 | } 32 | 33 | override fun loadTlb( 34 | cellSlice: CellSlice 35 | ): SigPubKey = cellSlice { 36 | val pubkey = loadBits(256) 37 | SigPubKey(pubkey) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /block-tlb/src/SmartContractInfo.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.bitstring.BitString 5 | 6 | 7 | @SerialName("smc_info") 8 | public data class SmartContractInfo( 9 | val actions: Int, 10 | val msgs_sent: Int, 11 | val unixtime: Long, 12 | val block_lt: Long, 13 | val trans_lt: Long, 14 | val rand_seed: BitString, 15 | val balance_remaining: CurrencyCollection, 16 | val myself: MsgAddressInt 17 | ) { 18 | init { 19 | require(rand_seed.size == 256) { "required: rand_seed.size == 256, actual: ${rand_seed.size}" } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /block-tlb/src/Text.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.tlb.TlbConstructor 6 | import org.ton.tlb.loadTlb 7 | import org.ton.tlb.providers.TlbConstructorProvider 8 | import org.ton.tlb.storeTlb 9 | 10 | 11 | public data class Text( 12 | val chunks: UByte, 13 | val rest: TextChunks 14 | ) { 15 | public companion object : TlbConstructorProvider by TextTlbConstructor 16 | } 17 | 18 | private object TextTlbConstructor : TlbConstructor( 19 | schema = "text\$_ chunks:(## 8) rest:(TextChunks chunks) = Text;" 20 | ) { 21 | override fun storeTlb(cellBuilder: CellBuilder, value: Text) { 22 | cellBuilder.storeUInt8(value.chunks) 23 | cellBuilder.storeTlb(TextChunks.tlbCodec(value.chunks.toInt()), value.rest) 24 | } 25 | 26 | override fun loadTlb(cellSlice: CellSlice): Text { 27 | val chunks = cellSlice.loadUInt8() 28 | val rest = cellSlice.loadTlb(TextChunks.tlbCodec(chunks.toInt())) 29 | return Text(chunks, rest) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /block-tlb/src/TextChunkEmpty.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.tlb.TlbConstructor 6 | import org.ton.tlb.providers.TlbConstructorProvider 7 | 8 | public object TextChunkEmpty : TextChunks, TlbConstructorProvider by TextChunkEmptyTlbConstructor 9 | 10 | private object TextChunkEmptyTlbConstructor : TlbConstructor( 11 | schema = "chunk_empty\$_ = TextChunks 0;" 12 | ) { 13 | override fun storeTlb(cellBuilder: CellBuilder, value: TextChunkEmpty) { 14 | } 15 | 16 | override fun loadTlb(cellSlice: CellSlice): TextChunkEmpty = TextChunkEmpty 17 | } 18 | -------------------------------------------------------------------------------- /block-tlb/src/TextChunkRef.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.tlb.TlbCombinator 4 | 5 | public sealed interface TextChunkRef { 6 | public companion object { 7 | public fun tlbCombinator(n: Int): TlbCombinator = if (n == 0) { 8 | ChunkRefEmpty.tlbConstructor() 9 | } else { 10 | ChunkRef.tlbConstructor(n) 11 | } as TlbCombinator 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /block-tlb/src/TextChunks.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.tlb.TlbCodec 4 | 5 | public sealed interface TextChunks { 6 | public companion object { 7 | public fun tlbCodec(n: Int): TlbCodec = if (n == 0) { 8 | TextChunkEmpty 9 | } else { 10 | TextChunk.tlbConstructor(n) 11 | } as TlbCodec 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /block-tlb/src/ValidatorBaseInfo.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | 5 | 6 | @SerialName("validator_base_info") 7 | public data class ValidatorBaseInfo( 8 | val validator_list_hash_short: Long, 9 | val catchain_seqno: Long 10 | ) 11 | -------------------------------------------------------------------------------- /block-tlb/src/VmContQuit.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.cell.invoke 7 | import org.ton.tlb.TlbConstructor 8 | 9 | @SerialName("vmc_quit") 10 | 11 | public data class VmContQuit( 12 | val exit_code: Int 13 | ) : VmCont { 14 | public companion object { 15 | public fun tlbConstructor(): TlbConstructor = VmContQuitTlbConstructor 16 | } 17 | } 18 | 19 | private object VmContQuitTlbConstructor : TlbConstructor( 20 | schema = "vmc_quit\$1000 exit_code:int32 = VmCont;" 21 | ) { 22 | override fun storeTlb( 23 | cellBuilder: CellBuilder, value: VmContQuit 24 | ) = cellBuilder { 25 | storeInt(value.exit_code, 32) 26 | } 27 | 28 | override fun loadTlb( 29 | cellSlice: CellSlice 30 | ): VmContQuit = cellSlice { 31 | val exitCode = loadInt(32).toInt() 32 | VmContQuit(exitCode) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /block-tlb/src/VmContQuitExc.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | 8 | @SerialName("vmc_quit_exc") 9 | 10 | public object VmContQuitExc : VmCont { 11 | public fun tlbConstructor(): TlbConstructor = VmContQuitExcTlbConstructor 12 | } 13 | 14 | private object VmContQuitExcTlbConstructor : TlbConstructor( 15 | schema = "vmc_quit_exc\$1001 = VmCont;" 16 | ) { 17 | override fun storeTlb( 18 | cellBuilder: CellBuilder, value: VmContQuitExc 19 | ) = Unit 20 | 21 | override fun loadTlb( 22 | cellSlice: CellSlice 23 | ): VmContQuitExc = VmContQuitExc 24 | } 25 | -------------------------------------------------------------------------------- /block-tlb/src/VmGasLimits.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("NOTHING_TO_INLINE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.SerialName 6 | 7 | @SerialName("gas_limits") 8 | 9 | public data class VmGasLimits( 10 | val remaining: Long, 11 | val max_limit: Long, 12 | val cur_limit: Long, 13 | val credit: Long 14 | ) 15 | -------------------------------------------------------------------------------- /block-tlb/src/VmLibraries.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.Cell 4 | import org.ton.hashmap.HashMapE 5 | 6 | 7 | public data class VmLibraries( 8 | val libraries: HashMapE 9 | ) 10 | -------------------------------------------------------------------------------- /block-tlb/src/VmSaveList.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.cell.invoke 6 | import org.ton.hashmap.HashMapE 7 | import org.ton.tlb.TlbCodec 8 | import org.ton.tlb.TlbConstructor 9 | import org.ton.tlb.loadTlb 10 | import org.ton.tlb.storeTlb 11 | import kotlin.jvm.JvmStatic 12 | 13 | 14 | public data class VmSaveList( 15 | val cregs: HashMapE 16 | ) { 17 | public companion object : TlbCodec by VmSaveListTlbConstructor { 18 | @JvmStatic 19 | public fun tlbCodec(): TlbConstructor = VmSaveListTlbConstructor 20 | } 21 | } 22 | 23 | private object VmSaveListTlbConstructor : TlbConstructor( 24 | schema = "_ cregs:(HashmapE 4 VmStackValue) = VmSaveList;" 25 | ) { 26 | private val hashmapCombinator = HashMapE.tlbCodec(4, VmStackValue) 27 | 28 | override fun storeTlb( 29 | cellBuilder: CellBuilder, 30 | value: VmSaveList 31 | ) = cellBuilder { 32 | storeTlb(hashmapCombinator, value.cregs) 33 | } 34 | 35 | override fun loadTlb( 36 | cellSlice: CellSlice 37 | ): VmSaveList = cellSlice { 38 | val creg = loadTlb(hashmapCombinator) 39 | VmSaveList(creg) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /block-tlb/src/VmStackBuilder.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.Cell 5 | import org.ton.cell.CellBuilder 6 | import org.ton.cell.CellSlice 7 | import org.ton.cell.invoke 8 | import org.ton.tlb.TlbConstructor 9 | import org.ton.tlb.providers.TlbConstructorProvider 10 | 11 | 12 | @SerialName("vm_stk_builder") 13 | public class VmStackBuilder( 14 | public val cell: Cell 15 | ) : VmStackValue { 16 | public constructor(cellBuilder: CellBuilder) : this(cellBuilder.endCell()) 17 | 18 | public fun toCellBuilder(): CellBuilder = CellBuilder(cell) 19 | 20 | override fun toString(): String = "(vm_stk_builder cell:$cell)" 21 | 22 | public companion object : TlbConstructorProvider by VmStackValueBuilderTlbConstructor 23 | } 24 | 25 | private object VmStackValueBuilderTlbConstructor : TlbConstructor( 26 | schema = "vm_stk_builder#05 cell:^Cell = VmStackValue;" 27 | ) { 28 | override fun storeTlb( 29 | cellBuilder: CellBuilder, 30 | value: VmStackBuilder 31 | ) = cellBuilder { 32 | storeRef(value.cell) 33 | } 34 | 35 | override fun loadTlb( 36 | cellSlice: CellSlice 37 | ): VmStackBuilder = cellSlice { 38 | val cell = loadRef() 39 | VmStackBuilder(cell) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /block-tlb/src/VmStackCell.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.Cell 5 | import org.ton.cell.CellBuilder 6 | import org.ton.cell.CellSlice 7 | import org.ton.cell.invoke 8 | import org.ton.tlb.TlbConstructor 9 | import org.ton.tlb.providers.TlbConstructorProvider 10 | 11 | 12 | @SerialName("vm_stk_cell") 13 | public data class VmStackCell( 14 | val cell: Cell 15 | ) : VmStackValue { 16 | override fun toString(): String = "(vm_stk_cell cont:$cell)" 17 | 18 | public companion object : TlbConstructorProvider by VmStackValueCellConstructor 19 | } 20 | 21 | private object VmStackValueCellConstructor : TlbConstructor( 22 | schema = "vm_stk_cell#03 cell:^Cell = VmStackValue;" 23 | ) { 24 | override fun storeTlb( 25 | cellBuilder: CellBuilder, 26 | value: VmStackCell 27 | ) = cellBuilder { 28 | storeRef(value.cell) 29 | } 30 | 31 | override fun loadTlb( 32 | cellSlice: CellSlice 33 | ): VmStackCell = cellSlice { 34 | val cell = loadRef() 35 | VmStackCell(cell) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /block-tlb/src/VmStackCont.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.cell.invoke 7 | import org.ton.tlb.TlbConstructor 8 | import org.ton.tlb.loadTlb 9 | import org.ton.tlb.providers.TlbConstructorProvider 10 | import org.ton.tlb.storeTlb 11 | 12 | 13 | @SerialName("vm_stk_cont") 14 | public data class VmStackCont( 15 | val cont: VmCont 16 | ) : VmStackValue { 17 | override fun toString(): String = "(vm_stk_cont cont:$cont)" 18 | 19 | public companion object : TlbConstructorProvider by VmStackValueContTlbConstructor 20 | } 21 | 22 | private object VmStackValueContTlbConstructor : TlbConstructor( 23 | schema = "vm_stk_cont#06 cont:VmCont = VmStackValue;" 24 | ) { 25 | override fun storeTlb( 26 | cellBuilder: CellBuilder, value: VmStackCont 27 | ) = cellBuilder { 28 | storeTlb(VmCont, value.cont) 29 | } 30 | 31 | override fun loadTlb( 32 | cellSlice: CellSlice 33 | ): VmStackCont = cellSlice { 34 | val cont = loadTlb(VmCont) 35 | VmStackCont(cont) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /block-tlb/src/VmStackNull.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | import org.ton.tlb.providers.TlbConstructorProvider 8 | 9 | @SerialName("vm_stk_null") 10 | 11 | public object VmStackNull : VmStackValue, TlbConstructorProvider by VmStackValueNullConstructor { 12 | override fun toString(): String = "vm_stk_null" 13 | } 14 | 15 | private object VmStackValueNullConstructor : TlbConstructor( 16 | schema = "vm_stk_null#00 = VmStackValue;" 17 | ) { 18 | override fun storeTlb( 19 | cellBuilder: CellBuilder, 20 | value: VmStackNull 21 | ) { 22 | } 23 | 24 | override fun loadTlb( 25 | cellSlice: CellSlice 26 | ): VmStackNull = VmStackNull 27 | } 28 | -------------------------------------------------------------------------------- /block-tlb/src/VmStackNumber.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.bigint.BigInt 4 | 5 | public sealed interface VmStackNumber : VmStackValue { 6 | public fun toInt(): Int 7 | public fun toLong(): Long 8 | public fun toBoolean(): Boolean 9 | public fun toBigInt(): BigInt 10 | 11 | public operator fun plus(other: VmStackNumber): VmStackNumber 12 | public operator fun minus(other: VmStackNumber): VmStackNumber 13 | public operator fun times(other: VmStackNumber): VmStackNumber 14 | public operator fun div(other: VmStackNumber): VmStackNumber 15 | } 16 | -------------------------------------------------------------------------------- /block-tlb/src/VmStackSlice.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.Cell 4 | import org.ton.cell.CellSlice 5 | 6 | public sealed interface VmStackSlice : VmStackValue { 7 | public val cell: Cell 8 | public val stBits: Int 9 | public val endBits: Int 10 | public val stRef: Int 11 | public val endRef: Int 12 | 13 | public fun toCellSlice(): CellSlice = cell.beginParse().apply { 14 | bitsPosition += stBits 15 | refsPosition += stRef 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /block-tlb/src/VmTuple.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.block 4 | 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | import org.ton.tlb.TlbCodec 7 | import kotlin.jvm.JvmStatic 8 | 9 | @JsonClassDiscriminator("@type") 10 | 11 | public sealed interface VmTuple { 12 | public fun depth(): Int 13 | 14 | public companion object { 15 | @Suppress("UNCHECKED_CAST") 16 | @JvmStatic 17 | public fun tlbCodec(n: Int): TlbCodec = if (n == 0) { 18 | VmTupleNil.tlbConstructor() 19 | } else { 20 | VmTupleTcons.tlbCodec(n) 21 | } as TlbCodec 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /block-tlb/src/VmTupleNil.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.tlb.TlbConstructor 7 | import org.ton.tlb.providers.TlbConstructorProvider 8 | 9 | @SerialName("vm_tuple_nil") 10 | 11 | public object VmTupleNil : VmTuple, TlbConstructorProvider by VmTupleNilTlbConstructor { 12 | override fun depth(): Int = 0 13 | 14 | override fun toString(): String = "vm_tuple_nil" 15 | } 16 | 17 | private object VmTupleNilTlbConstructor : TlbConstructor( 18 | schema = "vm_tuple_nil\$_ = VmTuple 0;" 19 | ) { 20 | override fun storeTlb(cellBuilder: CellBuilder, value: VmTupleNil) { 21 | } 22 | 23 | override fun loadTlb(cellSlice: CellSlice): VmTupleNil = VmTupleNil 24 | } 25 | -------------------------------------------------------------------------------- /block-tlb/test/AddrStdTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import kotlin.test.Test 4 | 5 | class AddrStdTest { 6 | @Test 7 | fun testParse() { 8 | val original = "EQAKtVj024T9MfYaJzU1xnDAkf_GGbHNu-V2mgvyjTuP6rvC" 9 | val addrStd = AddrStd(original) 10 | println(addrStd) 11 | val addrStd1 = AddrStd(0, "0AB558F4DB84FD31F61A273535C670C091FFC619B1CDBBE5769A0BF28D3B8FEA".hexToByteArray()) 12 | println(original) 13 | println(addrStd1.toString(userFriendly = true)) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /block-tlb/test/VmStackListTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.tlb.loadTlb 5 | import org.ton.tlb.storeTlb 6 | import kotlin.test.Test 7 | import kotlin.test.assertEquals 8 | 9 | class VmStackListTest { 10 | @Test 11 | fun `test VmStackList serialization`() { 12 | val vmStackList = VmStackList(VmStackTinyInt(37218)) 13 | val depth = vmStackList.count() 14 | 15 | val cellBuilder = CellBuilder.beginCell() 16 | cellBuilder.storeTlb(VmStackList.tlbCodec(depth), vmStackList) 17 | val cell = cellBuilder.endCell() 18 | 19 | val cellSlice = cell.beginParse() 20 | val newVmStackList = cellSlice.loadTlb(VmStackList.tlbCodec(depth)) 21 | 22 | assertEquals(vmStackList, newVmStackList) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /block-tlb/test/VmStackTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.tlb.loadTlb 5 | import org.ton.tlb.storeTlb 6 | import kotlin.test.Test 7 | import kotlin.test.assertEquals 8 | 9 | class VmStackTest { 10 | @Test 11 | fun `test VmStack serialization with VmStackList-Nil`() { 12 | val stack = VmStack(VmStackList.Nil) 13 | val cell = CellBuilder.createCell { 14 | storeTlb(VmStack.tlbCodec(), stack) 15 | } 16 | val stack2 = cell.parse { 17 | loadTlb(VmStack.tlbCodec()) 18 | } 19 | assertEquals(stack, stack2) 20 | } 21 | 22 | @Test 23 | fun `test VmStack serialization with VmStackList-Single`() { 24 | val stack = VmStack( 25 | VmStackList.Cons( 26 | VmStackList.Cons( 27 | VmStackList.Nil, VmStackTinyInt(1313) 28 | ), VmStackInt(12) 29 | ) 30 | ) 31 | val cell = CellBuilder.createCell { 32 | storeTlb(VmStack.tlbCodec(), stack) 33 | } 34 | val stack2 = cell.parse { 35 | loadTlb(VmStack.tlbCodec()) 36 | } 37 | assertEquals(stack, stack2) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /block-tlb/test/util.kt: -------------------------------------------------------------------------------- 1 | package org.ton.block 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.tlb.TlbCodec 5 | import org.ton.tlb.loadTlb 6 | import org.ton.tlb.storeTlb 7 | import kotlin.test.assertEquals 8 | 9 | fun testSerialization(codec: TlbCodec, stackValue: T) { 10 | val cellBuilder = CellBuilder.beginCell() 11 | cellBuilder.storeTlb(codec, stackValue) 12 | val cell = cellBuilder.endCell() 13 | 14 | val cellSlice = cell.beginParse() 15 | val result = cellSlice.loadTlb(codec) 16 | assertEquals(stackValue, result) 17 | cellSlice.endParse() 18 | } 19 | -------------------------------------------------------------------------------- /build-logic/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `kotlin-dsl` 3 | } 4 | 5 | repositories { 6 | mavenCentral() 7 | gradlePluginPortal() 8 | } 9 | 10 | dependencies { 11 | implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20") 12 | implementation("com.vanniktech:gradle-maven-publish-plugin:0.28.0") 13 | } 14 | -------------------------------------------------------------------------------- /build-logic/src/main/kotlin/publish.gradle.kts: -------------------------------------------------------------------------------- 1 | import com.vanniktech.maven.publish.SonatypeHost 2 | 3 | plugins { 4 | id("com.vanniktech.maven.publish") 5 | } 6 | 7 | mavenPublishing { 8 | pom { 9 | name = project.name 10 | description = "Kotlin/Multiplatform SDK for The Open Network" 11 | inceptionYear = "2025" 12 | url = "https://github.com/ton-community/ton-kotlin" 13 | 14 | licenses { 15 | license { 16 | name = "The Apache Software License, Version 2.0" 17 | url = "https://www.apache.org/licenses/LICENSE-2.0.txt" 18 | distribution = "repo" 19 | } 20 | } 21 | 22 | developers { 23 | developer { 24 | id = "andreypfau" 25 | name = "Andrey Pfau" 26 | email = "andreypfau@ton.org" 27 | } 28 | } 29 | scm { 30 | url = "https://github.com/ton-community/ton-kotlin" 31 | } 32 | } 33 | 34 | publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL) 35 | signAllPublications() 36 | } 37 | -------------------------------------------------------------------------------- /contract/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinLiteclient) 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /contract/src/ChunkedData.kt: -------------------------------------------------------------------------------- 1 | package org.ton.contract 2 | 3 | import org.ton.cell.Cell 4 | import org.ton.cell.CellBuilder 5 | import org.ton.cell.CellSlice 6 | import org.ton.hashmap.HashMapE 7 | import org.ton.tlb.TlbConstructor 8 | import org.ton.tlb.constructor.tlbCodec 9 | import org.ton.tlb.loadTlb 10 | import org.ton.tlb.providers.TlbConstructorProvider 11 | import org.ton.tlb.storeTlb 12 | 13 | public data class ChunkedData( 14 | val data: HashMapE 15 | ) { 16 | public companion object : TlbConstructorProvider by ChunkedDataConstructor 17 | } 18 | 19 | private object ChunkedDataConstructor : TlbConstructor( 20 | schema = "chunked_data#_ data:(HashMapE 32 ^(SnakeData ~0)) = ChunkedData;" 21 | ) { 22 | // SnakeData ~0 is SnakeDataTail 23 | private val dataCodec = 24 | HashMapE.tlbCodec(32, Cell.tlbCodec(SnakeDataTail)) 25 | 26 | override fun storeTlb(cellBuilder: CellBuilder, value: ChunkedData) { 27 | cellBuilder.storeTlb(dataCodec, value.data) 28 | } 29 | 30 | override fun loadTlb(cellSlice: CellSlice): ChunkedData = 31 | ChunkedData(cellSlice.loadTlb(dataCodec)) 32 | } 33 | -------------------------------------------------------------------------------- /contract/src/SmartContract.kt: -------------------------------------------------------------------------------- 1 | package org.ton.contract 2 | 3 | import org.ton.block.AddrStd 4 | import org.ton.lite.client.LiteClient 5 | 6 | public interface SmartContract { 7 | public val liteClient: LiteClient 8 | public val address: AddrStd 9 | } 10 | -------------------------------------------------------------------------------- /contract/src/Text.kt: -------------------------------------------------------------------------------- 1 | package org.ton.contract 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.tlb.TlbConstructor 6 | import org.ton.tlb.loadTlb 7 | import org.ton.tlb.providers.TlbConstructorProvider 8 | import org.ton.tlb.storeTlb 9 | 10 | public data class Text( 11 | val data: SnakeData 12 | ) { 13 | public companion object : TlbConstructorProvider by TextConstructor 14 | } 15 | 16 | private object TextConstructor : TlbConstructor( 17 | schema = "text#_ {n:#} data:(SnakeData ~n) = Text;" 18 | ) { 19 | override fun storeTlb(cellBuilder: CellBuilder, value: Text) { 20 | cellBuilder.storeTlb(SnakeData, value.data) 21 | } 22 | 23 | override fun loadTlb(cellSlice: CellSlice): Text = Text(cellSlice.loadTlb(SnakeData)) 24 | } 25 | -------------------------------------------------------------------------------- /contract/src/exception/AccountNotInitializedException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.contract.exception 2 | 3 | import org.ton.block.MsgAddressInt 4 | 5 | public class AccountNotInitializedException( 6 | public val address: MsgAddressInt 7 | ) : RuntimeException("Account not initialized: $address") 8 | -------------------------------------------------------------------------------- /contract/src/wallet/WalletContract.kt: -------------------------------------------------------------------------------- 1 | package org.ton.contract.wallet 2 | 3 | import org.ton.api.pk.PrivateKeyEd25519 4 | import org.ton.contract.SmartContract 5 | import kotlin.contracts.InvocationKind 6 | import kotlin.contracts.contract 7 | 8 | public interface WalletContract : SmartContract { 9 | public companion object { 10 | public const val DEFAULT_WALLET_ID: Int = 698983191 // First 4 bytes of zero state root hash of mainnet (as int32 in Little Endian) 11 | } 12 | 13 | public suspend fun transfer(privateKey: PrivateKeyEd25519, transfer: WalletTransfer) 14 | } 15 | 16 | public suspend inline fun WalletContract.transfer(privateKey: PrivateKeyEd25519, transfer: WalletTransferBuilder.() -> Unit) { 17 | contract { callsInPlace(transfer, InvocationKind.EXACTLY_ONCE) } 18 | transfer(privateKey, WalletTransferBuilder().apply(transfer).build()) 19 | } 20 | -------------------------------------------------------------------------------- /contract/test/wallet/LiteClient.kt: -------------------------------------------------------------------------------- 1 | package org.ton.contract.wallet 2 | 3 | import kotlinx.coroutines.Dispatchers 4 | import org.ton.lite.client.LiteClient 5 | 6 | fun liteClientTestnet() = LiteClient( 7 | liteClientConfigGlobal = TESTNET_GLOBAL_CONFIG, 8 | coroutineContext = Dispatchers.Default 9 | ) 10 | -------------------------------------------------------------------------------- /crypto/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(libs.ktor.utils) 11 | api(libs.sha2) 12 | api(libs.aes) 13 | api(libs.crc32) 14 | api(libs.pbkdf2) 15 | api(libs.hmac) 16 | implementation(libs.curve25519) 17 | implementation(libs.serialization.core) 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /crypto/src/Decryptor.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | public interface Decryptor { 4 | public fun decrypt(data: ByteArray): ByteArray 5 | public fun sign(message: ByteArray): ByteArray 6 | } 7 | 8 | public object DecryptorNone : Decryptor { 9 | override fun decrypt(data: ByteArray): ByteArray = data 10 | override fun sign(message: ByteArray): ByteArray = ByteArray(0) 11 | } 12 | 13 | public object DecryptorFail : Decryptor { 14 | override fun decrypt(data: ByteArray): ByteArray = 15 | throw IllegalStateException("Fail decryptor") 16 | 17 | override fun sign(message: ByteArray): ByteArray = 18 | throw IllegalStateException("Fail decryptor") 19 | } 20 | -------------------------------------------------------------------------------- /crypto/src/Encryptor.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | public interface Encryptor { 4 | public fun encrypt(data: ByteArray): ByteArray 5 | public fun verify(message: ByteArray, signature: ByteArray?): Boolean 6 | } 7 | 8 | public object EncryptorNone : Encryptor { 9 | override fun encrypt(data: ByteArray): ByteArray = data 10 | override fun verify(message: ByteArray, signature: ByteArray?): Boolean = true 11 | } 12 | 13 | public object EncryptorFail : Encryptor { 14 | override fun encrypt(data: ByteArray): ByteArray = 15 | throw IllegalStateException("Fail encryptor") 16 | 17 | override fun verify(message: ByteArray, signature: ByteArray?): Boolean = 18 | throw IllegalStateException("Fail encryptor") 19 | } 20 | -------------------------------------------------------------------------------- /crypto/src/HexByteArraySerializer.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | import kotlinx.serialization.KSerializer 4 | import kotlinx.serialization.descriptors.SerialDescriptor 5 | import kotlinx.serialization.descriptors.serialDescriptor 6 | import kotlinx.serialization.encoding.Decoder 7 | import kotlinx.serialization.encoding.Encoder 8 | 9 | @OptIn(ExperimentalStdlibApi::class) 10 | public object HexByteArraySerializer : KSerializer { 11 | override val descriptor: SerialDescriptor = serialDescriptor() 12 | 13 | override fun deserialize(decoder: Decoder): ByteArray = decoder.decodeString().hexToByteArray() 14 | 15 | override fun serialize(encoder: Encoder, value: ByteArray) { 16 | encoder.encodeString(value.toHexString()) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /crypto/src/SecureRandom.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | import kotlin.random.Random 4 | 5 | public expect object SecureRandom : Random 6 | 7 | internal inline fun Int.takeUpperBits(bitCount: Int): Int = 8 | this.ushr(32 - bitCount) and (-bitCount).shr(31) 9 | -------------------------------------------------------------------------------- /crypto/src@apple/SecureRandom.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | import kotlinx.cinterop.* 4 | import platform.posix.fclose 5 | import platform.posix.fopen 6 | import platform.posix.fread 7 | import kotlin.random.Random 8 | 9 | @OptIn(ExperimentalForeignApi::class, UnsafeNumber::class) 10 | public actual object SecureRandom : Random() { 11 | override fun nextBits(bitCount: Int): Int = nextInt().takeUpperBits(bitCount) 12 | 13 | override fun nextInt(): Int = memScoped { 14 | val file = fopen("/dev/urandom", "rb") ?: error("Can't open /dev/urandom") 15 | val int = alloc() 16 | fread(int.ptr, 4.convert(), 1.convert(), file) 17 | fclose(file) 18 | return int.value 19 | } 20 | 21 | override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray { 22 | val fd = fopen("/dev/urandom", "rb") ?: error("Can't open /dev/urandom") 23 | array.usePinned { 24 | fread(it.addressOf(fromIndex), 1u, (toIndex - fromIndex).convert(), fd) 25 | } 26 | fclose(fd) 27 | return array 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /crypto/src@jvm/AesCtr.kt: -------------------------------------------------------------------------------- 1 | @file:JvmName("AesCtrJvmKt") 2 | 3 | package org.ton.crypto 4 | 5 | import javax.crypto.Cipher 6 | import javax.crypto.spec.IvParameterSpec 7 | import javax.crypto.spec.SecretKeySpec 8 | 9 | public actual class AesCtr actual constructor( 10 | key: ByteArray, 11 | iv: ByteArray, 12 | ) { 13 | private val cipher = Cipher.getInstance("AES/CTR/NoPadding").also { 14 | it.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv)) 15 | } 16 | 17 | public actual fun doFinal(): ByteArray = cipher.doFinal() 18 | 19 | public actual fun update(input: ByteArray, output: ByteArray): ByteArray { 20 | cipher.update(input, 0, input.size, output, 0) 21 | return output 22 | } 23 | 24 | public actual fun doFinal(output: ByteArray): ByteArray { 25 | cipher.doFinal(output, 0) 26 | return output 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /crypto/src@jvm/SecureRandom.kt: -------------------------------------------------------------------------------- 1 | @file:JvmName("SecureRandomJvmKt") 2 | 3 | package org.ton.crypto 4 | 5 | import kotlin.random.Random 6 | 7 | public actual object SecureRandom : Random() { 8 | private val javaSecureRandom = java.security.SecureRandom() 9 | 10 | override fun nextBits(bitCount: Int): Int = nextInt().takeUpperBits(bitCount) 11 | 12 | override fun nextInt(): Int = javaSecureRandom.nextInt() 13 | 14 | override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray { 15 | val tmp = ByteArray(toIndex - fromIndex) 16 | javaSecureRandom.nextBytes(tmp) 17 | tmp.copyInto(array, fromIndex) 18 | return array 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /crypto/src@linux/SecureRandom.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | import kotlinx.cinterop.* 4 | import platform.posix.fclose 5 | import platform.posix.fopen 6 | import platform.posix.fread 7 | import kotlin.random.Random 8 | 9 | @OptIn(ExperimentalForeignApi::class) 10 | public actual object SecureRandom : Random() { 11 | override fun nextBits(bitCount: Int): Int = nextInt().takeUpperBits(bitCount) 12 | 13 | override fun nextInt(): Int = memScoped { 14 | val file = fopen("/dev/urandom", "rb") ?: error("Can't open /dev/urandom") 15 | val int = alloc() 16 | fread(int.ptr, 4.convert(), 1.convert(), file) 17 | fclose(file) 18 | return int.value 19 | } 20 | 21 | override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray { 22 | val fd = fopen("/dev/urandom", "rb") ?: error("Can't open /dev/urandom") 23 | array.usePinned { 24 | fread(it.addressOf(fromIndex), 1u, (toIndex - fromIndex).convert(), fd) 25 | } 26 | fclose(fd) 27 | return array 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /crypto/src@native/AesCtr.kt: -------------------------------------------------------------------------------- 1 | package org.ton.crypto 2 | 3 | import internal.AesCtr256Impl 4 | 5 | public actual class AesCtr actual constructor( 6 | key: ByteArray, 7 | iv: ByteArray 8 | ) { 9 | private val aes = AesCtr256Impl(key, iv) 10 | 11 | public actual fun update(input: ByteArray, output: ByteArray): ByteArray = 12 | aes.encrypt(input, output) 13 | 14 | public actual fun doFinal(output: ByteArray): ByteArray = byteArrayOf() 15 | 16 | public actual fun doFinal(): ByteArray = byteArrayOf() 17 | } 18 | -------------------------------------------------------------------------------- /crypto/test/Crc16Test.kt: -------------------------------------------------------------------------------- 1 | import org.ton.crypto.crc16 2 | import kotlin.test.Test 3 | import kotlin.test.assertEquals 4 | 5 | class Crc16Test { 6 | @Test 7 | fun `sanity tests`() { 8 | assertEquals( 9 | 0x0000, 10 | crc16("") 11 | ) 12 | assertEquals( 13 | 0x31C3, 14 | crc16("123456789") 15 | ) 16 | assertEquals( 17 | 0x9DD6, 18 | crc16("abc") 19 | ) 20 | assertEquals( 21 | 0x3994, 22 | crc16("ABC") 23 | ) 24 | assertEquals( 25 | 0x21E3, 26 | crc16("This is a string") 27 | ) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /examples/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension 2 | 3 | plugins { 4 | kotlin("multiplatform") 5 | } 6 | 7 | kotlin { 8 | jvm() 9 | configureSourceSetsLayout() 10 | 11 | sourceSets { 12 | commonMain { 13 | dependencies { 14 | api(projects.tonKotlinLiteclient) 15 | api(projects.tonKotlinContract) 16 | } 17 | } 18 | } 19 | } 20 | 21 | fun KotlinMultiplatformExtension.configureSourceSetsLayout() { 22 | sourceSets { 23 | all { 24 | if (name.endsWith("Main")) { 25 | val suffix = if (name.startsWith("common")) "" else "@${name.removeSuffix("Main")}" 26 | kotlin.srcDir("src$suffix") 27 | resources.srcDir("resources$suffix") 28 | } 29 | if (name.endsWith("Test")) { 30 | val suffix = if (name.startsWith("common")) "" else "@${name.removeSuffix("Test")}" 31 | kotlin.srcDir("test$suffix") 32 | resources.srcDir("testResources$suffix") 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /examples/src/ConfigContractExample.kt: -------------------------------------------------------------------------------- 1 | package org.ton.kotlin.examples 2 | 3 | import org.ton.block.AccountActive 4 | import org.ton.kotlin.examples.contract.config.ConfigContract 5 | import org.ton.kotlin.examples.contract.config.ConfigData 6 | import org.ton.kotlin.examples.provider.LiteClientProvider 7 | import org.ton.kotlin.examples.provider.liteClientMainnet 8 | 9 | private val provider = LiteClientProvider(liteClientMainnet()) 10 | 11 | suspend fun main() { 12 | val configContract = ConfigContract(provider) 13 | 14 | val account = configContract.getState()?.loadAccount() ?: error("cant load account not found") 15 | val data = (account.state as AccountActive).value.data.value?.cell ?: error("cant load data") 16 | val configData = ConfigData.loadTlb(data.beginParse()) 17 | println("seqno: ${configData.seqno}") 18 | println("public key: ${configData.publicKey}") 19 | println("voteDict: ${configData.voteDict}") 20 | for (entry in configData.voteDict) { 21 | println("proposal: ${entry.key}") 22 | println(entry.value) 23 | } 24 | } -------------------------------------------------------------------------------- /examples/src/TestSer.kt: -------------------------------------------------------------------------------- 1 | package org.ton.kotlin.examples 2 | 3 | import org.ton.block.AddrStd 4 | import org.ton.block.CommonMsgInfo 5 | import org.ton.block.ExtInMsgInfo 6 | import org.ton.cell.buildCell 7 | import org.ton.tlb.storeTlb 8 | 9 | fun main() { 10 | val a = ExtInMsgInfo( 11 | null, 12 | // AddrStd(0, BitString("FF".repeat(32))) 13 | AddrStd("kQBZE6MtrDRmMYDNKJwQDDRc1UiiaMRH9XFfrbVQ53zkz2Yl") 14 | ) 15 | val cell = buildCell { storeTlb(CommonMsgInfo, a) } 16 | println(cell.bits.toBinary()) 17 | } -------------------------------------------------------------------------------- /examples/src/contract/WalletMessage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.kotlin.examples.contract 2 | 3 | import org.ton.api.pk.PrivateKey 4 | import org.ton.bitstring.BitString 5 | import org.ton.contract.wallet.WalletTransfer 6 | 7 | interface WalletMessage { 8 | val seqno: Int 9 | val transfers: List 10 | 11 | fun sign(privateKey: PrivateKey): SignedWalletMessage 12 | } 13 | 14 | interface SignedWalletMessage : WalletMessage { 15 | val signature: BitString 16 | } -------------------------------------------------------------------------------- /examples/src/provider/LiteClient.kt: -------------------------------------------------------------------------------- 1 | package org.ton.kotlin.examples.provider 2 | 3 | import kotlinx.coroutines.Dispatchers 4 | import org.ton.lite.client.LiteClient 5 | 6 | fun liteClientTestnet() = LiteClient( 7 | liteClientConfigGlobal = TESTNET_GLOBAL_CONFIG, 8 | coroutineContext = Dispatchers.Default 9 | ) 10 | 11 | fun liteClientMainnet() = LiteClient( 12 | liteClientConfigGlobal = MAINNET_GLOBAL_CONFIG, 13 | coroutineContext = Dispatchers.Default 14 | ) -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | org.gradle.jvmargs=-Xmx4G 3 | org.gradle.parallel=true 4 | kotlin.version=1.9.20 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ton-blockchain/ton-kotlin/29b1b1d3cc91ebafab70af54cc4a19b7ec81ca5a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /hashmap-tlb/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinBitstring) 11 | api(projects.tonKotlinTlb) 12 | implementation(libs.serialization.json) 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /hashmap-tlb/src/AugmentedDictionary.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import org.ton.bitstring.BitString 4 | 5 | public interface AugmentedDictionary : Iterable>> { 6 | public operator fun get(key: BitString): Leaf? 7 | 8 | public interface Node { 9 | public val extra: Y 10 | public val value: X? 11 | } 12 | 13 | public interface Leaf : Node { 14 | override val extra: Y 15 | override val value: X 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /hashmap-tlb/src/DictionarySet.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("PackageDirectoryMismatch") 2 | 3 | package org.ton.kotlin.dict 4 | 5 | internal enum class SetMode( 6 | val mask: Int 7 | ) { 8 | /** 9 | * Sets the value associated with the key in the dictionary. 10 | */ 11 | Set(0b11), 12 | 13 | /** 14 | * Sets the value associated with the key in the dictionary only if the key was already present in it. 15 | */ 16 | Replace(0b01), 17 | 18 | /** 19 | * Sets the value associated with key in dictionary, but only if it is not already present. 20 | */ 21 | Add(0b10); 22 | 23 | fun canReplace(): Boolean = mask and 0b01 != 0 24 | 25 | fun canAdd(): Boolean = mask and 0b10 != 0 26 | } -------------------------------------------------------------------------------- /hashmap-tlb/src/HashMapNode.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.hashmap 4 | 5 | import kotlinx.serialization.Serializable 6 | import kotlinx.serialization.json.JsonClassDiscriminator 7 | import org.ton.tlb.TlbCodec 8 | import org.ton.tlb.TlbObject 9 | import kotlin.jvm.JvmStatic 10 | 11 | @Serializable 12 | @JsonClassDiscriminator("@type") 13 | public sealed interface HashMapNode : TlbObject { 14 | public companion object { 15 | @Suppress("UNCHECKED_CAST") 16 | @JvmStatic 17 | public fun tlbCodec(n: Int, x: TlbCodec): TlbCodec> = 18 | if (n == 0) { 19 | HmnLeaf.tlbCodec(x) 20 | } else { 21 | HmnFork.tlbCodec(n, x) 22 | } as TlbCodec> 23 | 24 | public fun leaf(value: T): HmnLeaf = HmnLeaf(value) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /hashmap-tlb/src/HmeEmpty.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.bitstring.BitString 6 | import org.ton.tlb.TlbPrettyPrinter 7 | 8 | @Serializable 9 | @SerialName("hme_empty") 10 | public class HmeEmpty : HashMapE { 11 | override fun iterator(): Iterator> = EmptyIterator() 12 | 13 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter = 14 | printer.type("hme_empty") 15 | 16 | override fun toString(): String = print().toString() 17 | } 18 | 19 | private class EmptyIterator : Iterator> { 20 | override fun hasNext(): Boolean = false 21 | override fun next(): Pair = throw NoSuchElementException() 22 | } 23 | -------------------------------------------------------------------------------- /hashmap-tlb/src/UnarySuccess.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tlb.TlbPrettyPrinter 6 | 7 | @Serializable 8 | @SerialName("unary_succ") 9 | public data class UnarySuccess( 10 | val x: Unary 11 | ) : Unary() { 12 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter = printer.type("unary_succ") { 13 | field("x", x) 14 | } 15 | 16 | override fun toString(): String = print().toString() 17 | } 18 | -------------------------------------------------------------------------------- /hashmap-tlb/src/UnaryZero.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tlb.TlbPrettyPrinter 6 | 7 | @Serializable 8 | @SerialName("unary_zero") 9 | public object UnaryZero : Unary() { 10 | override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter = printer.type("unary_zero") 11 | 12 | override fun toString(): String = print().toString() 13 | } 14 | -------------------------------------------------------------------------------- /hashmap-tlb/test/HashMapEditTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import org.ton.bitstring.BitString 4 | import kotlin.random.Random 5 | import kotlin.test.Test 6 | import kotlin.test.assertContentEquals 7 | import kotlin.time.ExperimentalTime 8 | 9 | class HashMapEditTest { 10 | 11 | @ExperimentalTime 12 | @Test 13 | fun test() { 14 | val list = List(100) { 15 | BitString(*BooleanArray(32) { Random.nextBoolean() }) 16 | } 17 | val expected = list.sorted() 18 | 19 | repeat(10) { 20 | var hashMap = HashMapE.empty() 21 | 22 | list.shuffled().forEach { 23 | hashMap = hashMap.set(it) 24 | } 25 | val actual = hashMap.map { it.first } 26 | assertContentEquals(expected, actual) 27 | } 28 | } 29 | } 30 | 31 | private fun HashMapE.set(key: BitString): HmeRoot { 32 | return set(key, key) 33 | } 34 | 35 | private fun HashMapE.debug() { 36 | println("=================================") 37 | println((this as HmeRoot<*>).root.value) 38 | println("--------------") 39 | println(this.joinToString("\n") { 40 | "${it.first.toBinary()} = ${it.second}" 41 | }) 42 | } 43 | -------------------------------------------------------------------------------- /hashmap-tlb/test/HashMapLabelTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import org.ton.bitstring.BitString 4 | import kotlin.test.Test 5 | import kotlin.test.assertEquals 6 | import kotlin.test.assertNull 7 | 8 | class HashMapLabelTest { 9 | @Test 10 | fun `test hml_same construction`() { 11 | assertNull(HmlSame.of(BitString.binary("0001"))) 12 | assertNull(HmlSame.of(BitString.binary("1110"))) 13 | assertNull(HmlSame.of(BitString.binary("1110111"))) 14 | assertNull(HmlSame.of(BitString.binary("0001000"))) 15 | 16 | assertEquals(HmlSame(true, 1), HmlSame.of(BitString.binary("1"))) 17 | assertEquals(HmlSame(true, 2), HmlSame.of(BitString.binary("11"))) 18 | assertEquals(HmlSame(true, 3), HmlSame.of(BitString.binary("111"))) 19 | assertEquals(HmlSame(false, 1), HmlSame.of(BitString.binary("0"))) 20 | assertEquals(HmlSame(false, 2), HmlSame.of(BitString.binary("00"))) 21 | assertEquals(HmlSame(false, 3), HmlSame.of(BitString.binary("000"))) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /hashmap-tlb/test/UnaryTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap 2 | 3 | import org.ton.bitstring.BitString 4 | import org.ton.cell.Cell 5 | import org.ton.hashmap.tlb.testSerialization 6 | import org.ton.tlb.loadNegatedTlb 7 | import kotlin.test.Test 8 | import kotlin.test.assertEquals 9 | 10 | class UnaryTest { 11 | @Test 12 | fun `test serialization Unary`() { 13 | val codec = Unary.tlbCodec() 14 | 15 | testSerialization(codec, UnaryZero) 16 | testSerialization(codec, UnarySuccess(UnaryZero)) 17 | testSerialization(codec, UnarySuccess(UnarySuccess(UnaryZero))) 18 | testSerialization(codec, UnarySuccess(UnarySuccess(UnarySuccess(UnaryZero)))) 19 | testSerialization(codec, UnarySuccess(UnarySuccess(UnarySuccess(UnarySuccess(UnaryZero))))) 20 | 21 | Cell(BitString.binary("1111111100101")).parse { 22 | val (depth, result) = loadNegatedTlb(codec) 23 | assertEquals(BitString.binary("0101"), loadBits(4)) 24 | assertEquals(Unary(8), result) 25 | assertEquals(8, depth) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /hashmap-tlb/test/util.kt: -------------------------------------------------------------------------------- 1 | package org.ton.hashmap.tlb 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.tlb.TlbCodec 5 | import org.ton.tlb.loadTlb 6 | import org.ton.tlb.storeTlb 7 | import kotlin.test.assertEquals 8 | 9 | fun testSerialization(codec: TlbCodec, stackValue: T) { 10 | val cellBuilder = CellBuilder.beginCell() 11 | cellBuilder.storeTlb(codec, stackValue) 12 | val cell = cellBuilder.endCell() 13 | val cellSlice = cell.beginParse() 14 | val result = cellSlice.loadTlb(codec) 15 | assertEquals(stackValue, result) 16 | cellSlice.endParse() 17 | } 18 | -------------------------------------------------------------------------------- /liteapi-tl/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinTonapiTl) 11 | api(projects.tonKotlinBlockTlb) //TODO: remove dependency 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerBlockData.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.api.tonnode.TonNodeBlockIdExt 7 | import org.ton.tl.* 8 | import kotlin.jvm.JvmName 9 | 10 | @Serializable 11 | @SerialName("liteServer.blockData") 12 | public class LiteServerBlockData( 13 | @get:JvmName("id") 14 | public val id: TonNodeBlockIdExt, 15 | 16 | @get:JvmName("data") 17 | @Serializable(ByteStringBase64Serializer::class) 18 | public val data: ByteString 19 | ) { 20 | public companion object : TlConstructor( 21 | schema = "liteServer.blockData id:tonNode.blockIdExt data:bytes = liteServer.BlockData" 22 | ) { 23 | override fun decode(reader: TlReader): LiteServerBlockData { 24 | val id = reader.read(TonNodeBlockIdExt) 25 | val data = reader.readByteString() 26 | return LiteServerBlockData(id, data) 27 | } 28 | 29 | override fun encode(writer: TlWriter, value: LiteServerBlockData) { 30 | writer.write(TonNodeBlockIdExt, value.id) 31 | writer.writeBytes(value.data) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerBlockLink.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import org.ton.api.tonnode.TonNodeBlockIdExt 4 | import org.ton.tl.TlCodec 5 | import org.ton.tl.TlCombinator 6 | 7 | public sealed interface LiteServerBlockLink { 8 | public val toKeyBlock: Boolean 9 | public val from: TonNodeBlockIdExt 10 | public val to: TonNodeBlockIdExt 11 | 12 | public companion object : TlCodec by LiteServerBlockLinkTlCombinator 13 | } 14 | 15 | private object LiteServerBlockLinkTlCombinator : TlCombinator( 16 | LiteServerBlockLink::class, 17 | LiteServerBlockLinkBack::class to LiteServerBlockLinkBack.tlConstructor(), 18 | LiteServerBlockLinkForward::class to LiteServerBlockLinkForward.tlConstructor() 19 | ) 20 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerCurrentTime.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlConstructor 6 | import org.ton.tl.TlReader 7 | import org.ton.tl.TlWriter 8 | import kotlin.jvm.JvmName 9 | 10 | @Serializable 11 | @SerialName("liteServer.currentTime") 12 | public data class LiteServerCurrentTime( 13 | @get:JvmName("now") 14 | val now: Int 15 | ) { 16 | public companion object : TlConstructor( 17 | schema = "liteServer.currentTime now:int = liteServer.CurrentTime" 18 | ) { 19 | override fun decode(reader: TlReader): LiteServerCurrentTime { 20 | val now = reader.readInt() 21 | return LiteServerCurrentTime(now) 22 | } 23 | 24 | override fun encode(writer: TlWriter, value: LiteServerCurrentTime) { 25 | writer.writeInt(value.now) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerDebugVerbosity.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | import kotlin.jvm.JvmName 10 | 11 | @Serializable 12 | @SerialName("liteServer.debug.verbosity") 13 | public data class LiteServerDebugVerbosity( 14 | @get:JvmName("value") 15 | val value: Int 16 | ) { 17 | public companion object : TlCodec by LiteServerDebugVerbosityTlConstructor 18 | } 19 | 20 | private object LiteServerDebugVerbosityTlConstructor : TlConstructor( 21 | schema = "liteServer.debug.verbosity value:int = liteServer.debug.Verbosity" 22 | ) { 23 | override fun decode(reader: TlReader): LiteServerDebugVerbosity { 24 | val value = reader.readInt() 25 | return LiteServerDebugVerbosity(value) 26 | } 27 | 28 | override fun encode(writer: TlWriter, value: LiteServerDebugVerbosity) { 29 | writer.writeInt(value.value) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerError.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlConstructor 6 | import org.ton.tl.TlReader 7 | import org.ton.tl.TlWriter 8 | import kotlin.jvm.JvmName 9 | 10 | @Serializable 11 | @SerialName("liteServer.error") 12 | public data class LiteServerError( 13 | @get:JvmName("code") 14 | val code: Int, 15 | 16 | @get:JvmName("message") 17 | val message: String 18 | ) { 19 | public companion object : TlConstructor( 20 | schema = "liteServer.error code:int message:string = liteServer.Error" 21 | ) { 22 | override fun decode(reader: TlReader): LiteServerError { 23 | val code = reader.readInt() 24 | val message = reader.readString() 25 | return LiteServerError(code, message) 26 | } 27 | 28 | override fun encode(writer: TlWriter, value: LiteServerError) { 29 | writer.writeInt(value.code) 30 | writer.writeString(value.message) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerQueryPrefix.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | 10 | @Serializable 11 | @SerialName("liteServer.queryPrefix") 12 | public object LiteServerQueryPrefix : TlCodec by LiteServerQueryPrefixTlConstructor 13 | 14 | private object LiteServerQueryPrefixTlConstructor : TlConstructor( 15 | schema = "liteServer.queryPrefix = Object" 16 | ) { 17 | override fun decode(reader: TlReader): LiteServerQueryPrefix = LiteServerQueryPrefix 18 | override fun encode(writer: TlWriter, value: LiteServerQueryPrefix) {} 19 | } 20 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/LiteServerSendMsgStatus.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlConstructor 6 | import org.ton.tl.TlReader 7 | import org.ton.tl.TlWriter 8 | import kotlin.jvm.JvmName 9 | 10 | @Serializable 11 | @SerialName("liteServer.sendMsgStatus") 12 | public data class LiteServerSendMsgStatus( 13 | @get:JvmName("status") 14 | val status: Int 15 | ) { 16 | public companion object : TlConstructor( 17 | schema = "liteServer.sendMsgStatus status:int = liteServer.SendMsgStatus" 18 | ) { 19 | override fun decode(reader: TlReader): LiteServerSendMsgStatus { 20 | val status = reader.readInt() 21 | return LiteServerSendMsgStatus(status) 22 | } 23 | 24 | override fun encode(writer: TlWriter, value: LiteServerSendMsgStatus) { 25 | writer.writeInt(value.status) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/functions/LiteServerGetMasterchainInfo.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver.functions 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.lite.api.liteserver.LiteServerMasterchainInfo 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @SerialName("liteServer.getMasterchainInfo") 10 | public object LiteServerGetMasterchainInfo : 11 | TLFunction, 12 | TlCodec by LiteServerGetMasterchainInfoTlConstructor { 13 | override fun tlCodec(): TlCodec = LiteServerGetMasterchainInfoTlConstructor 14 | override fun resultTlCodec(): TlCodec = LiteServerMasterchainInfo 15 | } 16 | 17 | private object LiteServerGetMasterchainInfoTlConstructor : TlConstructor( 18 | schema = "liteServer.getMasterchainInfo = liteServer.MasterchainInfo" 19 | ) { 20 | override fun decode(reader: TlReader): LiteServerGetMasterchainInfo = LiteServerGetMasterchainInfo 21 | 22 | override fun encode(writer: TlWriter, value: LiteServerGetMasterchainInfo) { 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/functions/LiteServerGetTime.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver.functions 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.lite.api.liteserver.LiteServerCurrentTime 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @SerialName("liteServer.getTime") 10 | public object LiteServerGetTime : 11 | TLFunction, 12 | TlCodec by LiteServerGetTimeTLConstructor { 13 | override fun tlCodec(): TlCodec = LiteServerGetTimeTLConstructor 14 | override fun resultTlCodec(): TlCodec = LiteServerCurrentTime 15 | } 16 | 17 | private object LiteServerGetTimeTLConstructor : TlConstructor( 18 | schema = "liteServer.getTime = liteServer.CurrentTime" 19 | ) { 20 | override fun decode(reader: TlReader): LiteServerGetTime = LiteServerGetTime 21 | 22 | override fun encode(writer: TlWriter, value: LiteServerGetTime) { 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/functions/LiteServerGetVersion.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.api.liteserver.functions 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.lite.api.liteserver.LiteServerVersion 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @SerialName("liteServer.getVersion") 10 | public object LiteServerGetVersion : 11 | TLFunction, 12 | TlCodec by LiteServerGetVersionTlConstructor { 13 | 14 | override fun tlCodec(): TlCodec = LiteServerGetVersionTlConstructor 15 | 16 | override fun resultTlCodec(): TlCodec = LiteServerVersion 17 | } 18 | 19 | private object LiteServerGetVersionTlConstructor : TlConstructor( 20 | schema = "liteServer.getVersion = liteServer.Version" 21 | ) { 22 | override fun decode(reader: TlReader): LiteServerGetVersion { 23 | return LiteServerGetVersion 24 | } 25 | 26 | override fun encode(writer: TlWriter, value: LiteServerGetVersion) { 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /liteapi-tl/src/liteserver/functions/LiteServerQuery.kt: -------------------------------------------------------------------------------- 1 | @file:UseSerializers(HexByteArraySerializer::class) 2 | 3 | package org.ton.lite.api.liteserver.functions 4 | 5 | import kotlinx.io.bytestring.ByteString 6 | import kotlinx.serialization.SerialName 7 | import kotlinx.serialization.Serializable 8 | import kotlinx.serialization.UseSerializers 9 | import org.ton.crypto.HexByteArraySerializer 10 | import org.ton.tl.* 11 | import kotlin.jvm.JvmName 12 | 13 | @Serializable 14 | @SerialName("liteServer.query") 15 | public data class LiteServerQuery( 16 | @get:JvmName("data") 17 | @Serializable(ByteStringBase64Serializer::class) 18 | val data: ByteString 19 | ) { 20 | public companion object : TlCodec by LiteServerQueryTlConstructor 21 | } 22 | 23 | private object LiteServerQueryTlConstructor : TlConstructor( 24 | schema = "liteServer.query data:bytes = Object" 25 | ) { 26 | override fun decode(input: TlReader): LiteServerQuery { 27 | val data = input.readByteString() 28 | return LiteServerQuery(data) 29 | } 30 | 31 | override fun encode(output: TlWriter, value: LiteServerQuery) { 32 | output.writeBytes(value.data) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /liteclient/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinAdnl) 11 | api(projects.tonKotlinLiteapiTl) 12 | api(projects.tonKotlinBlockTlb) 13 | implementation(libs.atomicfu) 14 | } 15 | } 16 | jvmTest { 17 | dependencies { 18 | api(projects.tonKotlinAdnl) 19 | api(projects.tonKotlinLiteapiTl) 20 | api(projects.tonKotlinBlockTlb) 21 | implementation(libs.atomicfu) 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /liteclient/src/LiteClientApi.kt: -------------------------------------------------------------------------------- 1 | package org.ton.lite.client 2 | 3 | import org.ton.api.tonnode.TonNodeBlockIdExt 4 | import org.ton.block.MsgAddressInt 5 | import org.ton.lite.client.internal.FullAccountState 6 | import org.ton.lite.client.internal.TransactionId 7 | import org.ton.lite.client.internal.TransactionInfo 8 | 9 | public interface LiteClientApi { 10 | public suspend fun getAccountState( 11 | accountAddress: MsgAddressInt 12 | ): FullAccountState 13 | 14 | public suspend fun getAccountState( 15 | accountAddress: MsgAddressInt, 16 | blockId: TonNodeBlockIdExt 17 | ): FullAccountState 18 | 19 | public suspend fun getTransactions( 20 | accountAddress: MsgAddressInt, 21 | fromTransactionId: TransactionId, 22 | count: Int, 23 | ): List 24 | } 25 | -------------------------------------------------------------------------------- /liteclient/test@jvm/LiteClientIntegrationTest.kt: -------------------------------------------------------------------------------- 1 | import kotlinx.coroutines.Dispatchers 2 | import kotlinx.coroutines.runBlocking 3 | import org.ton.lite.client.LiteClient 4 | import kotlin.test.Test 5 | 6 | class LiteClientIntegrationTest { 7 | @Test 8 | fun integrationTestGetLastBlockId() = runBlocking { 9 | val liteClient = LiteClient(Dispatchers.Default, CONFIG) 10 | println(liteClient.getLastBlockId()) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tl/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinCrypto) 11 | api(projects.tonKotlinBitstring) 12 | api(libs.ktor.utils) 13 | api(libs.serialization.json) 14 | api(libs.kotlinx.io) 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tl/src/AbstractTlCombinator.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl 2 | 3 | import kotlin.reflect.KClass 4 | 5 | public abstract class AbstractTlCombinator : TlCodec { 6 | public abstract val baseClass: KClass 7 | 8 | override fun decode(reader: TlReader): T = decodeBoxed(reader) 9 | 10 | override fun decodeBoxed(reader: TlReader): T { 11 | val id = reader.readInt() 12 | val constructor = findConstructorOrNull(id) 13 | requireNotNull(constructor) { "Unknown constructor ID: $id" } 14 | return constructor.decode(reader) 15 | } 16 | 17 | override fun encode(writer: TlWriter, value: T) { 18 | encodeBoxed(writer, value) 19 | } 20 | 21 | override fun encodeBoxed(writer: TlWriter, value: T) { 22 | val constructor = findConstructorOrNull(value) 23 | requireNotNull(constructor) { "Unknown constructor for type: ${value::class}" } 24 | constructor.encodeBoxed(writer, value) 25 | } 26 | 27 | public abstract fun findConstructorOrNull(id: Int): TlDecoder? 28 | 29 | public abstract fun findConstructorOrNull(value: T): TlEncoder? 30 | } 31 | -------------------------------------------------------------------------------- /tl/src/ByteStringBase64Serializer.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.io.bytestring.decodeToByteString 5 | import kotlinx.io.bytestring.encode 6 | import kotlinx.serialization.KSerializer 7 | import kotlinx.serialization.descriptors.PrimitiveKind 8 | import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor 9 | import kotlinx.serialization.descriptors.SerialDescriptor 10 | import kotlinx.serialization.encoding.Decoder 11 | import kotlinx.serialization.encoding.Encoder 12 | import kotlin.io.encoding.Base64 13 | 14 | public object ByteStringBase64Serializer : KSerializer { 15 | override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ByteString", PrimitiveKind.STRING) 16 | 17 | override fun deserialize(decoder: Decoder): ByteString { 18 | return Base64.decodeToByteString(decoder.decodeString()) 19 | } 20 | 21 | override fun serialize(encoder: Encoder, value: ByteString) { 22 | encoder.encodeString(Base64.encode(value)) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tl/src/TlCodec.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl 2 | 3 | public interface TlCodec : TlDecoder, TlEncoder 4 | 5 | public interface TlObject where T : TlObject { 6 | public fun tlCodec(): TlCodec 7 | 8 | @Suppress("UNCHECKED_CAST") 9 | public fun hash(): ByteArray = (tlCodec() as TlCodec).hash(this as T) 10 | 11 | @Suppress("UNCHECKED_CAST") 12 | public fun toByteArray(): ByteArray { 13 | val codec = tlCodec() as TlCodec 14 | return codec.encodeToByteArray(this as T) 15 | } 16 | } 17 | 18 | public interface TLFunction { 19 | public fun tlCodec(): TlCodec 20 | public fun resultTlCodec(): TlCodec 21 | } 22 | -------------------------------------------------------------------------------- /tl/src/TlDecoder.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl 2 | 3 | import kotlinx.io.Buffer 4 | import kotlinx.io.Source 5 | import kotlinx.io.bytestring.ByteString 6 | import kotlinx.io.write 7 | 8 | public interface TlDecoder { 9 | public fun decode(byteArray: ByteArray): T = decode(Buffer().also { 10 | it.write(byteArray) 11 | }) 12 | 13 | public fun decode(byteString: ByteString): T = decode(Buffer().also { 14 | it.write(byteString) 15 | }) 16 | 17 | public fun decode(source: Source): T = decode(TlReader(source)) 18 | public fun decode(reader: TlReader): T 19 | 20 | public fun decodeBoxed(byteArray: ByteArray): T = decodeBoxed(Buffer().also { 21 | it.write(byteArray) 22 | }) 23 | 24 | public fun decodeBoxed(byteString: ByteString): T = decodeBoxed(Buffer().also { 25 | it.write(byteString) 26 | }) 27 | 28 | public fun decodeBoxed(source: Source): T = decodeBoxed(TlReader(source)) 29 | public fun decodeBoxed(reader: TlReader): T 30 | } 31 | -------------------------------------------------------------------------------- /tl/src/TlEncoder.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl 2 | 3 | import kotlinx.io.Buffer 4 | import kotlinx.io.Sink 5 | import kotlinx.io.bytestring.ByteString 6 | import kotlinx.io.readByteArray 7 | 8 | public interface TlEncoder { 9 | public fun encode(sink: Sink, value: T): Unit = encode(TlWriter(sink), value) 10 | public fun encode(writer: TlWriter, value: T) 11 | 12 | public fun encodeBoxed(sink: Sink, value: T): Unit = encodeBoxed(TlWriter(sink), value) 13 | public fun encodeBoxed(writer: TlWriter, value: T) 14 | public fun encodeToByteArray(value: T, boxed: Boolean = true): ByteArray { 15 | val buffer = Buffer() 16 | if (boxed) encodeBoxed(buffer, value) else encode(buffer, value) 17 | return buffer.readByteArray() 18 | } 19 | 20 | public fun encodeToByteString(value: T, boxed: Boolean = true): ByteString = 21 | ByteString(*encodeToByteArray(value, boxed)) 22 | 23 | public fun hash(value: T): ByteArray = 24 | io.github.andreypfau.kotlinx.crypto.sha2.sha256(encodeToByteArray(value)) 25 | } 26 | 27 | @Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE") 28 | internal inline fun TlEncoder<*>.cast(): TlEncoder = this as TlEncoder 29 | -------------------------------------------------------------------------------- /tl/src/constructors/BoolTlConstructor.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl.constructors 2 | 3 | internal enum class Bool(val value: Boolean) { 4 | TRUE(true), 5 | FALSE(false); 6 | 7 | companion object { 8 | operator fun get(value: Boolean) = if (value) TRUE else FALSE 9 | } 10 | } 11 | 12 | internal object BoolTlCombinator : EnumTlCombinator( 13 | Bool::class, 14 | Bool.TRUE to "boolTrue = Bool", 15 | Bool.FALSE to "boolFalse = Bool" 16 | ) 17 | -------------------------------------------------------------------------------- /tl/src/constructors/BytesTlConstructor.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.tl.constructors 4 | 5 | import kotlinx.io.bytestring.ByteString 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | 10 | public object BytesTlConstructor : TlConstructor( 11 | schema = "bytes data:string = Bytes" 12 | ) { 13 | public fun sizeOf(value: ByteString): Int = sizeOf(value) 14 | 15 | public fun sizeOf(value: ByteArray): Int { 16 | var size = value.size 17 | size += if (size < 254) { 18 | 1 19 | } else if (size < (1 shl 24)) { 20 | 4 21 | } else { 22 | 8 23 | } 24 | size += size % 4 25 | return size 26 | } 27 | 28 | override fun decode(reader: TlReader): ByteArray { 29 | return reader.readBytes() 30 | } 31 | 32 | override fun encode(writer: TlWriter, value: ByteArray) { 33 | return writer.writeBytes(value) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tl/src/constructors/EnumTlCombinator.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tl.constructors 2 | 3 | import org.ton.tl.* 4 | import kotlin.reflect.KClass 5 | 6 | public open class EnumTlCombinator>( 7 | override val baseClass: KClass, 8 | values: List> 9 | ) : AbstractTlCombinator() { 10 | public constructor(baseClass: KClass, vararg values: Pair) : this(baseClass, values.toList()) 11 | 12 | private val enum2Constructor: Map> 13 | private val constructor2Enum: Map> 14 | 15 | init { 16 | enum2Constructor = values.asSequence().map { 17 | it.first to EnumConstructor(it.first, it.second) 18 | }.toMap() 19 | constructor2Enum = enum2Constructor.values.associateBy { it.id } 20 | } 21 | 22 | private class EnumConstructor>( 23 | val enum: T, 24 | schema: String 25 | ) : TlConstructor(schema) { 26 | override fun decode(reader: TlReader): T = enum 27 | 28 | override fun encode(writer: TlWriter, value: T) = Unit 29 | } 30 | 31 | override fun findConstructorOrNull(id: Int): TlDecoder? = constructor2Enum[id] 32 | 33 | override fun findConstructorOrNull(value: T): TlEncoder? = enum2Constructor[value]?.cast() 34 | } 35 | -------------------------------------------------------------------------------- /tlb/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinTvm) 11 | implementation(libs.atomicfu) 12 | implementation(kotlin("reflect")) 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tlb/src/AbstractTlbCombinator.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb 2 | 3 | import kotlin.reflect.KClass 4 | 5 | public abstract class AbstractTlbCombinator : TlbCodec { 6 | public abstract val baseClass: KClass 7 | } 8 | -------------------------------------------------------------------------------- /tlb/src/TlbObject.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb 2 | 3 | public interface TlbObject { 4 | public fun print(printer: TlbPrettyPrinter = TlbPrettyPrinter()): TlbPrettyPrinter 5 | } 6 | -------------------------------------------------------------------------------- /tlb/src/exception/ParseTlbException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb.exception 2 | 3 | public class ParseTlbException(message: String, cause: Throwable?) : RuntimeException(message, cause) 4 | -------------------------------------------------------------------------------- /tlb/src/exception/UnknownTlbConstructorException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb.exception 2 | 3 | import org.ton.bitstring.BitString 4 | 5 | public class UnknownTlbConstructorException( 6 | public val id: BitString? = null 7 | ) : IllegalArgumentException(if (id != null) "Unknown constructor: $id (${id.toBinary()})" else "Unknown constructor") 8 | -------------------------------------------------------------------------------- /tlb/src/providers/TlbCombinatorProvider.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb.providers 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.cell.CellSlice 5 | import org.ton.kotlin.cell.CellContext 6 | import org.ton.tlb.TlbCodec 7 | import org.ton.tlb.TlbCombinator 8 | 9 | public interface TlbCombinatorProvider : TlbProvider, TlbCodec { 10 | public fun tlbCombinator(): TlbCombinator 11 | 12 | override fun loadTlb(slice: CellSlice, context: CellContext): T = 13 | tlbCombinator().loadTlb(slice, context) 14 | 15 | override fun storeTlb(builder: CellBuilder, value: T, context: CellContext): Unit = 16 | tlbCombinator().storeTlb(builder, value, context) 17 | } 18 | -------------------------------------------------------------------------------- /tlb/src/providers/TlbConstructorProvider.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb.providers 2 | 3 | import org.ton.cell.CellBuilder 4 | import org.ton.kotlin.cell.CellContext 5 | import org.ton.tlb.TlbCodec 6 | import org.ton.tlb.TlbConstructor 7 | 8 | public interface TlbConstructorProvider : TlbCodec { 9 | public fun tlbConstructor(): TlbConstructor 10 | 11 | override fun storeTlb(builder: CellBuilder, value: T, context: CellContext) { 12 | tlbConstructor().storeTlb(builder, value) 13 | } 14 | 15 | override fun storeTlb(builder: CellBuilder, value: T) { 16 | tlbConstructor().storeTlb(builder, value, CellContext.EMPTY) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tlb/src/providers/TlbProvider.kt: -------------------------------------------------------------------------------- 1 | package org.ton.tlb.providers 2 | 3 | import org.ton.tlb.TlbCodec 4 | 5 | public sealed interface TlbProvider : TlbCodec 6 | -------------------------------------------------------------------------------- /ton-kotlin-dht/build.gradle.kts: -------------------------------------------------------------------------------- 1 | kotlin { 2 | sourceSets { 3 | val commonMain by getting { 4 | dependencies { 5 | api(projects.tonKotlinAdnl) 6 | } 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ton-kotlin-dht/src/commonMain/kotlin/org/ton/dht/Dht.kt: -------------------------------------------------------------------------------- 1 | package org.ton.dht 2 | 3 | import org.ton.api.adnl.AdnlIdShort 4 | import org.ton.api.dht.config.DhtConfigGlobal 5 | import org.ton.dht.storage.DhtStorage 6 | import kotlin.coroutines.CoroutineContext 7 | 8 | class Dht { 9 | companion object { 10 | fun client( 11 | id: AdnlIdShort, 12 | storage: DhtStorage, 13 | config: DhtConfigGlobal, 14 | coroutineContext: CoroutineContext 15 | ) { 16 | require(config.k > 0) 17 | require(config.a > 0) 18 | 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ton-kotlin-dht/src/commonMain/kotlin/org/ton/dht/DhtBucket.kt: -------------------------------------------------------------------------------- 1 | package org.ton.dht 2 | 3 | import org.ton.api.dht.DhtNode 4 | import org.ton.bitstring.BitString 5 | 6 | class DhtBucket( 7 | k: Int 8 | ) { 9 | private val activeNodes = ArrayList(k) 10 | private val backupNodes = ArrayList(k) 11 | 12 | public val activeCount get() = activeNodes.size 13 | 14 | public fun getNearestNode( 15 | id: Bits256, 16 | k: Int 17 | ): List { 18 | if (activeNodes.size == 0) return emptyList() 19 | 20 | val map = HashMap() 21 | for (i in activeNodes.indices) { 22 | val node = activeNodes[i] 23 | val distance = id xor node.key.id 24 | map[distance] = i 25 | } 26 | 27 | return map.entries.asSequence() 28 | .sortedBy { it.key } 29 | .map { activeNodes[it.value].dhtNode } 30 | .take(k) 31 | .toList() 32 | } 33 | 34 | public companion object { 35 | public const val PING_TIMEOUT_MS = 60_000L 36 | public const val MAX_MISSED_PINGS = 3L 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /ton-kotlin-dht/src/commonMain/kotlin/org/ton/dht/DhtMember.kt: -------------------------------------------------------------------------------- 1 | package org.ton.dht 2 | 3 | import kotlinx.coroutines.CoroutineName 4 | import kotlinx.coroutines.CoroutineScope 5 | import org.ton.api.adnl.AdnlIdShort 6 | import org.ton.api.dht.config.DhtConfigGlobal 7 | import org.ton.dht.storage.DhtStorage 8 | import kotlin.coroutines.CoroutineContext 9 | 10 | class DhtMember( 11 | val id: AdnlIdShort, 12 | val storage: DhtStorage, 13 | val config: DhtConfigGlobal, 14 | coroutineContext: CoroutineContext 15 | ) : CoroutineScope { 16 | override val coroutineContext: CoroutineContext = coroutineContext + CoroutineName(toString()) 17 | 18 | override fun toString(): String = "[dhtnode $id]" 19 | 20 | companion object { 21 | const val DEFAULT_K = 10 22 | const val DEFAULT_A = 3 23 | const val MAX_K = 10 24 | const val MAX_A = 10 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ton-kotlin-dht/src/commonMain/kotlin/org/ton/dht/DhtRemoteNode.kt: -------------------------------------------------------------------------------- 1 | package org.ton.dht 2 | 3 | import org.ton.api.dht.DhtNode 4 | 5 | class DhtRemoteNode( 6 | val dhtNode: DhtNode 7 | ) { 8 | val key get() = dhtNode.key() 9 | 10 | } 11 | -------------------------------------------------------------------------------- /ton-kotlin-dht/src/commonMain/kotlin/org/ton/dht/storage/DhtStorage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.dht.storage 2 | 3 | interface DhtStorage { 4 | } 5 | -------------------------------------------------------------------------------- /ton-kotlin-dht/src/commonMain/kotlin/org/ton/dht/storage/HeapDhtStorage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.dht.storage 2 | 3 | class HeapDhtStorage : DhtStorage { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/build.gradle.kts: -------------------------------------------------------------------------------- 1 | kotlin { 2 | sourceSets { 3 | val commonMain by getting { 4 | dependencies { 5 | api(projects.tonKotlinLiteclient) 6 | api(libs.ktor.server.cio) 7 | api(libs.ktor.client.cio) 8 | api(libs.serialization.json) 9 | api(libs.atomicfu) 10 | api("io.github.reactivecircus.cache4k:cache4k:0.8.0") 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/AdnlPacketReceiver.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl 2 | 3 | import org.ton.api.adnl.AdnlPacketContents 4 | 5 | interface AdnlPacketReceiver : AdnlMessageReceiver { 6 | fun receivePacket(packet: AdnlPacketContents) { 7 | packet.messages().forEach { 8 | receiveAdnlMessage(it) 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/PacketsHistory.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl 2 | 3 | import kotlinx.atomicfu.atomic 4 | 5 | class PacketsHistory private constructor( 6 | val deliveredSet: ArrayDeque?, 7 | seqno: Long, 8 | ) { 9 | private val _seqno = atomic(seqno) 10 | var seqno 11 | get() = _seqno.value 12 | set(value) { 13 | _seqno.value = value 14 | } 15 | 16 | fun reset() { 17 | deliveredSet?.clear() 18 | seqno = 0 19 | } 20 | 21 | fun deliverPacket(seqno: Long): Boolean { 22 | if (deliveredSet == null) { 23 | val lastSeqno = this.seqno 24 | if (lastSeqno < seqno) { 25 | this.seqno = seqno 26 | } 27 | return true 28 | } 29 | // TODO: use mask for optimization 30 | if (deliveredSet.contains(seqno)) { 31 | return false 32 | } 33 | deliveredSet.addLast(seqno) 34 | if (deliveredSet.size == 512) { 35 | deliveredSet.removeFirst() 36 | } 37 | return true 38 | } 39 | 40 | companion object { 41 | fun sender() = PacketsHistory(null, 0L) 42 | fun receiver() = PacketsHistory(ArrayDeque(512), 0L) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/PeerState.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl 2 | 3 | import kotlinx.datetime.Clock 4 | import kotlinx.datetime.Instant 5 | import java.util.concurrent.atomic.AtomicReference 6 | 7 | class PeerState private constructor( 8 | val ordinaryHistory: PacketsHistory, 9 | val priorityHistory: PacketsHistory, 10 | reinitDate: Instant 11 | ) { 12 | private val _reinitDate = AtomicReference(reinitDate) 13 | var reinitDate 14 | get() = _reinitDate.get() 15 | set(value) { 16 | _reinitDate.set(value) 17 | } 18 | 19 | companion object { 20 | fun receiver(reinitDate: Instant = Clock.System.now()) = 21 | PeerState( 22 | ordinaryHistory = PacketsHistory.receiver(), 23 | priorityHistory = PacketsHistory.receiver(), 24 | reinitDate = reinitDate 25 | ) 26 | 27 | fun sender() = PeerState( 28 | ordinaryHistory = PacketsHistory.sender(), 29 | priorityHistory = PacketsHistory.sender(), 30 | reinitDate = Instant.DISTANT_PAST 31 | ) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/channel/AdnlInputChannel.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("NOTHING_TO_INLINE") 2 | 3 | package org.ton.proxy.adnl.channel 4 | 5 | import org.ton.api.adnl.AdnlIdShort 6 | import org.ton.api.pk.PrivateKeyAes 7 | import org.ton.bitstring.BitString 8 | import kotlin.jvm.JvmStatic 9 | 10 | inline fun AdnlInputChannel(key: PrivateKeyAes): AdnlInputChannel = AdnlInputChannel.of(key) 11 | 12 | interface AdnlInputChannel { 13 | val id: AdnlIdShort 14 | val key: PrivateKeyAes 15 | 16 | companion object { 17 | @JvmStatic 18 | fun of(key: PrivateKeyAes): AdnlInputChannel = AdnlInputChannelImpl(key) 19 | } 20 | } 21 | 22 | private data class AdnlInputChannelImpl( 23 | override val key: PrivateKeyAes, 24 | ) : AdnlInputChannel { 25 | override val id: AdnlIdShort get() = key.toAdnlIdShort() 26 | } 27 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/channel/AdnlOutputChannel.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("NOTHING_TO_INLINE") 2 | 3 | package org.ton.proxy.adnl.channel 4 | 5 | import org.ton.api.adnl.AdnlIdShort 6 | import org.ton.api.pub.PublicKeyAes 7 | import org.ton.bitstring.BitString 8 | import kotlin.jvm.JvmStatic 9 | 10 | inline fun AdnlOutputChannel(key: PublicKeyAes): AdnlOutputChannel = AdnlOutputChannel.of(key) 11 | 12 | interface AdnlOutputChannel { 13 | val id: AdnlIdShort 14 | val key: PublicKeyAes 15 | 16 | companion object { 17 | @JvmStatic 18 | fun of(key: PublicKeyAes): AdnlOutputChannel = AdnlOutputChannelImpl(key) 19 | } 20 | } 21 | 22 | private data class AdnlOutputChannelImpl( 23 | override val key: PublicKeyAes, 24 | ) : AdnlOutputChannel { 25 | override val id get() = key.toAdnlIdShort() 26 | } 27 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/engine/AdnlNetworkEngine.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl.engine 2 | 3 | import io.ktor.utils.io.core.* 4 | import org.ton.api.adnl.AdnlAddressUdp 5 | 6 | interface AdnlNetworkEngine { 7 | suspend fun sendDatagram(adnlAddress: AdnlAddressUdp, payload: ByteReadPacket) 8 | suspend fun receiveDatagram(): Pair 9 | } 10 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/resolver/AdnlAddressResolver.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl.resolver 2 | 3 | import org.ton.api.adnl.AdnlAddress 4 | import org.ton.api.adnl.AdnlAddressList 5 | import org.ton.api.adnl.AdnlIdShort 6 | import org.ton.api.pub.PublicKey 7 | 8 | fun interface AdnlAddressResolver { 9 | suspend fun resolve(publicKey: PublicKey): List? = resolve(publicKey.toAdnlIdShort())?.second 10 | suspend fun resolve(address: AdnlIdShort): Pair>? 11 | } 12 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/resolver/CachedAdnlAddressResolver.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl.resolver 2 | 3 | import io.github.reactivecircus.cache4k.Cache 4 | import org.ton.api.adnl.AdnlAddress 5 | import org.ton.api.adnl.AdnlIdShort 6 | import org.ton.api.pub.PublicKey 7 | import kotlin.time.Duration 8 | 9 | class CachedAdnlAddressResolver( 10 | private val delegate: AdnlAddressResolver, 11 | ttl: Duration 12 | ) : AdnlAddressResolver { 13 | val cache = Cache.Builder() 14 | .expireAfterWrite(ttl) 15 | .build>>() 16 | 17 | override suspend fun resolve(address: AdnlIdShort): Pair>? { 18 | val cached = cache.get(address) 19 | if (cached != null) return cached 20 | val resolved = delegate.resolve(address) 21 | if (resolved != null) { 22 | cache.put(address, resolved) 23 | } 24 | return resolved 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/adnl/resolver/MapAdnlAddressResolver.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.adnl.resolver 2 | 3 | import org.ton.api.adnl.AdnlAddress 4 | import org.ton.api.adnl.AdnlIdShort 5 | import org.ton.api.pub.PublicKey 6 | 7 | class MapAdnlAddressResolver( 8 | private val map: Map>> 9 | ) : AdnlAddressResolver { 10 | override suspend fun resolve(address: AdnlIdShort): Pair>? = map[address] 11 | } 12 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/state/DhtState.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.state 2 | 3 | import org.ton.api.adnl.AdnlIdShort 4 | import org.ton.proxy.dht.DhtPeer 5 | import org.ton.proxy.dht.storage.DhtStorage 6 | import kotlin.jvm.JvmStatic 7 | 8 | interface DhtState { 9 | val knownPeers: Set 10 | val badPeers: Map 11 | fun addPeer(peer: DhtPeer): AdnlIdShort 12 | fun updatePeerStatus(peer: AdnlIdShort, isGood: Boolean) 13 | fun setGoodPeer(peer: AdnlIdShort) 14 | 15 | companion object { 16 | @JvmStatic 17 | fun full( 18 | localId: AdnlIdShort, 19 | knownPeers: Collection, 20 | storage: DhtStorage 21 | ) = FullDhtState(localId, knownPeers.toMutableSet(), storage) 22 | 23 | @JvmStatic 24 | fun lite( 25 | knownPeers: Collection 26 | ) = LiteDhtState(knownPeers.toMutableSet()) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/state/FullDhtState.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.state 2 | 3 | import org.ton.api.adnl.AdnlIdShort 4 | import org.ton.proxy.dht.DhtBucket 5 | import org.ton.proxy.dht.DhtPeer 6 | import org.ton.proxy.dht.storage.DhtStorage 7 | 8 | class FullDhtState( 9 | localId: AdnlIdShort, 10 | knownPeers: MutableSet, 11 | override val storage: DhtStorage 12 | ) : AbstractDhtState(knownPeers) { 13 | override val buckets: DhtBucket = DhtBucket(localId) 14 | } 15 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/state/LiteDhtState.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.state 2 | 3 | import org.ton.proxy.dht.DhtBucket 4 | import org.ton.proxy.dht.DhtPeer 5 | import org.ton.proxy.dht.storage.DhtStorage 6 | 7 | class LiteDhtState( 8 | knownPeers: MutableSet = HashSet() 9 | ) : AbstractDhtState(knownPeers) { 10 | override val buckets: DhtBucket? get() = null 11 | override val storage: DhtStorage? get() = null 12 | } 13 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/storage/DhtStorage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.storage 2 | 3 | import org.ton.api.dht.DhtValue 4 | import org.ton.bitstring.BitString 5 | 6 | interface DhtStorage { 7 | val config: DhtStorageConfig 8 | 9 | fun gc() 10 | 11 | fun put(value: DhtValue): Boolean 12 | fun get(bitString: BitString): DhtValue? 13 | } 14 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/storage/DhtStorageConfig.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.storage 2 | 3 | import kotlinx.serialization.Serializable 4 | import org.ton.proxy.dht.DhtConfig 5 | 6 | @Serializable 7 | data class DhtStorageConfig( 8 | val maxKeyNameLength: Int, 9 | val maxKeyIndex: Int, 10 | val allowUnsignedValues: Boolean = false 11 | ) { 12 | constructor(dhtConfig: DhtConfig) : this( 13 | maxKeyNameLength = dhtConfig.maxKeyNameLength, 14 | maxKeyIndex = dhtConfig.maxKeyIndex 15 | ) 16 | } 17 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/storage/InMemoryDhtStorage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.storage 2 | 3 | import kotlinx.datetime.Clock 4 | import org.ton.api.dht.DhtValue 5 | import org.ton.bitstring.BitString 6 | 7 | class InMemoryDhtStorage( 8 | config: DhtStorageConfig, 9 | val map: MutableMap = HashMap() 10 | ) : AbstractDhtStorage(config) { 11 | override fun putUnchecked(key: BitString, value: DhtValue) { 12 | map[key] = value 13 | } 14 | 15 | override fun getUnchecked(key: BitString): DhtValue? { 16 | return map[key] 17 | } 18 | 19 | override fun gc() { 20 | val now = Clock.System.now() 21 | map.entries.removeAll { it.value.ttl() <= now } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dht/storage/exception/DhtStorageException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dht.storage.exception 2 | 3 | import org.ton.api.dht.DhtValue 4 | 5 | sealed class DhtStorageException( 6 | val value: DhtValue, 7 | override val message: String 8 | ) : RuntimeException() { 9 | class UnsupportedUpdateRule( 10 | value: DhtValue, 11 | ) : DhtStorageException(value, "Unsupported update rule") 12 | 13 | class InvalidSignatureValue( 14 | value: DhtValue, 15 | ) : DhtStorageException(value, "Invalid signature value") 16 | 17 | class InvalidKeyDescription( 18 | value: DhtValue, 19 | ) : DhtStorageException(value, "Invalid key description") 20 | 21 | class InvalidDhtKey( 22 | value: DhtValue, 23 | ) : DhtStorageException(value, "Invalid dht key") 24 | 25 | class EmptyOverlayNodes( 26 | value: DhtValue, 27 | ) : DhtStorageException(value, "Empty overlay nodes") 28 | 29 | class ValueExpired( 30 | value: DhtValue, 31 | ) : DhtStorageException(value, "Value expired") 32 | 33 | class InvalidKey( 34 | value: DhtValue, 35 | ) : DhtStorageException(value, "Invalid key") 36 | } 37 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/dns/DnsCategory.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.dns 2 | 3 | import io.ktor.utils.io.core.* 4 | import org.ton.bigint.BigInt 5 | import org.ton.crypto.sha256.sha256 6 | 7 | enum class DnsCategory( 8 | val value: BigInt 9 | ) { 10 | WALLET("wallet"), 11 | SITE("site"); 12 | 13 | constructor(name: String) : this(BigInt(sha256(name.toByteArray()))) 14 | } 15 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/http/HttpProxy.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.http 2 | 3 | import org.ton.api.pk.PrivateKey 4 | import org.ton.api.pk.PrivateKeyEd25519 5 | import org.ton.api.pub.PublicKey 6 | import org.ton.api.pub.PublicKeyEd25519 7 | import org.ton.proxy.adnl.engine.AdnlNetworkEngine 8 | import org.ton.proxy.adnl.resolver.AdnlAddressResolver 9 | import org.ton.proxy.rldp.AbstractRldpPeerSession 10 | import org.ton.proxy.rldp.Rldp 11 | import org.ton.proxy.rldp.RldpPeerSession 12 | 13 | class HttpProxy( 14 | networkEngine: AdnlNetworkEngine, 15 | addressResolver: AdnlAddressResolver 16 | ) : Rldp( 17 | networkEngine, 18 | addressResolver 19 | ) { 20 | override fun createPeer(remoteKey: PublicKey, localKey: PrivateKey): RldpPeerSession { 21 | require(remoteKey is PublicKeyEd25519) 22 | require(localKey is PrivateKeyEd25519) 23 | return object : AbstractRldpPeerSession(this@HttpProxy, localKey, remoteKey) {} 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/http/HttpProxyPeerSession.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.http 2 | 3 | import org.ton.api.pk.PrivateKeyEd25519 4 | import org.ton.api.pub.PublicKeyEd25519 5 | import org.ton.proxy.rldp.AbstractRldpPeerSession 6 | import org.ton.proxy.rldp.RldpPeerSession 7 | 8 | interface HttpProxyPeerSession : RldpPeerSession { 9 | val httpProxy: HttpProxy 10 | } 11 | 12 | abstract class AbstractHttpProxyPeerSession( 13 | override val httpProxy: HttpProxy, 14 | localKey: PrivateKeyEd25519, 15 | remoteKey: PublicKeyEd25519 16 | ) : AbstractRldpPeerSession(httpProxy, localKey, remoteKey), HttpProxyPeerSession { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/http/headers.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.http 2 | 3 | import io.ktor.http.* 4 | import org.ton.api.http.HttpHeader 5 | 6 | fun Iterable.ktor() = Headers.build { 7 | this@ktor.forEach { 8 | append(it.name, it.value) 9 | } 10 | } 11 | 12 | fun Headers.ton() = entries().map { (key, values) -> 13 | values.map { value -> 14 | HttpHeader(key, value) 15 | } 16 | }.flatten() 17 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/Rldp.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp 2 | 3 | import org.ton.api.adnl.AdnlIdShort 4 | import org.ton.api.pk.PrivateKey 5 | import org.ton.api.pk.PrivateKeyEd25519 6 | import org.ton.api.pub.PublicKey 7 | import org.ton.api.pub.PublicKeyEd25519 8 | import org.ton.proxy.adnl.Adnl 9 | import org.ton.proxy.adnl.engine.AdnlNetworkEngine 10 | import org.ton.proxy.adnl.resolver.AdnlAddressResolver 11 | import kotlin.time.Duration 12 | 13 | open class Rldp( 14 | networkEngine: AdnlNetworkEngine, 15 | addressResolver: AdnlAddressResolver 16 | ) : Adnl(networkEngine, addressResolver) { 17 | override fun createPeer(remoteKey: PublicKey, localKey: PrivateKey): RldpPeerSession { 18 | require(remoteKey is PublicKeyEd25519) 19 | require(localKey is PrivateKeyEd25519) 20 | return object : AbstractRldpPeerSession(this@Rldp, localKey, remoteKey) {} 21 | } 22 | 23 | override suspend fun message(destination: AdnlIdShort, payload: ByteArray) = 24 | getPeer(destination).sendMessage(payload) 25 | 26 | override suspend fun query( 27 | destination: AdnlIdShort, 28 | payload: ByteArray, 29 | timeout: Duration, 30 | maxAnswerSize: Long 31 | ): ByteArray = getPeer(destination).query(payload, timeout, maxAnswerSize) 32 | } 33 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/RldpReceiver.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp 2 | 3 | import org.ton.api.rldp.RldpMessagePart 4 | 5 | interface RldpReceiver { 6 | fun receiveRldpMessagePart(message: RldpMessagePart) 7 | } 8 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/fec/FecDecoder.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp.fec 2 | 3 | import org.ton.api.fec.FecType 4 | 5 | interface FecDecoder { 6 | val fecType: FecType 7 | val isComplete: Boolean 8 | 9 | fun addSymbol(seqno: Int, data: ByteArray): Boolean 10 | fun decode(output: ByteArray): Boolean 11 | fun decode(): ByteArray? = if (isComplete) { 12 | val output = ByteArray(fecType.data_size) 13 | decode(output) 14 | output 15 | } else null 16 | } 17 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/fec/FecEncoder.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp.fec 2 | 3 | import org.ton.api.fec.FecType 4 | 5 | interface FecEncoder { 6 | val fecType: FecType 7 | 8 | fun encode(seqno: Int): ByteArray = encode(seqno, ByteArray(fecType.symbol_size)) 9 | 10 | fun encode(seqno: Int, output: ByteArray): ByteArray 11 | } 12 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/fec/raptorq/Deg.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp.fec.raptorq 2 | 3 | import kotlin.math.min 4 | 5 | private val DEGREE_DISTRIBUTION = intArrayOf( 6 | 0, 5243, 529531, 704294, 791675, 844104, 879057, 904023, 922747, 937311, 948962, 7 | 958494, 966438, 973160, 978921, 983914, 988283, 992138, 995565, 998631, 1001391, 1003887, 8 | 1006157, 1008229, 1010129, 1011876, 1013490, 1014983, 1016370, 1017662, 1048576, 9 | ) 10 | 11 | internal fun deg(v: Int, w: Int): Long { 12 | for (d in DEGREE_DISTRIBUTION.indices) { 13 | if (v < DEGREE_DISTRIBUTION[d]) { 14 | return min(d, w - 2).toLong() 15 | } 16 | } 17 | return 0L 18 | } 19 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/fec/raptorq/Symbol.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp.fec.raptorq 2 | 3 | import kotlin.jvm.JvmStatic 4 | import kotlin.math.min 5 | 6 | class Symbol( 7 | val id: Int, 8 | val data: ByteArray 9 | ) { 10 | companion object { 11 | @JvmStatic 12 | fun fromBytes(data: ByteArray, symbolsCount: Int, symbolsSize: Int): Array = 13 | Array(symbolsCount) { id -> 14 | val offset = id * symbolsSize 15 | val symbolData = ByteArray(symbolsSize) 16 | if (offset < data.size) { 17 | data.copyInto(symbolData, 0, offset, min(offset + symbolsSize, data.size)) 18 | } 19 | Symbol(id, symbolData) 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/fec/raptorq/math/GF256.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp.fec.raptorq.math 2 | 3 | import kotlin.experimental.xor 4 | 5 | class GF256( 6 | val data: ByteArray 7 | ) { 8 | constructor(size: Int) : this(ByteArray(size)) 9 | 10 | fun add(other: GF256) { 11 | for (i in data.indices) { 12 | data[i] = (data[i] xor other.data[i]) 13 | } 14 | } 15 | 16 | fun mul(x: Octet) { 17 | for (i in data.indices) { 18 | data[i] = (data[i].toOctet() * x).toByte() 19 | } 20 | } 21 | 22 | fun addMul(other: GF256, x: Octet) { 23 | for (i in data.indices) { 24 | data[i] = (data[i].toOctet() + (x * other.data[i].toOctet())).toByte() 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/commonMain/kotlin/org/ton/proxy/rldp/fec/raptorq/math/MatrixGF2.kt: -------------------------------------------------------------------------------- 1 | package org.ton.proxy.rldp.fec.raptorq.math 2 | 3 | import kotlin.experimental.xor 4 | 5 | class MatrixGF2( 6 | val data: Array 7 | ) { 8 | constructor(rows: Int, cols: Int) : this(Array(rows) { ByteArray(cols) }) 9 | 10 | val rows get() = data.size 11 | val cols get() = data[0].size 12 | 13 | fun rowAdd(rows: Int, value: ByteArray) { 14 | for (i in 0 until cols) { 15 | data[rows][i] = (data[rows][i] xor value[i]) 16 | } 17 | } 18 | 19 | operator fun set(row: Int, col: Int, value: Boolean) { 20 | data[row][col] = if (value) 1 else 0 21 | } 22 | 23 | operator fun get(row: Int, col: Int): Boolean = 24 | data[row][col] > 0.toByte() 25 | 26 | fun getRow(row: Int) = data[row] 27 | 28 | fun mul(s: SparseMatrixGF2): MatrixGF2 { 29 | val result = MatrixGF2(s.rows, cols) 30 | s.forEach { (row, col) -> 31 | result.rowAdd(row, getRow(col)) 32 | } 33 | return result 34 | } 35 | 36 | fun toGF256(): MatrixGF256 { 37 | val result = MatrixGF256(rows, cols) 38 | for (i in data.indices) { 39 | result.data[i] = GF256(data[i]) 40 | } 41 | return result 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ton-kotlin-experimental/src/jvmTest/kotlin/org/ton/experimental/Proxy.kt: -------------------------------------------------------------------------------- 1 | package org.ton.experimental 2 | 3 | import io.ktor.client.* 4 | import io.ktor.client.engine.cio.* 5 | import io.ktor.client.request.* 6 | import io.ktor.client.statement.* 7 | import kotlinx.serialization.decodeFromString 8 | import kotlinx.serialization.json.Json 9 | import org.ton.proxy.ProxyClient 10 | 11 | suspend fun main() { 12 | val file = HttpClient(CIO).get("https://ton.org/global-config.json").readBytes().decodeToString() 13 | val proxy = 14 | ProxyClient( 15 | Json { 16 | ignoreUnknownKeys = true 17 | }.decodeFromString(file) 18 | ).start(true) 19 | } 20 | -------------------------------------------------------------------------------- /ton-kotlin-fift/build.gradle.kts: -------------------------------------------------------------------------------- 1 | kotlin { 2 | sourceSets { 3 | val commonMain by getting { 4 | dependencies { 5 | implementation(projects.tonKotlinLogger) 6 | implementation(projects.tonKotlinBigint) 7 | implementation(projects.tonKotlinBoc) 8 | implementation(libs.ktor.utils) 9 | implementation(libs.serialization.json) 10 | } 11 | } 12 | val commonTest by getting { 13 | dependencies { 14 | implementation(kotlin("test")) 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ton-kotlin-fift/src/commonMain/kotlin/org/ton/fift/Box.kt: -------------------------------------------------------------------------------- 1 | package org.ton.fift 2 | 3 | public data class Box( 4 | var value: Any = Unit 5 | ) 6 | -------------------------------------------------------------------------------- /ton-kotlin-fift/src/commonMain/kotlin/org/ton/fift/exceptions.kt: -------------------------------------------------------------------------------- 1 | package org.ton.fift 2 | 3 | // TODO: exception code 4 | open class FiftException( 5 | exceptionCode: Int, 6 | message: String? = null, 7 | ) : Exception("Fift Exception: $exceptionCode${if (!message.isNullOrBlank()) ", $message" else ""}") 8 | 9 | class FiftStackOverflow(message: String? = null) : FiftException(0, message) 10 | -------------------------------------------------------------------------------- /ton-kotlin-fift/src/commonMain/kotlin/org/ton/fift/utils.kt: -------------------------------------------------------------------------------- 1 | package org.ton.fift 2 | 3 | internal fun Any.fiftFormat() = if (this is String) "\"$this\"" else toString() 4 | -------------------------------------------------------------------------------- /ton-kotlin-rldp/build.gradle.kts: -------------------------------------------------------------------------------- 1 | kotlin { 2 | sourceSets { 3 | val commonMain by getting { 4 | dependencies { 5 | api(projects.tonKotlinAdnl) 6 | api(projects.tonKotlinLogger) 7 | } 8 | } 9 | val jvmTest by getting { 10 | dependencies { 11 | implementation ("io.ktor:ktor-server-cio:2.2.4") 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tonapi-tl/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinBigint) 11 | api(projects.tonKotlinTl) 12 | api(projects.tonKotlinBitstring) 13 | api(projects.tonKotlinTlb) 14 | api(libs.datetime) 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tonapi-tl/src/SignedTlObject.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import org.ton.api.pk.PrivateKey 5 | import org.ton.api.pub.PublicKey 6 | import org.ton.tl.TlObject 7 | 8 | public interface SignedTlObject> : TlObject { 9 | public val signature: ByteString? 10 | 11 | public fun signed(privateKey: PrivateKey): T 12 | 13 | public fun verify(publicKey: PublicKey): Boolean 14 | } 15 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/AdnlNode.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.pub.PublicKey 6 | import org.ton.tl.* 7 | import kotlin.jvm.JvmName 8 | 9 | @Serializable 10 | @SerialName("adnl.node") 11 | public data class AdnlNode( 12 | @get:JvmName("id") 13 | val id: PublicKey, 14 | 15 | @SerialName("addr_list") 16 | @get:JvmName("addrList") 17 | val addrList: AdnlAddressList 18 | ) { 19 | public companion object : TlConstructor( 20 | schema = "adnl.node id:PublicKey addr_list:adnl.addressList = adnl.Node" 21 | ) { 22 | override fun encode(writer: TlWriter, value: AdnlNode) { 23 | writer.write(PublicKey, value.id) 24 | writer.write(AdnlAddressList, value.addrList) 25 | } 26 | 27 | override fun decode(reader: TlReader): AdnlNode { 28 | val id = reader.read(PublicKey) 29 | val addrList = reader.read(AdnlAddressList) 30 | return AdnlNode(id, addrList) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/AdnlNodes.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.* 6 | import kotlin.jvm.JvmName 7 | 8 | @Serializable 9 | @SerialName("adnl.nodes") 10 | public data class AdnlNodes( 11 | @get:JvmName("nodes") 12 | val nodes: List = emptyList() 13 | ) { 14 | public companion object : TlCodec by AdnlNodesTlConstructor 15 | } 16 | 17 | private object AdnlNodesTlConstructor : TlConstructor( 18 | schema = "adnl.nodes nodes:(vector adnl.node) = adnl.Nodes" 19 | ) { 20 | override fun encode(writer: TlWriter, value: AdnlNodes) = writer { 21 | writeVector(value.nodes) { 22 | write(AdnlNode, it) 23 | } 24 | } 25 | 26 | override fun decode(reader: TlReader): AdnlNodes = reader { 27 | val nodes = readVector { 28 | read(AdnlNode) 29 | } 30 | AdnlNodes(nodes) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/AdnlPing.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlConstructor 6 | import org.ton.tl.TlReader 7 | import org.ton.tl.TlWriter 8 | import kotlin.jvm.JvmName 9 | 10 | @Serializable 11 | @SerialName("adnl.ping") 12 | public data class AdnlPing( 13 | @get:JvmName("value") 14 | val value: Long 15 | ) { 16 | public companion object : TlConstructor( 17 | "adnl.ping value:long = adnl.Pong" 18 | ) { 19 | override fun decode(reader: TlReader): AdnlPing { 20 | val value = reader.readLong() 21 | return AdnlPing(value) 22 | } 23 | 24 | override fun encode(writer: TlWriter, value: AdnlPing) { 25 | writer.writeLong(value.value) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/AdnlPong.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlConstructor 6 | import org.ton.tl.TlReader 7 | import org.ton.tl.TlWriter 8 | import kotlin.jvm.JvmName 9 | 10 | @Serializable 11 | @SerialName("adnl.pong") 12 | public data class AdnlPong( 13 | @get:JvmName("value") 14 | val value: Long 15 | ) { 16 | public companion object : TlConstructor( 17 | schema = "adnl.pong value:long = adnl.Pong" 18 | ) { 19 | override fun decode(reader: TlReader): AdnlPong { 20 | val value = reader.readLong() 21 | return AdnlPong(value) 22 | } 23 | 24 | override fun encode(output: TlWriter, value: AdnlPong) { 25 | output.writeLong(value.value) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/config/AdnlConfigGlobal.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl.config 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.adnl.AdnlNodes 6 | import org.ton.tl.* 7 | import kotlin.jvm.JvmName 8 | 9 | @SerialName("adnl.config.global") 10 | @Serializable 11 | public data class AdnlConfigGlobal( 12 | @SerialName("static_nodes") 13 | @get:JvmName("staticNodes") 14 | val staticNodes: AdnlNodes = AdnlNodes() 15 | ) { 16 | public companion object : TlCodec by AdnlConfigGlobalTlConstructor 17 | } 18 | 19 | private object AdnlConfigGlobalTlConstructor : TlConstructor( 20 | schema = "adnl.config.global static_nodes:adnl.nodes = adnl.config.Global" 21 | ) { 22 | override fun encode(writer: TlWriter, value: AdnlConfigGlobal) = writer { 23 | write(AdnlNodes, value.staticNodes) 24 | } 25 | 26 | override fun decode(reader: TlReader): AdnlConfigGlobal = reader { 27 | val staticNodes = read(AdnlNodes) 28 | AdnlConfigGlobal(staticNodes) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/message/AdnlMessageCustom.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl.message 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.tl.ByteStringBase64Serializer 7 | import org.ton.tl.TlConstructor 8 | import org.ton.tl.TlReader 9 | import org.ton.tl.TlWriter 10 | import org.ton.tl.constructors.BytesTlConstructor 11 | import kotlin.jvm.JvmName 12 | 13 | @SerialName("adnl.message.custom") 14 | @Serializable 15 | public data class AdnlMessageCustom( 16 | @get:JvmName("data") 17 | @Serializable(ByteStringBase64Serializer::class) 18 | val data: ByteString 19 | ) : AdnlMessage { 20 | public companion object : TlConstructor( 21 | schema = "adnl.message.custom data:bytes = adnl.Message", 22 | ) { 23 | public fun sizeOf(value: AdnlMessageCustom): Int = 24 | BytesTlConstructor.sizeOf(value.data) 25 | 26 | override fun decode(reader: TlReader): AdnlMessageCustom { 27 | val data = reader.readByteString() 28 | return AdnlMessageCustom(data) 29 | } 30 | 31 | override fun encode(writer: TlWriter, value: AdnlMessageCustom) { 32 | writer.writeBytes(value.data) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/message/AdnlMessageNop.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl.message 2 | 3 | import kotlinx.serialization.SerialName 4 | import org.ton.tl.TlConstructor 5 | import org.ton.tl.TlReader 6 | import org.ton.tl.TlWriter 7 | 8 | @SerialName("adnl.message.nop") 9 | public object AdnlMessageNop : AdnlMessage, TlConstructor( 10 | schema = "adnl.message.nop = adnl.Message" 11 | ) { 12 | public const val SIZE_BYTES: Int = 0 13 | 14 | override fun decode(reader: TlReader): AdnlMessageNop = this 15 | 16 | override fun encode(writer: TlWriter, value: AdnlMessageNop) { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tonapi-tl/src/adnl/message/AdnlMessageReinit.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.adnl.message 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlConstructor 6 | import org.ton.tl.TlReader 7 | import org.ton.tl.TlWriter 8 | import kotlin.jvm.JvmName 9 | 10 | @SerialName("adnl.message.reinit") 11 | @Serializable 12 | public data class AdnlMessageReinit( 13 | @get:JvmName("date") 14 | val date: Int 15 | ) : AdnlMessage { 16 | public companion object : TlConstructor( 17 | schema = "adnl.message.reinit date:int = adnl.Message", 18 | ) { 19 | public const val SIZE_BYTES: Int = Int.SIZE_BYTES 20 | 21 | override fun encode(writer: TlWriter, value: AdnlMessageReinit) { 22 | writer.writeInt(value.date) 23 | } 24 | 25 | override fun decode(reader: TlReader): AdnlMessageReinit { 26 | val date = reader.readInt() 27 | return AdnlMessageReinit(date) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tonapi-tl/src/config/ConfigLocal.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.config 4 | 5 | import kotlinx.serialization.Polymorphic 6 | import kotlinx.serialization.SerialName 7 | import kotlinx.serialization.Serializable 8 | import kotlinx.serialization.json.JsonClassDiscriminator 9 | import org.ton.api.control.ControlConfigLocal 10 | import org.ton.api.dht.config.DhtConfigLocal 11 | import org.ton.api.id.config.IdConfigLocal 12 | import org.ton.api.liteserver.config.LiteServerConfigLocal 13 | import org.ton.api.validator.config.ValidatorConfigLocal 14 | 15 | @Serializable 16 | @Polymorphic 17 | @SerialName("config.local") 18 | @JsonClassDiscriminator("@type") 19 | public data class ConfigLocal( 20 | @SerialName("local_ids") 21 | val localIds: Collection, 22 | val dht: Collection, 23 | val validators: Collection, 24 | @SerialName("liteservers") 25 | val liteServers: Collection, 26 | val control: Collection 27 | ) 28 | -------------------------------------------------------------------------------- /tonapi-tl/src/control/ControlConfigLocal.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.control 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.Polymorphic 5 | import kotlinx.serialization.SerialName 6 | import kotlinx.serialization.Serializable 7 | import kotlinx.serialization.json.JsonClassDiscriminator 8 | import org.ton.api.pk.PrivateKey 9 | import org.ton.tl.ByteStringBase64Serializer 10 | 11 | @Serializable 12 | @Polymorphic 13 | @SerialName("control.config.local") 14 | @JsonClassDiscriminator("@type") 15 | public data class ControlConfigLocal( 16 | val priv: PrivateKey, 17 | @Serializable(ByteStringBase64Serializer::class) 18 | val pub: ByteString, 19 | val port: Int 20 | ) 21 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/DhtMessage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht 2 | 3 | import kotlinx.serialization.Serializable 4 | import org.ton.tl.* 5 | 6 | @Serializable 7 | public data class DhtMessage( 8 | val node: DhtNode 9 | ) : TlObject { 10 | override fun tlCodec(): TlCodec = Companion 11 | 12 | public companion object : TlConstructor( 13 | schema = "dht.message node:dht.node = dht.Message" 14 | ) { 15 | override fun encode(writer: TlWriter, value: DhtMessage) { 16 | writer.write(DhtNode, value.node) 17 | } 18 | 19 | override fun decode(reader: TlReader): DhtMessage { 20 | val node = reader.read(DhtNode) 21 | return DhtMessage(node) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/DhtNodes.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht 2 | 3 | import kotlinx.serialization.Serializable 4 | import kotlinx.serialization.json.JsonClassDiscriminator 5 | import org.ton.api.adnl.AdnlNodes 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @JsonClassDiscriminator("@type") 10 | public data class DhtNodes( 11 | val nodes: List = emptyList() 12 | ) : TlObject { 13 | public fun toAdnlNodes(): AdnlNodes = AdnlNodes(nodes.map { it.toAdnlNode() }) 14 | 15 | override fun tlCodec(): TlCodec = Companion 16 | 17 | public companion object : TlConstructor( 18 | schema = "dht.nodes nodes:(vector dht.node) = dht.Nodes" 19 | ) { 20 | override fun encode(writer: TlWriter, value: DhtNodes) { 21 | writer.writeVector(value.nodes) { 22 | write(DhtNode, it) 23 | } 24 | } 25 | 26 | override fun decode(reader: TlReader): DhtNodes { 27 | val nodes = reader.readVector { 28 | read(DhtNode) 29 | } 30 | return DhtNodes(nodes) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/DhtPong.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.* 6 | 7 | @Serializable 8 | public data class DhtPong( 9 | @SerialName("random_id") 10 | val randomId: Long 11 | ) : TlObject { 12 | override fun tlCodec(): TlCodec = DhtPongTlConstructor 13 | 14 | public companion object : TlCodec by DhtPongTlConstructor 15 | } 16 | 17 | private object DhtPongTlConstructor : TlConstructor( 18 | schema = "dht.pong random_id:long = dht.Pong" 19 | ) { 20 | override fun encode(writer: TlWriter, value: DhtPong) { 21 | writer.writeLong(value.randomId) 22 | } 23 | 24 | override fun decode(input: TlReader): DhtPong { 25 | val randomId = input.readLong() 26 | return DhtPong(randomId) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/DhtStore.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht 2 | 3 | import org.ton.tl.* 4 | 5 | public data class DhtStore( 6 | val value: DhtValue 7 | ) : TLFunction { 8 | override fun tlCodec(): TlCodec = DhtStoreTlConstructor 9 | override fun resultTlCodec(): TlCodec = DhtStored 10 | 11 | public companion object : TlCodec by DhtStoreTlConstructor 12 | } 13 | 14 | private object DhtStoreTlConstructor : TlConstructor( 15 | schema = "dht.store value:dht.value = dht.Stored" 16 | ) { 17 | override fun decode(input: TlReader): DhtStore { 18 | val value = input.read(DhtValue) 19 | return DhtStore(value) 20 | } 21 | 22 | override fun encode(output: TlWriter, value: DhtStore) { 23 | output.write(DhtValue, value.value) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/DhtStored.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht 2 | 3 | import org.ton.tl.* 4 | 5 | public object DhtStored : TlConstructor( 6 | schema = "dht.stored = dht.Stored" 7 | ), TlObject { 8 | override fun tlCodec(): TlCodec = this 9 | 10 | override fun encode(writer: TlWriter, value: DhtStored) { 11 | } 12 | 13 | override fun decode(reader: TlReader): DhtStored = DhtStored 14 | } 15 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/DhtUpdateRule.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlObject 7 | import org.ton.tl.constructors.EnumTlCombinator 8 | 9 | @Serializable 10 | public enum class DhtUpdateRule : TlObject { 11 | @SerialName("dht.updateRule.signature") 12 | SIGNATURE, 13 | 14 | @SerialName("dht.updateRule.anybody") 15 | ANYBODY, 16 | 17 | @SerialName("dht.updateRule.overlayNodes") 18 | OVERLAY_NODES; 19 | 20 | override fun tlCodec(): TlCodec = Companion 21 | 22 | public companion object : EnumTlCombinator( 23 | DhtUpdateRule::class, 24 | SIGNATURE to "dht.updateRule.signature = dht.UpdateRule", 25 | ANYBODY to "dht.updateRule.anybody = dht.UpdateRule", 26 | OVERLAY_NODES to "dht.updateRule.overlayNodes = dht.UpdateRule" 27 | ) 28 | } 29 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/config/DhtConfigIdLocal.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.dht.config 4 | 5 | import kotlinx.serialization.SerialName 6 | import org.ton.api.adnl.AdnlIdShort 7 | import org.ton.tl.* 8 | import kotlin.jvm.JvmName 9 | 10 | @SerialName("dht.config.local") 11 | public data class DhtConfigIdLocal( 12 | @get:JvmName("id") 13 | val id: AdnlIdShort 14 | ) : DhtConfigLocal { 15 | public companion object : TlConstructor( 16 | schema = "dht.config.local id:adnl.id_short = dht.config.Local" 17 | ) { 18 | override fun encode(writer: TlWriter, value: DhtConfigIdLocal) { 19 | writer.write(AdnlIdShort, value.id) 20 | } 21 | 22 | override fun decode(reader: TlReader): DhtConfigIdLocal { 23 | val id = reader.read(AdnlIdShort) 24 | return DhtConfigIdLocal(id) 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/config/DhtConfigLocal.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.dht.config 4 | 5 | import kotlinx.serialization.Polymorphic 6 | import kotlinx.serialization.Serializable 7 | import kotlinx.serialization.json.JsonClassDiscriminator 8 | import org.ton.tl.TlCodec 9 | import org.ton.tl.TlCombinator 10 | import org.ton.tl.TlObject 11 | 12 | @Polymorphic 13 | @Serializable 14 | @JsonClassDiscriminator("@type") 15 | public sealed interface DhtConfigLocal : TlObject { 16 | override fun tlCodec(): TlCodec = Companion 17 | 18 | public companion object : TlCombinator( 19 | DhtConfigLocal::class, 20 | DhtConfigRandomLocal::class to DhtConfigRandomLocal.Companion, 21 | DhtConfigIdLocal::class to DhtConfigIdLocal.Companion, 22 | ) 23 | } 24 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/config/DhtConfigRandomLocal.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.dht.config 4 | 5 | import kotlinx.serialization.Polymorphic 6 | import kotlinx.serialization.SerialName 7 | import kotlinx.serialization.Serializable 8 | import kotlinx.serialization.json.JsonClassDiscriminator 9 | import org.ton.tl.TlConstructor 10 | import org.ton.tl.TlReader 11 | import org.ton.tl.TlWriter 12 | import kotlin.jvm.JvmName 13 | 14 | @SerialName("dht.config.random.local") 15 | @Polymorphic 16 | @Serializable 17 | @JsonClassDiscriminator("@type") 18 | public class DhtConfigRandomLocal( 19 | @get:JvmName("cnt") 20 | public val cnt: Int 21 | ) : DhtConfigLocal { 22 | public companion object : TlConstructor( 23 | schema = "dht.config.random.local cnt:int = dht.config.Local", 24 | ) { 25 | override fun encode(writer: TlWriter, value: DhtConfigRandomLocal) { 26 | writer.writeInt(value.cnt) 27 | } 28 | 29 | override fun decode(reader: TlReader): DhtConfigRandomLocal { 30 | val cnt = reader.readInt() 31 | return DhtConfigRandomLocal(cnt) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/db/DhtDbBucket.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht.db 2 | 3 | import kotlinx.serialization.Serializable 4 | import org.ton.api.dht.DhtNode 5 | import org.ton.api.dht.DhtNodes 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | public data class DhtDbBucket( 10 | val nodes: DhtNodes 11 | ) : TlObject { 12 | public constructor(nodes: List) : this(DhtNodes(nodes)) 13 | 14 | override fun tlCodec(): TlCodec = Companion 15 | 16 | public companion object : TlConstructor( 17 | schema = "dht.db.bucket nodes:dht.nodes = dht.db.Bucket" 18 | ) { 19 | override fun encode(writer: TlWriter, value: DhtDbBucket) { 20 | writer.write(DhtNodes, value.nodes) 21 | } 22 | 23 | override fun decode(reader: TlReader): DhtDbBucket { 24 | val nodes = reader.read(DhtNodes) 25 | return DhtDbBucket(nodes) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/db/DhtDbKey.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht.db 2 | 3 | import kotlinx.serialization.Serializable 4 | import org.ton.tl.* 5 | 6 | @Serializable 7 | public data class DhtDbKey( 8 | val id: Int 9 | ) : TlObject { 10 | override fun tlCodec(): TlCodec = Companion 11 | 12 | public companion object : TlConstructor( 13 | schema = "dht.db.key.bucket id:int = dht.db.Key" 14 | ) { 15 | override fun encode(output: TlWriter, value: DhtDbKey) { 16 | output.writeInt(value.id) 17 | } 18 | 19 | override fun decode(input: TlReader): DhtDbKey { 20 | val id = input.readInt() 21 | return DhtDbKey(id) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/functions/DhtFindValue.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht.functions 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import org.ton.api.dht.DhtKey 5 | import org.ton.api.dht.DhtValueResult 6 | import org.ton.tl.* 7 | 8 | public data class DhtFindValue( 9 | val key: ByteString, 10 | val k: Int 11 | ) : TLFunction { 12 | public constructor(key: ByteArray, k: Int) : this(ByteString(key), k) 13 | public constructor(key: DhtKey, k: Int) : this(key.hash(), k) 14 | 15 | override fun tlCodec(): TlCodec = DhtFindValueTlConstructor 16 | 17 | override fun resultTlCodec(): TlCodec = DhtValueResult 18 | } 19 | 20 | private object DhtFindValueTlConstructor : TlConstructor( 21 | schema = "dht.findValue key:int256 k:int = dht.ValueResult" 22 | ) { 23 | override fun encode(output: TlWriter, value: DhtFindValue) { 24 | output.writeRaw(value.key) 25 | output.writeInt(value.k) 26 | } 27 | 28 | override fun decode(input: TlReader): DhtFindValue { 29 | val key = input.readByteString(32) 30 | val k = input.readInt() 31 | return DhtFindValue(key, k) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/functions/DhtPing.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht.functions 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.dht.DhtPong 6 | import org.ton.tl.* 7 | 8 | @SerialName("dht.ping") 9 | @Serializable 10 | public data class DhtPing( 11 | @SerialName("random_id") 12 | val randomId: Long 13 | ) : TLFunction { 14 | override fun tlCodec(): TlCodec = DhtPing 15 | override fun resultTlCodec(): TlCodec = DhtPong 16 | 17 | public companion object : TlCodec by DhtPingTlConstructor 18 | } 19 | 20 | private object DhtPingTlConstructor : TlConstructor( 21 | schema = "dht.ping random_id:long = dht.Pong" 22 | ) { 23 | override fun decode(input: TlReader): DhtPing { 24 | val random_id = input.readLong() 25 | return DhtPing(random_id) 26 | } 27 | 28 | override fun encode(output: TlWriter, value: DhtPing) { 29 | output.writeLong(value.randomId) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tonapi-tl/src/dht/functions/DhtQuery.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.dht.functions 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.dht.DhtNode 6 | import org.ton.tl.* 7 | 8 | public interface DhtQueryFunction { 9 | public fun query(node: DhtNode) 10 | } 11 | 12 | @SerialName("dht.query") 13 | @Serializable 14 | public data class DhtQuery( 15 | val node: DhtNode 16 | ) { 17 | public companion object : TlCodec by DhtQueryTlConstructor 18 | } 19 | 20 | private object DhtQueryTlConstructor : TlConstructor( 21 | schema = "dht.query node:dht.node = True" 22 | ) { 23 | override fun decode(reader: TlReader): DhtQuery { 24 | val node = reader.read(DhtNode) 25 | return DhtQuery(node) 26 | } 27 | 28 | override fun encode(writer: TlWriter, value: DhtQuery) { 29 | writer.write(DhtNode, value.node) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tonapi-tl/src/exception/TvmException.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.exception 2 | 3 | public class TvmException( 4 | public val code: Int, 5 | message: String? = null, 6 | cause: Throwable? = null 7 | ) : RuntimeException(if (message != null) code.toString() else "[$code] $message", cause) 8 | -------------------------------------------------------------------------------- /tonapi-tl/src/fec/FecType.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.fec 2 | 3 | import kotlinx.serialization.Serializable 4 | import org.ton.tl.TlCombinator 5 | import org.ton.tl.TlObject 6 | 7 | @Serializable 8 | public sealed interface FecType : TlObject { 9 | public val dataSize: Int 10 | public val symbolSize: Int 11 | public val symbolCount: Int 12 | 13 | public companion object : TlCombinator( 14 | FecType::class, 15 | FecRaptorQ::class to FecRaptorQ, 16 | ) { 17 | public fun check(fecType: FecType) { 18 | require(fecType.symbolSize != 0) { "expected symbol_size != 0, actual: ${fecType.symbolSize}" } 19 | require(fecType.symbolSize <= 1 shl 11) { "symbol_size must be less than ${1 shl 11}, actual: ${fecType.symbolSize}" } 20 | val expectedSymbolCount = (fecType.dataSize + fecType.symbolSize - 1) / fecType.symbolSize 21 | if (fecType.symbolCount != expectedSymbolCount) { 22 | throw IllegalArgumentException("expected symbol_count: $expectedSymbolCount, actual: ${fecType.symbolCount}") 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tonapi-tl/src/http/HttpHeader.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.http 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | 10 | @SerialName("http.header") 11 | @Serializable 12 | public data class HttpHeader( 13 | val name: String, 14 | val value: String 15 | ) { 16 | public companion object : TlCodec by HttpHeaderTlConstructor 17 | } 18 | 19 | public inline operator fun Iterable.get(name: String): String? = 20 | firstOrNull { it.name == name }?.value 21 | 22 | public inline fun Iterable.getAll(name: String): Sequence = 23 | asSequence().filter { it.name == name }.map { it.value } 24 | 25 | private object HttpHeaderTlConstructor : TlConstructor( 26 | schema = "http.header name:string value:string = http.Header" 27 | ) { 28 | override fun decode(input: TlReader): HttpHeader { 29 | val name = input.readString() 30 | val value = input.readString() 31 | return HttpHeader(name, value) 32 | } 33 | 34 | override fun encode(output: TlWriter, value: HttpHeader) { 35 | output.writeString(value.name) 36 | output.writeString(value.value) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tonapi-tl/src/http/server/HttpServerDnsEntry.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.http.server 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.adnl.AdnlIdShort 6 | import org.ton.tl.* 7 | 8 | @SerialName("http.server.dnsEntry") 9 | @Serializable 10 | public data class HttpServerDnsEntry( 11 | val domain: String, 12 | val addr: AdnlIdShort 13 | ) { 14 | public companion object : TlCodec by HttpServerDnsEntryTlConstructor 15 | } 16 | 17 | private object HttpServerDnsEntryTlConstructor : TlConstructor( 18 | schema = "http.server.dnsEntry domain:string addr:adnl.id_short = http.server.DnsEntry" 19 | ) { 20 | override fun decode(input: TlReader): HttpServerDnsEntry { 21 | val domain = input.readString() 22 | val addr = input.read(AdnlIdShort) 23 | return HttpServerDnsEntry(domain, addr) 24 | } 25 | 26 | override fun encode(output: TlWriter, value: HttpServerDnsEntry) { 27 | output.writeString(value.domain) 28 | output.write(AdnlIdShort, value.addr) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tonapi-tl/src/id/config/IdConfigLocal.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.id.config 4 | 5 | import kotlinx.serialization.SerialName 6 | import kotlinx.serialization.Serializable 7 | import kotlinx.serialization.json.JsonClassDiscriminator 8 | import org.ton.api.pk.PrivateKey 9 | 10 | @Serializable 11 | @SerialName("id.config.local") 12 | @JsonClassDiscriminator("@type") 13 | public data class IdConfigLocal( 14 | val id: PrivateKey 15 | ) 16 | -------------------------------------------------------------------------------- /tonapi-tl/src/liteclient/config/LiteClientConfigGlobal.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.liteclient.config 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.dht.config.DhtConfigGlobal 6 | import org.ton.api.liteserver.LiteServerDesc 7 | import org.ton.api.validator.config.ValidatorConfigGlobal 8 | 9 | @SerialName("liteclient.config.global") 10 | @Serializable 11 | public data class LiteClientConfigGlobal( 12 | val dht: DhtConfigGlobal = DhtConfigGlobal(), 13 | @SerialName("liteservers") 14 | val liteServers: Collection, 15 | val validator: ValidatorConfigGlobal = ValidatorConfigGlobal() 16 | ) 17 | -------------------------------------------------------------------------------- /tonapi-tl/src/liteserver/LiteServerDesc.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.liteserver 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.api.pub.PublicKey 6 | import kotlin.jvm.JvmName 7 | 8 | @Serializable 9 | @SerialName("liteserver.desc") 10 | public data class LiteServerDesc( 11 | @get:JvmName("id") 12 | val id: PublicKey, 13 | @get:JvmName("ip") 14 | val ip: Int, 15 | @get:JvmName("port") 16 | val port: Int 17 | ) { 18 | override fun toString(): String = "$ip:$port:$id" 19 | } 20 | -------------------------------------------------------------------------------- /tonapi-tl/src/liteserver/config/LiteServerConfigLocal.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.liteserver.config 2 | 3 | import kotlinx.serialization.Polymorphic 4 | import kotlinx.serialization.Serializable 5 | import kotlinx.serialization.json.JsonClassDiscriminator 6 | 7 | @Serializable 8 | @Polymorphic 9 | @JsonClassDiscriminator("@type") 10 | public sealed interface LiteServerConfigLocal 11 | -------------------------------------------------------------------------------- /tonapi-tl/src/liteserver/config/LiteServerConfigRandomLocal.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.liteserver.config 2 | 3 | import kotlinx.serialization.Polymorphic 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import kotlinx.serialization.json.JsonClassDiscriminator 7 | 8 | @SerialName("liteserver.config.random.local") 9 | @Polymorphic 10 | @Serializable 11 | @JsonClassDiscriminator("@type") 12 | public data class LiteServerConfigRandomLocal( 13 | val port: Int 14 | ) : LiteServerConfigLocal 15 | -------------------------------------------------------------------------------- /tonapi-tl/src/overlay/OverlayNodes.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.overlay 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.* 6 | 7 | @Serializable 8 | @SerialName("overlay.nodes") 9 | public class OverlayNodes( 10 | public val nodes: List 11 | ) : TlObject { 12 | public constructor(vararg nodes: OverlayNode) : this(nodes.toList()) 13 | 14 | override fun tlCodec(): TlCodec = Companion 15 | 16 | public companion object : TlConstructor( 17 | schema = "overlay.nodes nodes:(vector overlay.node) = overlay.Nodes", 18 | ) { 19 | override fun encode(output: TlWriter, value: OverlayNodes) { 20 | output.writeVector(value.nodes) { 21 | write(OverlayNode, it) 22 | } 23 | } 24 | 25 | override fun decode(input: TlReader): OverlayNodes { 26 | val nodes = input.readVector { 27 | read(OverlayNode) 28 | } 29 | return OverlayNodes(nodes) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tonapi-tl/src/pk/pk.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.pk 4 | 5 | import kotlinx.serialization.Serializable 6 | import org.ton.api.adnl.AdnlIdShort 7 | import org.ton.api.pub.PublicKey 8 | import org.ton.crypto.Decryptor 9 | import org.ton.tl.TlCombinator 10 | 11 | @Serializable 12 | public sealed interface PrivateKey : Decryptor { 13 | public fun publicKey(): PublicKey 14 | public fun toAdnlIdShort(): AdnlIdShort = publicKey().toAdnlIdShort() 15 | 16 | public companion object : TlCombinator( 17 | PrivateKey::class, 18 | PrivateKeyUnencrypted::class to PrivateKeyUnencrypted, 19 | PrivateKeyEd25519::class to PrivateKeyEd25519, 20 | PrivateKeyAes::class to PrivateKeyAes, 21 | PrivateKeyOverlay::class to PrivateKeyOverlay, 22 | ) 23 | } 24 | -------------------------------------------------------------------------------- /tonapi-tl/src/rldp/RldpAnswer.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.rldp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.tl.* 7 | 8 | @SerialName("rldp.answer") 9 | @Serializable 10 | public data class RldpAnswer( 11 | @SerialName("query_id") 12 | @Serializable(ByteStringBase64Serializer::class) 13 | val queryId: ByteString, 14 | @Serializable(ByteStringBase64Serializer::class) 15 | override val data: ByteString 16 | ) : RldpMessage { 17 | override val id: ByteString 18 | get() = queryId 19 | 20 | override fun tlCodec(): TlCodec = Companion 21 | 22 | public companion object : TlConstructor( 23 | schema = "rldp.answer query_id:int256 data:bytes = rldp.Message", 24 | ) { 25 | override fun encode(writer: TlWriter, value: RldpAnswer) { 26 | writer.writeRaw(value.queryId) 27 | writer.writeBytes(value.data) 28 | } 29 | 30 | override fun decode(reader: TlReader): RldpAnswer { 31 | val queryId = reader.readByteString(32) 32 | val data = reader.readByteString() 33 | return RldpAnswer(queryId, data) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tonapi-tl/src/rldp/RldpComplete.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.rldp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @SerialName("rldp.complete") 10 | public data class RldpComplete( 11 | @SerialName("transfer_id") 12 | @Serializable(ByteStringBase64Serializer::class) 13 | override val transferId: ByteString, 14 | override val part: Int, 15 | ) : RldpMessagePart { 16 | 17 | override fun tlCodec(): TlCodec = Companion 18 | 19 | public companion object : TlConstructor( 20 | schema = "rldp.complete transfer_id:int256 part:int = rldp.MessagePart", 21 | ) { 22 | override fun encode(output: TlWriter, value: RldpComplete) { 23 | output.writeRaw(value.transferId) 24 | output.writeInt(value.part) 25 | } 26 | 27 | override fun decode(input: TlReader): RldpComplete { 28 | val transfer_id = input.readByteString(32) 29 | val part = input.readInt() 30 | return RldpComplete(transfer_id, part) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tonapi-tl/src/rldp/RldpConfirm.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.rldp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @SerialName("rldp.confirm") 10 | public data class RldpConfirm( 11 | @SerialName("transfer_id") 12 | @Serializable(ByteStringBase64Serializer::class) 13 | override val transferId: ByteString, 14 | override val part: Int, 15 | val seqno: Int 16 | ) : RldpMessagePart { 17 | override fun tlCodec(): TlCodec = Companion 18 | 19 | public companion object : TlConstructor( 20 | schema = "rldp.confirm transfer_id:int256 part:int seqno:int = rldp.MessagePart", 21 | ) { 22 | override fun encode(output: TlWriter, value: RldpConfirm) { 23 | output.writeRaw(value.transferId) 24 | output.writeInt(value.part) 25 | output.writeInt(value.seqno) 26 | } 27 | 28 | override fun decode(input: TlReader): RldpConfirm { 29 | val transfer_id = input.readByteString(23) 30 | val part = input.readInt() 31 | val seqno = input.readInt() 32 | return RldpConfirm(transfer_id, part, seqno) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tonapi-tl/src/rldp/RldpMessage.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.rldp 4 | 5 | import kotlinx.io.bytestring.ByteString 6 | import kotlinx.serialization.Polymorphic 7 | import kotlinx.serialization.Serializable 8 | import kotlinx.serialization.json.JsonClassDiscriminator 9 | import org.ton.tl.TlCombinator 10 | import org.ton.tl.TlObject 11 | 12 | @Polymorphic 13 | @Serializable 14 | @JsonClassDiscriminator("@type") 15 | public sealed interface RldpMessage : TlObject { 16 | public val id: ByteString 17 | public val data: ByteString 18 | 19 | public companion object : TlCombinator( 20 | RldpMessage::class, 21 | RldpMessageData::class to RldpMessageData, 22 | RldpQuery::class to RldpQuery, 23 | RldpAnswer::class to RldpAnswer, 24 | ) 25 | } 26 | -------------------------------------------------------------------------------- /tonapi-tl/src/rldp/RldpMessageData.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.rldp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.tl.* 7 | 8 | @Serializable 9 | @SerialName("rldp.message") 10 | public data class RldpMessageData( 11 | @Serializable(ByteStringBase64Serializer::class) 12 | override val id: ByteString, 13 | @Serializable(ByteStringBase64Serializer::class) 14 | override val data: ByteString 15 | ) : RldpMessage { 16 | override fun tlCodec(): TlCodec = Companion 17 | 18 | public companion object : TlConstructor( 19 | schema = "rldp.message id:int256 data:bytes = rldp.Message", 20 | ) { 21 | override fun encode(output: TlWriter, value: RldpMessageData) { 22 | output.writeRaw(value.id.toByteArray()) 23 | output.writeBytes(value.data) 24 | } 25 | 26 | override fun decode(input: TlReader): RldpMessageData { 27 | val id = input.readByteString(32) 28 | val data = input.readByteString() 29 | return RldpMessageData(id, data) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tonapi-tl/src/rldp/RldpMessagePart.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.rldp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCombinator 6 | import org.ton.tl.TlObject 7 | 8 | @Serializable 9 | public sealed interface RldpMessagePart : TlObject { 10 | public val transferId: ByteString 11 | public val part: Int 12 | 13 | public companion object : TlCombinator( 14 | RldpMessagePart::class, 15 | RldpMessagePartData::class to RldpMessagePartData, 16 | RldpConfirm::class to RldpConfirm, 17 | RldpComplete::class to RldpComplete, 18 | ) 19 | } 20 | -------------------------------------------------------------------------------- /tonapi-tl/src/tcp/TcpAuthentificate.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tcp 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | 10 | @SerialName("tcp.authentificate") 11 | @Serializable 12 | public data class TcpAuthentificate( 13 | val nonce: ByteArray 14 | ) { 15 | override fun equals(other: Any?): Boolean { 16 | if (this === other) return true 17 | if (other !is TcpAuthentificate) return false 18 | if (!nonce.contentEquals(other.nonce)) return false 19 | return true 20 | } 21 | 22 | override fun hashCode(): Int = nonce.contentHashCode() 23 | 24 | public companion object : TlCodec by TcpAuthentificateTlConstructor 25 | } 26 | 27 | private object TcpAuthentificateTlConstructor : TlConstructor( 28 | schema = "tcp.authentificate nonce:bytes = tcp.Authentificate" 29 | ) { 30 | override fun decode(input: TlReader): TcpAuthentificate { 31 | val nonce = input.readBytes() 32 | return TcpAuthentificate(nonce) 33 | } 34 | 35 | override fun encode(output: TlWriter, value: TcpAuthentificate) { 36 | output.writeBytes(value.nonce) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tonapi-tl/src/tcp/TcpAuthentificationComplete.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tcp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.api.pub.PublicKey 7 | import org.ton.tl.* 8 | 9 | @SerialName("tcp.authentificationComplete") 10 | @Serializable 11 | public data class TcpAuthentificationComplete( 12 | val key: PublicKey, 13 | @Serializable(ByteStringBase64Serializer::class) 14 | val signature: ByteString 15 | ) : TcpMessage { 16 | public companion object : TlCodec by TcpAuthentificationCompleteTlConstructor 17 | } 18 | 19 | private object TcpAuthentificationCompleteTlConstructor : TlConstructor( 20 | schema = "tcp.authentificationComplete key:PublicKey signature:bytes = tcp.Message" 21 | ) { 22 | override fun decode(reader: TlReader): TcpAuthentificationComplete { 23 | val key = reader.read(PublicKey) 24 | val signature = reader.readByteString() 25 | return TcpAuthentificationComplete(key, signature) 26 | } 27 | 28 | override fun encode(writer: TlWriter, value: TcpAuthentificationComplete) { 29 | writer.write(PublicKey, value.key) 30 | writer.writeBytes(value.signature) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tonapi-tl/src/tcp/TcpAuthentificationNonce.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tcp 2 | 3 | import kotlinx.io.bytestring.ByteString 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import org.ton.tl.* 7 | 8 | @SerialName("tcp.authentificationNonce") 9 | @Serializable 10 | public data class TcpAuthentificationNonce( 11 | @Serializable(ByteStringBase64Serializer::class) 12 | val nonce: ByteString 13 | ) { 14 | public companion object : TlCodec by TcpAuthentificationNonceTlConstructor 15 | } 16 | 17 | private object TcpAuthentificationNonceTlConstructor : TlConstructor( 18 | schema = "tcp.authentificationNonce nonce:bytes = tcp.Message" 19 | ) { 20 | override fun decode(reader: TlReader): TcpAuthentificationNonce { 21 | val nonce = reader.readByteString() 22 | return TcpAuthentificationNonce(nonce) 23 | } 24 | 25 | override fun encode(writer: TlWriter, value: TcpAuthentificationNonce) { 26 | writer.writeBytes(value.nonce) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tonapi-tl/src/tcp/TcpMessage.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tcp 2 | 3 | public sealed interface TcpMessage { 4 | } 5 | -------------------------------------------------------------------------------- /tonapi-tl/src/tcp/TcpPing.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tcp 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | 10 | @Serializable 11 | @SerialName("tcp.ping") 12 | public data class TcpPing( 13 | @SerialName("random_id") 14 | val randomId: Long 15 | ) { 16 | public companion object : TlCodec by TcpPingTlConstructor 17 | } 18 | 19 | private object TcpPingTlConstructor : TlConstructor( 20 | schema = "tcp.ping random_id:long = tcp.Pong" 21 | ) { 22 | override fun decode(input: TlReader): TcpPing { 23 | val randomId = input.readLong() 24 | return TcpPing(randomId) 25 | } 26 | 27 | override fun encode(output: TlWriter, value: TcpPing) { 28 | output.writeLong(value.randomId) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tonapi-tl/src/tcp/TcpPong.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tcp 2 | 3 | import kotlinx.serialization.SerialName 4 | import kotlinx.serialization.Serializable 5 | import org.ton.tl.TlCodec 6 | import org.ton.tl.TlConstructor 7 | import org.ton.tl.TlReader 8 | import org.ton.tl.TlWriter 9 | 10 | @Serializable 11 | @SerialName("tcp.pong") 12 | public data class TcpPong( 13 | @SerialName("random_id") 14 | val randomId: Long 15 | ) { 16 | public companion object : TlCodec by TcpPongTlConstructor 17 | } 18 | 19 | private object TcpPongTlConstructor : TlConstructor( 20 | schema = "tcp.pong random_id:long = tcp.Pong" 21 | ) { 22 | override fun decode(reader: TlReader): TcpPong { 23 | val randomId = reader.readLong() 24 | return TcpPong(randomId) 25 | } 26 | 27 | override fun encode(writer: TlWriter, value: TcpPong) { 28 | writer.writeLong(value.randomId) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tonapi-tl/src/tonnode/Workchain.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.tonnode 2 | 3 | public object Workchain { 4 | public const val INVALID_WORKCHAIN: Int = 0x80000000.toInt() 5 | public const val MASTERCHAIN_ID: Int = -1 6 | public const val BASECHAIN_ID: Int = 0 7 | } 8 | -------------------------------------------------------------------------------- /tonapi-tl/src/validator/config/ValidatorConfigLocal.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("OPT_IN_USAGE") 2 | 3 | package org.ton.api.validator.config 4 | 5 | import kotlinx.serialization.Polymorphic 6 | import kotlinx.serialization.Serializable 7 | import kotlinx.serialization.json.JsonClassDiscriminator 8 | 9 | @Serializable 10 | @Polymorphic 11 | @JsonClassDiscriminator("@type") 12 | public sealed interface ValidatorConfigLocal 13 | -------------------------------------------------------------------------------- /tonapi-tl/src/validator/config/ValidatorConfigRandomLocal.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.validator.config 2 | 3 | import kotlinx.serialization.Polymorphic 4 | import kotlinx.serialization.SerialName 5 | import kotlinx.serialization.Serializable 6 | import kotlinx.serialization.json.JsonClassDiscriminator 7 | import org.ton.api.adnl.AdnlAddressList 8 | 9 | @SerialName("validator.config.random.local") 10 | @Polymorphic 11 | @Serializable 12 | @JsonClassDiscriminator("@type") 13 | public data class ValidatorConfigRandomLocal( 14 | @SerialName("addr_list") 15 | val addrList: AdnlAddressList 16 | ) : ValidatorConfigLocal 17 | -------------------------------------------------------------------------------- /tonapi-tl/test/PrivateKeyTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.api.pk 2 | 3 | import io.ktor.util.* 4 | import org.ton.tl.asByteString 5 | import kotlin.test.Test 6 | import kotlin.test.assertContentEquals 7 | 8 | class PrivateKeyTest { 9 | @Test 10 | fun `test creation PublicKey`() { 11 | val privateKeyEd25519 = PrivateKeyEd25519("d53mOPj3+xx69TYJZ2LvzhxrNn32WBvt/ioV4Ha4gz8=".decodeBase64Bytes().asByteString()) 12 | val publicKeyEd25519 = privateKeyEd25519.publicKey() 13 | assertContentEquals( 14 | "4745ede03eb4ef607843359c1f206d061a5632f68caa6f63021aa23b400950fd".hexToByteArray(), 15 | publicKeyEd25519.key.toByteArray() 16 | ) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tvm/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("multiplatform") 3 | id("publish") 4 | } 5 | 6 | kotlin { 7 | sourceSets { 8 | commonMain { 9 | dependencies { 10 | api(projects.tonKotlinBitstring) 11 | api(projects.tonKotlinBigint) 12 | api(projects.tonKotlinCrypto) 13 | implementation(libs.serialization.json) 14 | api(libs.kotlinx.io) 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tvm/src/Tvm.kt: -------------------------------------------------------------------------------- 1 | package org.ton 2 | -------------------------------------------------------------------------------- /tvm/src/boc/BagOfCellsImpl.kt: -------------------------------------------------------------------------------- 1 | package org.ton.boc 2 | 3 | import io.ktor.utils.io.core.* 4 | import org.ton.cell.Cell 5 | 6 | internal data class BagOfCellsImpl( 7 | override val roots: List 8 | ) : BagOfCells, List by roots { 9 | constructor(root: Cell) : this(roots = listOf(root)) 10 | 11 | override fun iterator(): Iterator = iterator { 12 | yieldAll(roots) 13 | roots.forEach { root -> 14 | yieldAll(root.treeWalk()) 15 | } 16 | } 17 | 18 | override fun toByteArray(): ByteArray = buildPacket { 19 | writeBagOfCells(this@BagOfCellsImpl) 20 | }.readBytes() 21 | 22 | override fun toString(): String = buildString { 23 | roots.forEach { cell -> 24 | Cell.toString(cell, this) 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tvm/src/cell/CellContext.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("PackageDirectoryMismatch") 2 | 3 | package org.ton.kotlin.cell 4 | 5 | import org.ton.cell.Cell 6 | import org.ton.cell.CellBuilder 7 | import org.ton.cell.DataCell 8 | import org.ton.cell.VirtualCell 9 | 10 | public interface CellContext { 11 | public fun loadCell(cell: Cell): DataCell 12 | 13 | public fun finalizeCell(builder: CellBuilder): Cell 14 | 15 | public companion object { 16 | public val EMPTY: CellContext = object : CellContext { 17 | override fun loadCell(cell: Cell): DataCell { 18 | if (cell is DataCell) return cell 19 | if (cell is VirtualCell && cell.cell is DataCell) return cell.cell 20 | else throw IllegalArgumentException("Can't load ${cell::class} $cell") 21 | } 22 | 23 | override fun finalizeCell(builder: CellBuilder): Cell { 24 | return builder.build() 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /tvm/src/cell/EmptyCell.kt: -------------------------------------------------------------------------------- 1 | package org.ton.cell 2 | 3 | import org.ton.bitstring.BitString 4 | 5 | internal object EmptyCell : Cell { 6 | private val EMPTY_CELL_HASH = BitString( 7 | "96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7".hexToByteArray() 8 | ) 9 | 10 | override val bits: BitString = BitString.empty() 11 | override val refs: List = emptyList() 12 | override val descriptor: CellDescriptor = CellDescriptor(0, 0) 13 | 14 | override fun hash(level: Int): BitString = EMPTY_CELL_HASH 15 | 16 | override fun depth(level: Int): Int = 0 17 | 18 | override fun virtualize(offset: Int): Cell = this 19 | 20 | override fun toString(): String = "x{}" 21 | 22 | override fun equals(other: Any?): Boolean { 23 | if (other === this) return true 24 | if (other !is Cell) return false 25 | return descriptor == other.descriptor 26 | } 27 | 28 | override fun hashCode(): Int = 0 29 | } 30 | -------------------------------------------------------------------------------- /tvm/src/cell/VirtualCell.kt: -------------------------------------------------------------------------------- 1 | package org.ton.cell 2 | 3 | import org.ton.bitstring.BitString 4 | 5 | internal class VirtualCell( 6 | val cell: Cell, 7 | val offset: Int 8 | ) : Cell by cell { 9 | override val refs: List 10 | get() = cell.refs.map { it.virtualize(offset) } 11 | 12 | override val levelMask: LevelMask 13 | get() = cell.levelMask.virtualize(offset) 14 | 15 | override fun virtualize(offset: Int): Cell { 16 | return if (this.offset == offset) this 17 | else VirtualCell(cell, offset) 18 | } 19 | 20 | override fun hash(level: Int): BitString { 21 | return cell.hash(levelMask.apply(level).level) 22 | } 23 | 24 | override fun depth(level: Int): Int { 25 | return cell.depth(levelMask.apply(level).level) 26 | } 27 | 28 | override fun beginParse(): CellSlice { 29 | return CellSlice.beginParse(this) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tvm/src/cell/exception/exceptions.kt: -------------------------------------------------------------------------------- 1 | package org.ton.cell.exception 2 | 3 | public class CellOverflowException : RuntimeException { 4 | public constructor(message: String) : super(message) 5 | public constructor(cause: Throwable) : super("Cell overflow", cause) 6 | public constructor(message: String, cause: Throwable) : super(message, cause) 7 | } 8 | 9 | public class CellUnderflowException : RuntimeException { 10 | public constructor(message: String) : super(message) 11 | public constructor(cause: Throwable) : super("Cell underflow", cause) 12 | public constructor(message: String, cause: Throwable) : super(message, cause) 13 | } 14 | -------------------------------------------------------------------------------- /tvm/test/CellSliceTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.cell 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertEquals 5 | 6 | class CellSliceTest { 7 | @Test 8 | fun `extract ints from slice`() { 9 | Cell("0000010100000000000000457C_").parse { 10 | assertEquals(0, loadUInt(16).toInt()) 11 | assertEquals(1, loadUInt(8).toInt()) 12 | assertEquals(1, loadUInt(8).toInt()) 13 | assertEquals(69, loadUInt(64).toInt()) 14 | assertEquals(15, loadUInt(5).toInt()) 15 | } 16 | Cell("0000FF880FFFFFFFFFFFFFFDDC_").parse { 17 | assertEquals(0, loadInt(16).toInt()) 18 | assertEquals(-1, loadInt(8).toInt()) 19 | assertEquals(-15, loadInt(5).toInt()) 20 | assertEquals(1, loadInt(8).toInt()) 21 | assertEquals(-69, loadInt(64).toInt()) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tvm/test/PrunedBranchTest.kt: -------------------------------------------------------------------------------- 1 | package org.ton.cell 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertEquals 5 | 6 | class PrunedBranchTest { 7 | @Test 8 | fun test() { 9 | val cell = buildCell { 10 | storeBytes("000000000000000000deafbeaf123123".hexToByteArray()) 11 | storeRef(Cell.empty()) 12 | } 13 | 14 | val prunedBranch = CellBuilder.createPrunedBranch(cell, 0) 15 | assertEquals(cell.hash(), prunedBranch.hash(0)) 16 | assertEquals(cell.depth(0), prunedBranch.depth(0)) 17 | 18 | val virtualCell = cell.virtualize() 19 | assertEquals(cell.hash(), virtualCell.hash()) 20 | assertEquals(cell.depth(3), virtualCell.depth(3)) 21 | 22 | val virtualPrunedBranch = CellBuilder.createPrunedBranch(virtualCell, 0) 23 | assertEquals(prunedBranch, virtualPrunedBranch) 24 | } 25 | } 26 | --------------------------------------------------------------------------------