测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2017年8月 |
SwiftSwift 版本 | 4.0 |
SPM支持 SPM | ✗ |
由 Logan Sease 维护。
要运行示例项目,请克隆仓库,并首先在 Example 目录下运行 pod install
。
此项目使用 Swift 4,只能在 XCode 9+ 上运行(当时处于测试阶段)。要下载 xcode 9 的测试版,请参阅 developer.apple.com/ios。
对于 Swift 3 兼容版本,请参阅发布版本 v0.1.9。
BlockstackCoreApi 可通过 CocoaPods 获取。要安装它,只需将以下行添加到你的 Podfile 即可。
pod "BlockstackCoreApi-iOS"
如果你的应用使用 Blockstack 授权,请完成以下设置步骤。
1- 为你的应用创建一个唯一的自定义 URL 架构来处理 blockstack 的授权回调。在你的 info.plist 中,你必须添加以下条目,以允许 blockstack 应用在授权后打开你的应用程序。你还必须添加一个 blockstack 回调 URL 参数。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>bsk[UNIQUEIDENTIFIER]</string>
</array>
</dict>
</array>
<key>BlockstackCompletionUri</key>
<string>bsk[UNIQUEIDENTIFIER]://auth</string>
2- 将 blockstack 添加到你的查询方案中,以便应用可以验证 blockstack 是否已安装。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>blockstack</string>
</array>
3- 在你的应用代理的 did open url 方法中,你必须传递 blockstack 的授权响应。
//in order to complete the authorization process we must call the browser auth openURL method so it may
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let scheme = url.scheme, scheme.contains("bsk")
{
return BlockstackAuth.application(app, open: url, options: options)
}
return false
}
4- 现在,你可以使用以下内容验证 blockstack 应用是否已安装,并完成授权。
//verify blockstack is installed.
guard BlockstackAuth.canAuthorize() == true else
{
return
}
有关安装门户应用程序的更多信息,请参阅:https://github.com/BedKin/BlockstackBrowser-ios
调用 blockstack 进行授权
BlockstackAuth.authorize() { (token) in
if let token = token
{
//app authorized. Use the provided token
}
}
登出
BlockstackAuth.logout()
登录后获取当前用户配置文件和名称
if let name = BlockstackAuth.currentUserProfile()?.name
{
//display user name
}
//check that we are logged in and serialize some object into data then write it to storage
if BlockstackAuth.loggedIn() == true, let data = myDataObject.serialize()
{
BlockstackStorage.shared().writeToStorage(resourceIdentifier: "MY_DATA_FILE", data: data , handler: { (writtenData, error) in
if let _ = writtenData
{
//the data was written to file
}
}
})
//check that we are logged in, and if so then read our data from storage and then deserialize it into
//our data object
if BlockstackAuth.loggedIn() == true
{
BlockstackStorage.shared().readFromStorage(resourceIdentifier: "MY_DATA_FILE", handler: { (readData, error) in
if let deserialized = Object.deserialize(from: readData)
{
//our data has been read and objects deserialized
}
})
}
可以通过 CoreApi 访问 Blockstack 核心API。有关所有 API 方法及文档的列表,请参阅 core.blockstack.org。
func ping()
{
CoreApi.ping { (result, error) in }
}
func allNames()
{
CoreApi.allNames { (result, error) in }
}
func nameInfo(){
CoreApi.nameInfo(for: "muneeb.id", { (result, error) in })
}
func zoneFile(){
CoreApi.zoneFile(for: "muneeb.id", with: "b100a68235244b012854a95f9114695679002af9", { (result, error) in })
}
func namesOwned(){
CoreApi.namesOwned(on: "bitcoin", for: "1Q3K7ymNVycu3TQoTDUaty8Q5fUVB3feEQ", { (result, error) in })
}
func allNamespaces(){
CoreApi.allNamespaces({ (result, error) in })
}
func namespaceNames(){
CoreApi.namespaceNames(namespace: "id") { (result, error) in }
}
func namespacePrice(){
CoreApi.namespacePrice(namespace: "cnn") { (result, error) in }
}
func namePrice(){
CoreApi.namePrice(name: "logan.id") { (result, error) in }
}
func consensusHash(){
CoreApi.consensusHash(blockchain: "bitcoin") { (result, error) in }
}
func pendingTransactions(){
CoreApi.pendingTransactions(blockchain: "bitcoin") { (result, error) in }
}
func userProfile(){
CoreApi.userProfile(username: "fredwilson") { (result, error) in }
}
func search(){
CoreApi.search(query: "taylor") { (result, error) in }
}
jsontokens-js 已通过 JavaScriptCore 框架集成到此库中,并封装在 TokenSigner 类中
let token = ["iat" : NSNumber(value: 1440713414.85)]
let privateKey = "278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f"
let publicKey = "03fdd57adec3d438ea237fe46b33ee1e016eda6b585c3e27ea66686c2ea5358479"
//sign a token
let signed = TokenSigner.shared().sign(tokenPayload: token, privateKey: privateKey)!
//decode a signed token
let decoded = TokenSigner.shared().decodeToken(signed) as! [String : Any]
//sign a token without a private key
let unsecured = TokenSigner.shared().createUnsecuredToken(tokenPayload: token)!
//verify a signed token via public key
let verified = TokenSigner.shared().verify(token: signed, publicKey: publicKey)
一些重要的 JWT / 私钥 / 公钥方法已被包含在 JavaScript 封装器中,可以按以下方式访问
let pk = CryptoUtils.shared().makeECPrivateKey()
let pubKey = CryptoUtils.shared().derivePublicKey(privateKey: pk)
let did = CryptoUtils.shared().makeDID(from: pubKey)
let uuid = CryptoUtils.shared().makeUUID4()
let address = CryptoUtils.shared().address(from: pubKey)
//generate a new recovery passphrase (from a new private key)
let passphrase = CryptoUtils.shared().generatePassphrase()
//verify a passphrase
let verifiedPass = CryptoUtils.shared().validatePassphrase(passphrase)
//get the private key from a generated pass phrase
let passphraseKey = CryptoUtils.shared().privateKey(from: passphrase)
有关如何重建 jsontokens.js 文件以更新 jsontokens-js 的信息,请参阅文件 Resources/BlockstackCoreApi/Assets/howto-rebuild-jsontoken.txt
Logan Sease,[email protected]
BlockstackCoreApi 根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。