DEC Documentation

Logo

Decentralized Ecosystem Cooperation

View the Project on GitHub dececo/docs

参考文档

官方文档地址 https://loopring.github.io/relay-cluster/relay_api_spec_v2.html#loopring_notifytransactionsubmitted

接口

loopring_notifyTransactionSubmitted

参数含义

其中,input, v, r, s含义如下:

input

input是交易时的data(详见 https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction)。 普通交易时data是附带的二进制信息,创建合约时data为合约代码。

参照路印的实现 data 经过 web3swift对实体encode得到。而这个实体有方法名(string),输入参数(array),输出参数(arry)三个属性 实体构造分为 以下几种情况得到

vrs

组装待签名消息

因loopring_notifyTransactionSubmitted接口未实现,该部分选用loopring_submitOrder作为示例,接口文档见 https://loopring.github.io/relay-cluster/relay_api_spec_v2.html#loopring_submitorder

    func getOrderHash(order: OriginalOrder) -> Data {
        var result: Data = Data()
        result.append(contentsOf: order.delegate.hexBytes)
        result.append(contentsOf: order.address.hexBytes)
        let tokens = TokenDataManager.shared.getAddress(by: order.tokenSell)!
        result.append(contentsOf: tokens.hexBytes)
        let tokenb = TokenDataManager.shared.getAddress(by: order.tokenBuy)!
        result.append(contentsOf: tokenb.hexBytes)
        result.append(contentsOf: order.walletAddress.hexBytes)
        result.append(contentsOf: order.authAddr.hexBytes)
        result.append(contentsOf: _encode(order.amountSell, order.tokenSell))
        result.append(contentsOf: _encode(order.amountBuy, order.tokenBuy))
        result.append(contentsOf: _encode(order.validSince))
        result.append(contentsOf: _encode(order.validUntil))
        result.append(contentsOf: _encode(order.lrcFee, "LRC"))
        let flag: [UInt8] = order.buyNoMoreThanAmountB ? [1] : [0]
        result.append(contentsOf: flag)
        result.append(contentsOf: [order.marginSplitPercentage])
        return result
    }
keccak256加密(通用,与具体交易和参数无关)
    open class func sign(message: Data, keystore: GethKeyStore, account: GethAccount, passphrase: String) -> (SignatureData?, String?) {
        let hashedMessage = message.sha3(SHA3.Variant.keccak256)
        let header: Data = "\u{0019}Ethereum Signed Message:\n32".data(using: .utf8)!
        let newData: Data = header + hashedMessage
        let secret = newData.sha3(SHA3.Variant.keccak256)
        
        do {
            // TODO:- Add timed Unlock
            _ = try? keystore.unlock(account, passphrase: passphrase)
            let accountAddress = account.getAddress()
            print("############### use address \(accountAddress?.getHex()) to sign")
            let hashedSignedMessage = try keystore.signHash(accountAddress, hash: secret)
            // let hashedSignedMessage = try keystore.signHash(accountAddress, hash: hashedMessage)
            let r = hashedSignedMessage.subdata(in: Range(0..<32))
            let s = hashedSignedMessage.subdata(in: Range(32..<64))
            let v = hashedSignedMessage.subdata(in: Range(64..<65)) // TODO:- Use length
        
            if let recId = Int(v.toHexString()) {
                let headerByte = recId + 27
                let hash = "0x" + hashedMessage.hexString
                let signature = (SignatureData(v: headerByte, r: r, s: s))
                return (signature, hash)
            } else {
                return (nil, nil)
            }
        } catch {
            print("Failed to signhash \(error)")
            return (nil, nil)
        }
    }

返回值