Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btc: Expose sign_message() to TS API and create UI #54

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

asi345
Copy link
Collaborator

@asi345 asi345 commented Oct 26, 2023

After implementing BTC message signing for Rust API, further extensions are added to make wasm expose this functionality to TypeScript API. Finally, an extra field added under the Bitcoin section of sandbox UI app for BTC message signing.

@asi345 asi345 requested a review from benma October 26, 2023 14:17
@asi345 asi345 self-assigned this Oct 26, 2023
@asi345 asi345 force-pushed the btc_sign_message branch 2 times, most recently from 62b4661 to 9ae3378 Compare October 26, 2023 14:33
src/wasm/mod.rs Outdated
pub async fn btc_sign_message(
&self,
coin: types::TsBtcCoin,
script_config: Option<types::TsBtcScriptConfigWithKeypath>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls make this not an Option<>, it is requried after all. I missed this in the previous review - you'll also need to change it in btc.rs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, changing it in the Rust side too.

type BtcSignMessageSignature = {
sig: Uint8Array,
recid: bigint,
electrum_sig65: Uint8Array,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JS/TS, camelCase is more common, and we use it in this project too. electrumSig65.

You can use #[serde(rename_all = "camelCase")] on the SignMessageSignature struct to see if that works (check other instances or the serde docs).

Comment on lines 290 to 295
#[derive(serde::Serialize)]
pub struct BtcSignMessageSignature {
pub sig: Vec<u8>,
pub recid: u8,
pub electrum_sig65: Vec<u8>,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to replicate it here, right? the struct already exists in btc::SignMessageSignature.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I replicated it since ETH had a similar struct there, but it may not be necessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I remove this, I get into errors and compiler can not find btc::SignMessageSignature, so I kept it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it, the error was due to the serialization definition for the struct

setResult(undefined);
setErr(undefined);
try {
const signature = await bb02.btcSignMessage(coin, script_config_w_keypath, stringToUint8Array(message));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are using the hardcoded config - use keypath and simpleType to construct the one made from the user input

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my mistake, I forgot it while testing

const [result, setResult] = useState<bitbox.BtcSignMessageSignature | undefined>();
const [err, setErr] = useState<bitbox.Error>();

const simpleTypeOptions: bitbox.BtcSimpleType[] = ['p2wpkhP2sh', 'p2wpkh', 'p2tr'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove p2tr here as it is not supported anyway?

@@ -232,6 +232,90 @@ function BtcSignPSBT({ bb02 }: Props) {
);
}

function BtcSignMessage({ bb02 }: Props) {
const [coin, setCoin] = useState<bitbox.BtcCoin>('btc');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this state, sign msg only works for btc anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, coin is hardcoded to btc now

<label>
Simple Type
<select value={simpleType} onChange={(e: ChangeEvent<HTMLSelectElement>) => setSimpleType(e.target.value as bitbox.BtcSimpleType)}>
{simpleTypeOptions.map(option => <option key={option} value={option} disabled={option === 'p2tr' && (coin === 'ltc' || coin === 'tltc')}>{option}</option>)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for these checks, just remove them from the input options. only btc for p2wsh and p2wsh-p2sh is supported by the firmware.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, no need for them after removing p2tr

<label>
Keypath
</label>
<textarea value={keypath} onChange={e => setKeypath(e.target.value)} rows={1} cols={80} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<input type="string" ...> is the normal way for a single line input. in this textarea field, if you hit enter, you can enter more lines, which is not needed for the keypath.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had set the rows to 1 and thought it would serve the same purpose, but after reading this I saw it was not the same. Adjusted it to the input tag.

Comment on lines 304 to 306
rows={32}
readOnly
defaultValue={parsedResult}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent this

@asi345 asi345 force-pushed the btc_sign_message branch 2 times, most recently from 21be20c to de376e3 Compare November 1, 2023 09:57
Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested and works! but I left some small nits

src/wasm/mod.rs Outdated
.await?;

Ok(
serde_wasm_bindgen::to_value(&crate::btc::SignMessageSignature {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just do serde_wasm_bindgen::to_value(signature) right? no need to create a copy of the struct instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I use the actual struct from btc, yes I can I suppose.

const coin = 'btc';
const simpleTypeOptions: bitbox.BtcSimpleType[] = ['p2wpkhP2sh', 'p2wpkh'];

const script_config: bitbox.BtcScriptConfig = { simpleType };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use camelCase in Javascript, scriptConfig (also below)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusting them.

@asi345
Copy link
Collaborator Author

asi345 commented Nov 1, 2023

Perfect! According to the last small details, pushing one last version.

Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK

const simpleTypeOptions: bitbox.BtcSimpleType[] = ['p2wpkhP2sh', 'p2wpkh'];

const scriptConfig: bitbox.BtcScriptConfig = { simpleType };
const scriptConfigWKeypath: bitbox.BtcScriptConfigWithKeypath = { scriptConfig: scriptConfig, keypath: keypath };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scriptConfig: scriptConfig => scriptConfig now after the rename 😁

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, using the shorter notation in both of the keys then.

After implementing BTC message signing for Rust API, further extensions
are added to make wasm expose this functionality to TypeScript API.
Finally, an extra field added under the Bitcoin section of sandbox UI
app for BTC message signing.

Signed-off-by: asi345 <[email protected]>
@asi345 asi345 merged commit 29d4368 into BitBoxSwiss:master Nov 1, 2023
5 checks passed
@benma benma mentioned this pull request Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants