Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for usdt #44

Open
mousehaohao opened this issue May 28, 2019 · 0 comments
Open

support for usdt #44

mousehaohao opened this issue May 28, 2019 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@mousehaohao
Copy link

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.

  1. transfer 546 Satoshi to destination
  2. change to change address
  3. 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
    }

some references links:

@impul impul self-assigned this May 28, 2019
@impul impul added the enhancement New feature or request label May 28, 2019
@impul impul pinned this issue May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants