MinterCore 1.2.7

MinterCore 1.2.7

Alexey Sidorov维护。



 
依赖
Alamofire= 4.7.3
ObjectMapper~> 3.1
BigInt~> 3.0
CryptoSwift~> 1.0
secp256k1.swift~> 0.1
 

  • sidorov.panda

Version CI Status Version Platform License Last commit

MinterCore

示例

要运行示例项目,先克隆仓库,然后从Example目录运行pod install

安装

MinterCore可通过CocoaPods获取。要安装

它,只需将以下行添加到Podfile中

pod 'MinterCore'

关于

这是一个用于与Minter区块链进行工作的纯Swift SDK

使用MinterAPI

您可以在Minter节点API获取所有有效响应和完整文档

初始化SDK

///Minter SDK initialization
import MinterCore

let nodeUrlString = "https://minter-node-1.testnet.minter.network:8841" // example of a node url

MinterCoreSDK.initialize(urlString: nodeUrlString)

地址

返回地址的代币列表、余额和交易数(用于nonce)。

public func address(_ address: String, height: String = "0", with completion: (([String: Any]?, Error?) -> ())?)
示例
AccountManager.default.address("Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99", with: { (data, err) in
  //["balance": ["MNT":"10000000000"], "transaction_count":0]
})

nonce

使用address方法获取nonce

send

返回发送已签名交易的结果

public func send(tx: String, completion: ((String?, String?, Error?) -> ())?)
示例
TransactionManager.default.send(tx: "Mt...") { (hash, statusText, error) in

}

状态

返回节点状态信息。

public func status(with completion: (([String : Any]?, Error?) -> ())?)

验证者

返回活动验证者列表。

public func validators(height: Int, with completion: (([[String: Any]]?, Error?) -> ())?)

estimateCoinBuy

返回购买硬币交易估计。

public func estimateCoinBuy(fromId: Int, toId: Int, amount: Decimal, completion: ((Decimal?, Decimal?, Error?) -> ())?)

estimateCoinSell

返回销售硬币交易估计。

public func estimateCoinSell(fromId: Int, toId: Int, amount: Decimal, completion: ((Decimal?, Decimal?, Error?) -> ())?)

硬币信息

返回硬币信息。注意:此方法不返回关于基本硬币(MNT和BIP)的信息。

public func info(symbol: String, height: String = "0", completion: ((Coin?, Error?) -> ())?)

区块

返回给定高度的区块数据。

public func blocks(height: String = "0", with completion: (([String : Any]?, Error?) -> ())?)

事件

返回给定高度的的事件。

public func events(height: String = "0", with completion: (([String : Any]?, Error?) -> ())?)

transaction

返回交易信息。

public func transaction(hash: String, completion: ((Transaction?, Error?) -> ())?)

candidate

通过提供的public_key返回候选人的信息。如果找不到候选人,将返回404状态码。

public func candidate(publicKey: String, height: String = "0", completion: (([String : Any]?, Error?) -> ())?)

candidates

返回候选人列表。

height为可选参数。

public func candidates(height: String = "0", includeStakes: Bool = false, completion: (([[String : Any]]?, Error?) -> ())?)

estimateTxCommission

返回交易估算费用。

public func estimateCommission(for rawTx: String, height: String = "0", completion: ( (Decimal?, Error?) -> ())?)

transactions

通过查询返回交易。

public func transaction(query: String, completion: (([Transaction]?, Error?) -> ())?)

unconfirmedTransactions

返回未确认的交易。

public func unconfirmedTransaction(limit: String = "0", completion: (([String : Any]?, Error?) -> ())?)

使用MinterSDK

签名交易

返回一个已签名的交易。

示例
  • 签名发送货币交易
let sendData = SendCoinRawTransactionData(to: "Mx6b6b3c763d2605b842013f84cac4d670a5cb463d", value:
BigUInt(decimal: 1 * TransactionCoinFactorDecimal)!, coinId: 0).encode()

let rawTransaction = SendCoinRawTransaction(nonce: BigUInt(1), chainId: 2, gasCoinId: 0, data: sendData!)

let mnemonic = "adjust correct photo fancy knee lion blur away coconut inform sun cancel"

let seed = String.seedString(mnemonic)!
let pk = PrivateKey(seed: Data(hex: seed))

guard let key = try! pk.derive(at: 44, hardened: true).derive(at: 60, hardened: true).derive(at: 0, hardened: true).derive(at: 0).derive(at: 0).raw.toHexString()

/// Signing raw transaction
let signedTx = RawTransactionSigner.sign(rawTx: rawTransaction, privateKey: key)!

/// Sending raw transaction
transactionManager.send(tx: "Mt" + signedTx) { (txHash, resultText, error) in
  print(txHash)
  print(resultText)
  print(error)
}
示例
  • 签名出售货币交易
let gasCoinId = 0
let nonce = BigUInt(1)
let coinFromId = 0
let coinToId = 1
let value = BigUInt(1)
let minimumValue = BigUInt(0)
let tx = SellCoinRawTransaction(nonce: nonce, chainId: 2, gasCoinId: gasCoinId, coinFromId: coinFromId, coinToId: coinToId, value: value, minimumValueToBuy: minimumValue)
示例
  • 签名全部出售货币交易
let gasCoinId = 0
let nonce = BigUInt(1)
let coinFromId = 0
let coinToId = 1
let minimumValue = BigUInt(0)
let tx = SellAllCoinsRawTransaction(nonce: nonce, chainId: 2, gasCoinId: gasCoinId, coinFromId: coinFromId, coinToId: coinToId, minimumValueToBuy: minimumValue)
示例
  • 签名购买货币交易
let gasCoinId = 0
let nonce = BigUInt(1)
let coinFromId = 0
let coinToId = 1
let value = BigUInt(1)
let maximumValue = BigUInt(0)
let tx = BuyCoinRawTransaction(nonce: nonce, chainId: 2, gasCoinId: gasCoinId, coinFromId: coinFromId, coinToId: coinToId, value: value, maximumValueToSell: maximumValue)
示例
  • 签名创建货币交易
let name = "Name"
let symbol = "SYMBOL"
let amount = BigUInt(1000)
let reserve = BigUInt(300000000000000000000000)
let ratio = BigUInt(70)
let maxSupply = BigUInt(1000000000000000) 

let data = CreateCoinRawTransactionData(
  name: name,
  symbol: symbol,
  initialAmount: initialAmount,
  initialReserve: initialReserve,
  reserveRatio: reserveRatio,
  maxSupply: maxSupply
)
let tx = CreateCoinRawTransaction(nonce: nonce, chainId: 2, gasCoinId: 0, data: data.encode()!)
示例
  • 签名声明候选资格交易
let nonce = BigUInt(1)
let gasCoinId = 0
let coinId = 0
let address = "Mx7633980c000139dd3bd24a3f54e06474fa941e16"
let publicKey = "Mp91cab56e6c6347560224b4adaea1200335f34687766199335143a52ec28533a5"
let commission = BigUInt(1)
let stake = BigUInt(2)
let model = DeclareCandidacyRawTransaction(
  nonce: nonce,
  chainId: 2,
  gasCoinId: gasCoinId,
  address: address,
  publicKey: publicKey,
  commission: commission,
  coinId: coinId,
  stake: stake
)
示例
  • 签名 委托 交易
let tx = DelegateRawTransaction(
  nonce: BigUInt(1),
  chainId: 2,
  gasCoinId: 0,
  publicKey: "Mp91cab56e6c6347560224b4adaea1200335f34687766199335143a52ec28533a5",
  coinId: 0,
  value: BigUInt(1000)
)
示例
  • 签名 设置候选人状态为开启 交易
let nonce = BigUInt(1)
let gasCoinId = 0
let publicKey = "Mpeadea542b99de3b414806b362910cc518a177f8217b8452a8385a18d1687a80b"
let model = SetCandidateOnlineRawTransaction(nonce: nonce, chainId: 2, gasCoinId: gasCoinId, publicKey: publicKey)
示例
  • 签名 设置候选人状态为关闭 交易
let nonce = BigUInt(1)
let gasCoinId = 0
let publicKey = "Mpeadea542b99de3b414806b362910cc518a177f8217b8452a8385a18d1687a80b"
let model = SetCandidateOfflineRawTransaction(nonce: nonce, chainId: 2, gasCoinId: gasCoinId, publicKey: publicKey)
示例
  • 签名 赎回支票 交易
let tx = RedeemCheckRawTransaction(
  nonce: BigUInt(1),
  chainId: 2,
  gasCoinId: 0,
  rawCheck: <Check data>,
  proof: <Proof Data>)
示例
  • 签名 解绑 交易
let coinId = 0
let publicKey = "91cab56e6c6347560224b4adaea1200335f34687766199335143a52ec28533a5"
let value = BigUInt(2)
let model = UnbondRawTransactionData(publicKey: publicKey, coinId: coinId, value: value)
示例
  • 签名 编辑候选人 交易
let pk = "Mpc5b635cde82f796d1f8320efb8ec634f443e6b533a973570e4b5ea04aa44e96d"
let address1 = "Mxe7ca647d17599d3e83048830fbb2df3726a7d22c"
let address2 = "Mxa8ca647d17599d3e83048830fbb2df3726a7d215"
let model = EditCandidateRawTransaction(nonce: BigUInt(1), chainId: 2, gasCoinId: 0, publicKey: pk, rewardAddress: address1, ownerAddress: address2)

创建铸造者支票

示例
  • 创建检查
let nonce = "1"
let dueBlock = BigUInt(99)
let coinId = 0
let value = BigUInt(1)
let phrase = "123"
let tx = IssueCheckRawTransaction(nonce: nonce, dueBlock: dueBlock, coinId: coinId, value: value, gasCoinId: coinId, passPhrase: phrase)
tx.data = data.encode()!
let result = RawTransactionSigner.sign(rawTx: tx, privateKey: <Private Key>)
  • 创建证明
let proof = RawTransactionSigner.proof(address: "Mxe7ca647d17599d3e83048830fbb2df3726a7d22c", passphrase: "some pass phrase")

铸币钱包

  • 生成助记词。
let mnemonics = String.generateMnemonicString()
  • 从助记词获取种子。
let res = RawTransactionSigner.seed(from: mnemonic, passphrase: "", language: .english)
  • 从种子获取私钥。
let privateKey = PrivateKey(seed: Data(hex: seed))
let key = try? privateKey
  .derive(at: 44, hardened: true)
  .derive(at: 60, hardened: true)
  .derive(at: 0, hardened: true)
  .derive(at: 0)
  .derive(at: 0)
  • 从私钥获取公钥。
let publicKey = RawTransactionSigner.publicKey(privateKey: privateKey!.raw, compressed: false)!.dropFirst()
  • 从公钥获取Minter地址。
let address = RawTransactionSigner.address(publicKey: publicKey)

作者

sidorov.panda, [email protected]

许可

MinterCore可在MIT许可下使用。有关更多信息,请参阅LICENSE文件。