You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
USDT is a BTC token base on omni layer. As HDWallet is support BTC well, so support USDT is easy to implement.
To transfer USDT, we need to construct a transaction base on BTC, so we have two outputs.
transfer 546 Satoshi to destination
change to change address
a OP_RETURN output script to construct the USDT transaction.
Please check the code below:
public struct USDTTransactionBuilder: UtxoTransactionBuilderInterface {
public init(usdtLockingScript: Data) {
self.usdtLockingScript = usdtLockingScript
}
private let usdtLockingScript: Data
public func build(destinations: [(address: Address, amount: UInt64)], utxos: [UnspentTransaction]) throws -> UnsignedTransaction {
var outputs = try destinations.map { (address: Address, amount: UInt64) -> TransactionOutput in
guard let lockingScript = Script(address: address)?.data else {
throw TransactionBuildError.error("Invalid address type")
}
return TransactionOutput(value: amount, lockingScript: lockingScript)
}
let usdtTxOutput = TransactionOutput(value: 0, lockingScript: usdtLockingScript)
outputs.append(usdtTxOutput) //add a new script output
let unsignedInputs = utxos.map { TransactionInput(previousOutput: $0.outpoint, signatureScript: $0.output.lockingScript, sequence: UInt32.max) }
let tx = Transaction(version: 1, inputs: unsignedInputs, outputs: outputs, lockTime: 0)
return UnsignedTransaction(tx: tx, utxos: utxos)
}
}
static func generateUSDTLockingScript(amountInUSDT: Double) -> Data {
//
let converter = BitcoinConverter(bitcoinString: "\(amountInUSDT)")
let amountInSatoshi = converter.inSatoshi
var usdtData = Data()
usdtData += [0x6a,0x14] //OP_RETURN
usdtData += [0x6f,0x6d,0x6e,0x69] //OMNI
usdtData += [0x00,0x00] //Transaction version
usdtData += [0x00,0x00] // Transaction type
usdtData += [0x00,0x00,0x00,0x1f] //Currency identifier
ewLogger.debug("[USDT] amount data=\(amountInSatoshi.littleEndianData.toHexString())")
usdtData += amountInSatoshi.littleEndianData
return usdtData
}
USDT is a BTC token base on omni layer. As HDWallet is support BTC well, so support USDT is easy to implement.
To transfer USDT, we need to construct a transaction base on BTC, so we have two outputs.
Please check the code below:
some references links:
The text was updated successfully, but these errors were encountered: