forked from cryptape/ckb-auth
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix BTC compressed flags issue (#34)
* Support unisat (ckb-auth-cli) * Fix bitcoin compress
- Loading branch information
Showing
8 changed files
with
201 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::{auth_script::run_auth_exec, utils::decode_string, BlockChain, BlockChainArgs}; | ||
use anyhow::{anyhow, Error}; | ||
use bitcoin::bech32::{self, FromBase32}; | ||
use ckb_auth_rs::AuthAlgorithmIdType; | ||
use clap::{arg, ArgMatches, Command}; | ||
|
||
pub struct UnisatLockArgs {} | ||
|
||
impl BlockChainArgs for UnisatLockArgs { | ||
fn block_chain_name(&self) -> &'static str { | ||
"unisat" | ||
} | ||
|
||
fn reg_parse_args(&self, cmd: Command) -> Command { | ||
cmd | ||
} | ||
fn reg_generate_args(&self, cmd: Command) -> Command { | ||
cmd | ||
} | ||
fn reg_verify_args(&self, cmd: Command) -> Command { | ||
cmd.arg(arg!(-a --address <PUBKEYHASH> "The unisat address")) | ||
.arg(arg!(-s --signature <SIGNATURE> "The signature to verify")) | ||
.arg(arg!(-m --message <MESSAGE> "The signature message")) | ||
} | ||
|
||
fn get_block_chain(&self) -> Box<dyn BlockChain> { | ||
Box::new(UnisatLock {}) | ||
} | ||
} | ||
|
||
pub struct UnisatLock {} | ||
|
||
impl BlockChain for UnisatLock { | ||
fn parse(&self, _operate_mathches: &ArgMatches) -> Result<(), Error> { | ||
Err(anyhow!("unisat does not parse")) | ||
} | ||
|
||
fn generate(&self, _operate_mathches: &ArgMatches) -> Result<(), Error> { | ||
Err(anyhow!("unisat does not generate")) | ||
} | ||
|
||
fn verify(&self, operate_mathches: &ArgMatches) -> Result<(), Error> { | ||
let address = operate_mathches | ||
.get_one::<String>("address") | ||
.expect("Get address from args"); | ||
let (_hrp, address, _v) = bech32::decode(&address).expect("decode bech32"); | ||
let address = Vec::<u8>::from_base32(&address[1..33]).unwrap(); | ||
|
||
let mut signature = decode_string( | ||
operate_mathches | ||
.get_one::<String>("signature") | ||
.expect("Get signature from args"), | ||
"base64", | ||
) | ||
.expect("decode signature from base64 string"); | ||
|
||
let message = hex::decode( | ||
operate_mathches | ||
.get_one::<String>("message") | ||
.expect("Get message from args"), | ||
) | ||
.expect("decode message"); | ||
|
||
if address.len() < 20 { | ||
return Err(anyhow!("unisat address invalidate")); | ||
} | ||
if signature.len() != 65 { | ||
return Err(anyhow!("unisat signature size is not 65")); | ||
} | ||
signature[0] += 4; | ||
if message.len() != 32 { | ||
return Err(anyhow!("unisat message size is not 32")); | ||
} | ||
|
||
run_auth_exec(AuthAlgorithmIdType::Bitcoin, &address, &message, &signature)?; | ||
|
||
println!("Signature verification succeeded!"); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
|
||
|
||
run: | ||
ckb-debugger -f tx.json -s lock --bin ../../build/auth -- 04 \ | ||
1f430ab16027b41c1f6b987a5ab413fab0d2139af5ed0d2d8fd73c5e5bfbbb075b53ee6c0b8617b215b8d37a276a31ee8a881e57ceced83cc002baeb595d6fc04e \ | ||
0000000000000000000000000000000000000000000000000000000000000000 \ | ||
9a1d6626ee987c5689366b8e4fa7a15e18540996 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{ | ||
"mock_info": { | ||
"inputs": [ | ||
{ | ||
"input": { | ||
"previous_output": { | ||
"tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000002", | ||
"index": "0x0" | ||
}, | ||
"since": "0x0" | ||
}, | ||
"output": { | ||
"capacity": "0x10000000", | ||
"lock": { | ||
"args": "0x", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data2" | ||
}, | ||
"type": null | ||
}, | ||
"data": "0x" | ||
} | ||
], | ||
"cell_deps": [ | ||
{ | ||
"cell_dep": { | ||
"out_point": { | ||
"tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
"index": "0x0" | ||
}, | ||
"dep_type": "code" | ||
}, | ||
"output": { | ||
"capacity": "0x10000000", | ||
"lock": { | ||
"args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data1" | ||
}, | ||
"type": "{{ def_type secp256k1_data }}" | ||
}, | ||
"data": "0x{{ data ../../build/secp256k1_data_20210801 }}" | ||
} | ||
], | ||
"header_deps": [] | ||
}, | ||
"tx": { | ||
"version": "0x0", | ||
"cell_deps": [ | ||
{ | ||
"out_point": { | ||
"tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
"index": "0x0" | ||
}, | ||
"dep_type": "code" | ||
} | ||
], | ||
"header_deps": [], | ||
"inputs": [ | ||
{ | ||
"previous_output": { | ||
"tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000002", | ||
"index": "0x0" | ||
}, | ||
"since": "0x0" | ||
} | ||
], | ||
"outputs": [ | ||
{ | ||
"capacity": "0x0", | ||
"lock": { | ||
"args": "0x", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data2" | ||
}, | ||
"type": { | ||
"args": "0x", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data2" | ||
} | ||
} | ||
], | ||
"witnesses": [ | ||
"0x" | ||
], | ||
"outputs_data": [ | ||
"0x" | ||
] | ||
} | ||
} |