Skip to content

Instantly share code, notes, and snippets.

@90K2
Created February 12, 2023 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 90K2/b88fe55f9dfc9457f7d894a4bc1aae0d to your computer and use it in GitHub Desktop.
Save 90K2/b88fe55f9dfc9457f7d894a4bc1aae0d to your computer and use it in GitHub Desktop.
import org.ton.api.pk.PrivateKeyEd25519
import org.ton.bigint.BigInt
import org.ton.block.*
import org.ton.boc.BagOfCells
import org.ton.cell.Cell
import org.ton.cell.CellBuilder
import org.ton.contract.wallet.WalletContract
import org.ton.contract.wallet.WalletTransfer
import org.ton.crypto.hex
import org.ton.hashmap.HashMapE
import org.ton.lite.api.LiteApi
import org.ton.tlb.CellRef
import org.ton.tlb.constructor.AnyTlbConstructor
import org.ton.tlb.constructor.tlbCodec
import org.ton.tlb.storeRef
import org.ton.tlb.storeTlb
import kotlin.math.pow
class HighloadWalletV2(
override val privateKey: PrivateKeyEd25519,
override val workchain: Int = 0,
override val subWalletId: Int = WalletContract.DEFAULT_WALLET_ID + workchain
) : AbstractWallet(privateKey, workchain, subWalletId) {
private fun generateQueryId(timeout: BigInt): BigInt {
return (BigInt.valueOf(utcLongNow()) + timeout).shiftLeft(32)
}
override fun createDataInit(): Cell = CellBuilder.createCell {
storeUInt(subWalletId, 32) // stored_subwallet
storeUInt(0, 64) // last_cleaned
storeBytes(privateKey.publicKey().key.toByteArray())
storeTlb(HashMapE.tlbCodec(16, Cell.tlbCodec()), HashMapE.empty()) // old_queries
}
override val code: Cell = CODE
override val address: AddrStd = address()
companion object {
// https://github.com/ton-blockchain/ton/blob/master/crypto/smartcont/highload-wallet-v2-code.fc
val CODE = BagOfCells(
hex("B5EE9C724101090100E5000114FF00F4A413F4BCF2C80B010201200203020148040501EAF28308D71820D31FD33FF823AA1F5320B9F263ED44D0D31FD33FD3FFF404D153608040F40E6FA131F2605173BAF2A207F901541087F910F2A302F404D1F8007F8E16218010F4786FA5209802D307D43001FB009132E201B3E65B8325A1C840348040F4438AE63101C8CB1F13CB3FCBFFF400C9ED54080004D03002012006070017BD9CE76A26869AF98EB85FFC0041BE5F976A268698F98E99FE9FF98FA0268A91040207A0737D098C92DBFC95DD1F140034208040F4966FA56C122094305303B9DE2093333601926C21E2B39F9E545A")
).first()
}
suspend fun transfer(liteApi: LiteApi, transfers: List<WalletTransfer>) {
require(transfers.isNotEmpty() && transfers.size <= 254) { throw BadRequestException("wrong transfers size") }
val message = createTransferMessage(
address(), createStateInit(), transfers
)
sendExternalMessage(liteApi, message)
}
fun createTransferMessage(
address: AddrStd,
stateInit: StateInit?,
transfers: List<WalletTransfer>
): Message<Cell> {
val info = ExtInMsgInfo(
src = AddrNone,
dest = address,
importFee = Coins()
)
val maybeStateInit =
Maybe.of(stateInit?.let { Either.of<StateInit, CellRef<StateInit>>(null, CellRef(it)) })
val transfersBody = transfers.mapIndexed { index, walletTransfer ->
index to walletTransfer
}.toMap()
val giftBody = CellBuilder.createCell {
storeUInt(subWalletId, 32)
storeUInt(generateQueryId(BigInt.valueOf(60)), 64)
storeRef(serializeMap(transfersBody, 16) { src, cb ->
cb.storeUInt(src.sendMode, 8)
cb.storeRef(MessageRelaxed.tlbCodec(AnyTlbConstructor), CellRef(createIntMsg(src)))
})
}
val body = Either.of<Cell, CellRef<Cell>>(null, CellRef(giftBody))
return Message(
info = info,
init = maybeStateInit,
body = body
)
}
fun createDeployMessage() = Message(
init = Maybe.of(
Either.of(null, CellRef(createStateInit())),
),
body = Either.of(
null,
CellRef(CellBuilder.createCell {
storeUInt(subWalletId, 32)
storeUInt(generateQueryId(BigInt.valueOf(2).pow(16)), 64)
storeTlb(HashMapE.tlbCodec(16, Cell.tlbCodec()), HashMapE.empty())
})
),
info = ExtInMsgInfo(
dest = address()
)
)
}
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.ton.api.pk.PrivateKeyEd25519
import org.ton.bitstring.BitString
import org.ton.block.*
import org.ton.cell.Cell
import org.ton.cell.CellBuilder
import org.ton.contract.wallet.WalletContract
import org.ton.contract.wallet.WalletTransfer
import org.ton.lite.api.LiteApi
import org.ton.tlb.CellRef
import org.ton.tlb.constructor.AnyTlbConstructor
import org.ton.tlb.storeRef
abstract class AbstractWallet(
open val privateKey: PrivateKeyEd25519,
open val workchain: Int = 0,
open val subWalletId: Int = WalletContract.DEFAULT_WALLET_ID + workchain
) : WalletContract {
public suspend fun transfer(
liteApi: LiteApi,
privateKey: PrivateKeyEd25519,
vararg transfers: WalletTransfer
): Unit = coroutineScope {
val seqno = async { kotlin.runCatching { getSeqno(liteApi) }.getOrNull() ?: 0 }
transfer(liteApi, privateKey, seqno.await(), *transfers)
}
private suspend fun transfer(
liteApi: LiteApi,
privateKey: PrivateKeyEd25519,
seqno: Int,
vararg transfers: WalletTransfer
) {
val message = createTransferMessage(
address = address,
stateInit = if (seqno == 0) createStateInit() else null,
privateKey = privateKey,
validUntil = Int.MAX_VALUE,
walletId = subWalletId,
seqno = seqno,
payload = transfers
)
sendExternalMessage(liteApi, message)
}
companion object {
fun createTransferMessage(
address: AddrStd,
stateInit: StateInit?,
privateKey: PrivateKeyEd25519,
walletId: Int,
validUntil: Int,
seqno: Int,
vararg payload: WalletTransfer
): Message<Cell> {
val info = ExtInMsgInfo(
src = AddrNone,
dest = address,
importFee = Coins()
)
val maybeStateInit =
Maybe.of(stateInit?.let { Either.of<StateInit, CellRef<StateInit>>(null, CellRef(it)) })
val giftBody = createGiftMessageBody(
privateKey,
walletId,
validUntil,
seqno,
*payload
)
val body = Either.of<Cell, CellRef<Cell>>(null, CellRef(giftBody))
return Message(
info = info,
init = maybeStateInit,
body = body
)
}
private fun createGiftMessageBody(
privateKey: PrivateKeyEd25519,
walletId: Int,
validUntil: Int,
seqno: Int,
vararg gifts: WalletTransfer
): Cell {
val unsignedBody = CellBuilder.createCell {
storeUInt(walletId, 32)
storeUInt(validUntil, 32)
storeUInt(seqno, 32)
storeUInt(0, 8) // OP_TRANSFER
for (gift in gifts) {
var sendMode = 3
if (gift.sendMode > -1) {
sendMode = gift.sendMode
}
val intMsg = CellRef(createIntMsg(gift))
storeUInt(sendMode, 8)
storeRef(MessageRelaxed.tlbCodec(AnyTlbConstructor), intMsg)
}
}
val signature = BitString(privateKey.sign(unsignedBody.hash()))
return CellBuilder.createCell {
storeBits(signature)
storeBits(unsignedBody.bits)
storeRefs(unsignedBody.refs)
}
}
fun createIntMsg(gift: WalletTransfer): MessageRelaxed<Cell> {
val info = CommonMsgInfoRelaxed.IntMsgInfoRelaxed(
ihrDisabled = true,
bounce = gift.bounceable,
bounced = false,
src = AddrNone,
dest = gift.destination,
value = gift.coins,
ihrFee = Coins(),
fwdFee = Coins(),
createdLt = 0u,
createdAt = 0u
)
val init = Maybe.of(gift.stateInit?.let {
Either.of<StateInit, CellRef<StateInit>>(null, CellRef(it))
})
val body = if (gift.body == null) {
Either.of<Cell, CellRef<Cell>>(Cell.of(), null)
} else {
Either.of<Cell, CellRef<Cell>>(null, CellRef(gift.body!!))
}
return MessageRelaxed(
info = info,
init = init,
body = body,
)
}
}
val asd = Any()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment