Skip to content

Commit

Permalink
feat: use recoverable signature
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jun 12, 2019
1 parent fbf9ec0 commit bb2838c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
@rpc = CKB::RPC.new(host: host)
if mode == MODE::TESTNET
# Testnet system script code_hash
expected_code_hash = "0xf1951123466e4479842387a66fabfd6b65fc87fd84ae8e6cd3053edb27fff2fd"
expected_code_hash = "0x94334bdda40b69bae067d84937aa6bbccf8acd0df6626d4b9ac70d4612a11933"
# For testnet chain, we can assume the second cell of the first transaction
# in the genesis block contains default lock script we can use here.
system_cell_transaction = genesis_block.transactions.first
Expand Down
6 changes: 3 additions & 3 deletions lib/ckb/platform/ruby/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def initialize(privkey)
def sign(data)
privkey_bin = Utils.hex_to_bin(privkey)
secp_key = Secp256k1::PrivateKey.new(privkey: privkey_bin)
signature_bin = secp_key.ecdsa_serialize(
secp_key.ecdsa_sign(Utils.hex_to_bin(data), raw: true)
signature_bin, recid = secp_key.ecdsa_recoverable_serialize(
secp_key.ecdsa_sign_recoverable(Utils.hex_to_bin(data), raw: true)
)
Utils.bin_to_hex(signature_bin)
Utils.bin_to_hex(signature_bin + [recid].pack("C*"))
end

def self.random_private_key
Expand Down
2 changes: 1 addition & 1 deletion lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def sign(key, tx_hash)
blake2b.update(Utils.hex_to_bin(datum))
end
message = blake2b.hexdigest
data = [key.pubkey, key.sign(message)] + old_data
data = [key.sign(message)] + old_data
Types::Witness.from_h(data: data)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
end

let(:api) { CKB::API.new }
let(:lock_hash) { "0x07bab3ada3cc0ff223b387d7f5038ef57b335aa1d8dadc418d5d8ad3b19aeadb" }
let(:lock_hash) { "0xe94e4b509d5946c54ea9bc7500af12fd35eebe0d47a6b3e502127f94d34997ac" }

it "genesis block" do
expect(api.genesis_block).to be_a(Types::Block)
Expand Down
2 changes: 1 addition & 1 deletion spec/ckb/rpc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
end

let(:rpc) { CKB::RPC.new }
let(:lock_hash) { "0x07bab3ada3cc0ff223b387d7f5038ef57b335aa1d8dadc418d5d8ad3b19aeadb" }
let(:lock_hash) { "0xe94e4b509d5946c54ea9bc7500af12fd35eebe0d47a6b3e502127f94d34997ac" }

it "genesis block" do
result = rpc.genesis_block
Expand Down
9 changes: 3 additions & 6 deletions spec/ckb/types/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
expect(signed_tx.to_h[:witnesses]).to eq([
{
data: [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x304402202c643579e47045be050d3842ed9270151af8885e33954bddad0e53e81d1c2dbe02202dc637877a8302110846ebc6a16d9148c106e25f945063ad1c4d4db2b6952408"
"0x2c643579e47045be050d3842ed9270151af8885e33954bddad0e53e81d1c2dbe2dc637877a8302110846ebc6a16d9148c106e25f945063ad1c4d4db2b695240800",
]
}
])
Expand Down Expand Up @@ -143,15 +142,13 @@
expect(signed_tx.witnesses.map(&:to_h)).to eq([
{
data: [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x3044022068a57373f4e98aecfb9501ec1cc4a78c048361332e4b6706bdc1469d30bd52ea022042feca657dd1de1eff384e6ed24a6910b011d49d855bd1ed209f5ce77d8116ac",
"0x68a57373f4e98aecfb9501ec1cc4a78c048361332e4b6706bdc1469d30bd52ea42feca657dd1de1eff384e6ed24a6910b011d49d855bd1ed209f5ce77d8116ac01",
"0x4107bd23eedb9f2a2a749108f6bb9720d745d50f044cc4814bafe189a01fe6fb"
]
},
{
data: [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x304402203b13c362f254e7becb0e731e4756e742bfddbf2f5d7c16cd609ba127d2b7e07f02201d588c3a7132fc20c478e2de14f6370fbb9e4402d240e4b32c8d671177e1f311"
"0x3b13c362f254e7becb0e731e4756e742bfddbf2f5d7c16cd609ba127d2b7e07f1d588c3a7132fc20c478e2de14f6370fbb9e4402d240e4b32c8d671177e1f31101"
]
}
])
Expand Down

0 comments on commit bb2838c

Please sign in to comment.