CSecp256k1.swift

🐧 linux: ready GitHub license Build Status GitHub release SPM compatible CocoaPods version Platform macOS | iOS | tvOS | watchOS | Linux

用于 bitcoin secp256k1 库 的 Swift C 模块。

安装

CSecp256k1.swift 部署到 macOS、iOS、tvOS、watchOS 和 Linux。 然而,它仅在最新的操作系统版本上进行了测试。 由于该模块使用的平台提供的 API 非常少,因此早期版本应该不会有太多问题。

设置说明

使用示例

签名和验证

import CSecp256k1

let privKey: [UInt8] = [
    0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba,
    0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49,
    0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac,
    0x03, 0x1c, 0xae, 0x7f, 0x60
]

let data = [UInt8](repeating: 0, count: 32)

let context: OpaquePointer! = secp256k1_context_create(
    UInt32(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY)
)

var signature = secp256k1_ecdsa_recoverable_signature()
let res = secp256k1_ecdsa_sign_recoverable(context, &signature, data, privKey, nil, nil)
assert(res > 0)

var pubKey = secp256k1_pubkey()
let res2 = secp256k1_ec_pubkey_create(context, &pubKey, privKey)
assert(res2 > 0)

var ssig = secp256k1_ecdsa_signature()
let res3 = secp256k1_ecdsa_recoverable_signature_convert(context, &ssig, &signature)
assert(res3 > 0)

let isValid = secp256k1_ecdsa_verify(context, &ssig, hash, &pubKey)
assert(isValid == 1)

许可

CSecp256k1.swift 可以被使用、分发和修改,根据 Apache 2.0 许可

Bitcoin secp256k1 可以被使用、分发和修改,根据 MIT 许可