xssnick / tonutils-go

TON SDK Library in pure Golang for interacting with The Open Network ecosystem using native protocols, such as ADNL, RLDP and etc.
Apache License 2.0
464 stars 95 forks source link

User-friendly to Raw adress conversion/comparison #180

Closed kvizyx closed 4 months ago

kvizyx commented 4 months ago

Hi! In a nutshell, i'm building an app that detect transactions from specific addresses set by user so i need to compare user-friendly addresses (that user send to app) with raw addresses from subscription channel:

senderUFAddr := "sender-user-friendly-address"

go api.SubscribeOnTransactions(context.Background(), receiverAddress, lastProcessedLT, transactions)

for tx := range transactions {
    if tx.IO.In == nil || tx.IO.In.Msg == nil {
        fmt.Println("not an in message")
        continue
    }

    internalMessage, ok := tx.IO.In.Msg.(*tlb.InternalMessage)
    if !ok {
        fmt.Println("not an internal message")
        continue
    }

    senderRawAddr := internalMessage.SrcAddr.String()

    lastProcessedLT = tx.LT
}

How can i do this comparison with tonutils or i have to implement it on my own?

xssnick commented 4 months ago

Hi, to make raw address you could do this way:

raw := fmt.Sprintf("%d:%x", internalMessage.SrcAddr.Workchain(), internalMessage.SrcAddr.Data())
xssnick commented 4 months ago
internalMessage.SrcAddr.String()

returns user friendly address

kvizyx commented 4 months ago

I think i didn't put it that way. When user want to detect transactions from some address and send it to the app (0QAqow_s3o17yQk8h6QXVvE0yw_7IVzohVK1qcHfTCx_wVe_ for example) then i need to compare SrcAddr of each transaction to this address to detect whether it's transaction from address that user gave to us or not. But when i get the address from the transaction it's a bit different from one that user sent (EQAqow_s3o17yQk8h6QXVvE0yw_7IVzohVK1qcHfTCx_wbHw for the address shown in the example above) so it's a problem for me how to compare them. If anything, I'm new to TON, so I apologize in advance if this is a stupid question :)

xssnick commented 4 months ago

This is because address flags, your address which starts from 0 is address with testnet and nonboubce flags, you could set flag to src address too using SetTestnetOnly(true) and SetBounce(false), then call String(), then they should be the same.

kvizyx commented 4 months ago

Thanks!