Module: Bitcoin::Secp256k1::Native
- Extended by:
- Secp256k1
- Defined in:
- lib/bitcoin/secp256k1/native.rb
Overview
binding for secp256k1 (https://github.com/bitcoin-core/secp256k1/) tag: v0.8.0, which secp256k1rb 0.8.0 binds the public API of. An older library is missing symbols the gem attaches, such as secp256k1_ec_pubkey_sort and the musig module. this is not included by default, to enable set shared object path to ENV for linux, ENV = '/usr/local/lib/libsecp256k1.so' or '/usr/lib64/libsecp256k1.so' for mac,
Class Method Summary collapse
-
.generate_key(compressed: true) ⇒ Object
generate bitcoin key object.
-
.native? ⇒ Boolean
Whether this module is native c wrapper or not?.
-
.recover_compact(data, signature, compressed) ⇒ Bitcoin::Key
Recover public key from compact signature.
-
.sign_compact(data, privkey) ⇒ Array[signature, recovery id]
Sign data with compact format.
-
.sign_data(data, private_key, extra_entropy = nil, algo: :ecdsa) ⇒ String
Sign to data.
-
.sp_available? ⇒ Boolean
Whether the loaded library supports BIP-352 silent payments.
-
.sp_create_label(scan_key, m) ⇒ Array
Create the label and the label tweak of the
mth label ofscan_key. -
.sp_create_outputs(recipients, outpoint_smallest, plain_seckeys: [], taproot_seckeys: []) ⇒ Array
Create the silent payment outputs for
recipients. -
.sp_scan_outputs(tx_outputs, scan_key, outpoint_smallest, spend_pubkey, plain_pubkeys: [], xonly_pubkeys: [], labels: {}) ⇒ Array
Scan
tx_outputsfor the silent payment outputs of the recipient. -
.verify_sig(data, signature, pubkey, algo: :ecdsa) ⇒ Boolean
Verify signature.
Class Method Details
.generate_key(compressed: true) ⇒ Object
generate bitcoin key object
27 28 29 30 |
# File 'lib/bitcoin/secp256k1/native.rb', line 27 def generate_key(compressed: true) privkey, pubkey = generate_key_pair(compressed: compressed) Bitcoin::Key.new(priv_key: privkey, pubkey: pubkey, compressed: compressed) end |
.native? ⇒ Boolean
Whether this module is native c wrapper or not?
22 23 24 |
# File 'lib/bitcoin/secp256k1/native.rb', line 22 def native? true end |
.recover_compact(data, signature, compressed) ⇒ Bitcoin::Key
Recover public key from compact signature.
46 47 48 49 |
# File 'lib/bitcoin/secp256k1/native.rb', line 46 def recover_compact(data, signature, compressed) pubkey = recover(data, signature, compressed) Bitcoin::Key.new(pubkey: pubkey, compressed: compressed) end |
.sign_compact(data, privkey) ⇒ Array[signature, recovery id]
Sign data with compact format.
36 37 38 39 |
# File 'lib/bitcoin/secp256k1/native.rb', line 36 def sign_compact(data, privkey) sig, rec_id = sign_recoverable(data, privkey) [ECDSA::Signature.new(sig[0...64].to_i(16), sig[64..-1].to_i(16)), rec_id] end |
.sign_data(data, private_key, extra_entropy = nil, algo: :ecdsa) ⇒ String
Sign to data.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bitcoin/secp256k1/native.rb', line 58 def sign_data(data, private_key, extra_entropy = nil, algo: :ecdsa) case algo when :ecdsa begin sign_ecdsa(data, private_key, extra_entropy) rescue ArgumentError false end when :schnorr begin sign_schnorr(data, private_key, extra_entropy) rescue ArgumentError false end else raise ArgumentError, "unknown algo: #{algo}" end end |
.sp_available? ⇒ Boolean
Whether the loaded library supports BIP-352 silent payments. The module exists in libsecp256k1 v0.8.0 or later.
106 107 108 |
# File 'lib/bitcoin/secp256k1/native.rb', line 106 def sp_available? silentpayments_available? end |
.sp_create_label(scan_key, m) ⇒ Array
Create the label and the label tweak of the m th label of scan_key.
128 129 130 |
# File 'lib/bitcoin/secp256k1/native.rb', line 128 def sp_create_label(scan_key, m) silentpayments_create_label(scan_key, m) end |
.sp_create_outputs(recipients, outpoint_smallest, plain_seckeys: [], taproot_seckeys: []) ⇒ Array
Create the silent payment outputs for recipients.
format(33 bytes). A recipient may appear more than once.
with hex format(36 bytes).
119 120 121 122 |
# File 'lib/bitcoin/secp256k1/native.rb', line 119 def sp_create_outputs(recipients, outpoint_smallest, plain_seckeys: [], taproot_seckeys: []) silentpayments_sender_create_outputs( recipients, outpoint_smallest, plain_seckeys: plain_seckeys, taproot_seckeys: taproot_seckeys) end |
.sp_scan_outputs(tx_outputs, scan_key, outpoint_smallest, spend_pubkey, plain_pubkeys: [], xonly_pubkeys: [], labels: {}) ⇒ Array
Scan tx_outputs for the silent payment outputs of the recipient.
with hex format(36 bytes).
143 144 145 146 147 148 149 |
# File 'lib/bitcoin/secp256k1/native.rb', line 143 def sp_scan_outputs(tx_outputs, scan_key, outpoint_smallest, spend_pubkey, plain_pubkeys: [], xonly_pubkeys: [], labels: {}) summary = silentpayments_create_prevouts_summary( outpoint_smallest, plain_pubkeys: plain_pubkeys, xonly_pubkeys: xonly_pubkeys) silentpayments_scan_outputs( tx_outputs, scan_key, summary, spend_pubkey, labels: labels.empty? ? nil : labels) end |
.verify_sig(data, signature, pubkey, algo: :ecdsa) ⇒ Boolean
Verify signature.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bitcoin/secp256k1/native.rb', line 84 def verify_sig(data, signature, pubkey, algo: :ecdsa) case algo when :ecdsa begin verify_ecdsa(data, signature, pubkey) rescue ArgumentError false end when :schnorr begin verify_schnorr(data, signature, pubkey) rescue ArgumentError false end else raise ArgumentError, "unknown algo: #{algo}" end end |