Skip to content

Commit

Permalink
feat(sdk): integrate bip322-js to sign & verify non-legacy address me…
Browse files Browse the repository at this point in the history
…ssages (#91)

* chore: install bip322-js lib

* feat: use bip322 to sign non-legacy addr message

also, use the same lib to verify non-legacy addr message

* feat: allow non-legacy addresses to sign message

* fix: pass missing WIF & network to msg signer

also, remove compressed flag

* refactor: restore fallback verification

also, replace ternary expression w/ if block
  • Loading branch information
iamcrazycoder authored Nov 3, 2023
1 parent 0e3c322 commit de0fdf1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@bitcoinerlab/secp256k1": "1.0.2",
"bip32": "4.0.0",
"bip322-js": "^1.1.0",
"bip39": "3.1.0",
"bitcoinjs-lib": "6.1.3",
"bitcoinjs-message": "2.2.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/browser-wallets/metamask/signatures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address, Signer } from "bip322-js"
import { Psbt } from "bitcoinjs-lib"
import { sign } from "bitcoinjs-message"
import { ethers } from "ethers"
Expand Down Expand Up @@ -86,7 +87,9 @@ export async function signMessage(options: SignMetaMaskMessageOptions) {
const node = await getDerivedNodeFromMetaMaskSignature(signature, "", options.network)
const { address: addressBtc } = createTransaction(node.parent.publicKey, "p2pkh", options.network)

const signedMessage = sign(options.message, node.parent.privateKey!)
const signedMessage = Address.isP2PKH(address)
? sign(options.message, node.parent.privateKey!)
: Signer.sign(node.parent.privateKey!.toString(), address, options.message)

return {
hex: signedMessage.toString("hex"),
Expand Down
17 changes: 9 additions & 8 deletions packages/sdk/src/signatures/message.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address, Signer, Verifier } from "bip322-js"
import { sign, verify } from "bitcoinjs-message"

import { Network } from "../config/types"
Expand All @@ -20,7 +21,9 @@ export async function signMessage(options: SignMessageOptions) {
// const keyPair = EcPair.fromWIF(wif);
const { address } = createTransaction(parent.publicKey, "p2pkh", network)

const signature = sign(options.message, parent.privateKey!)
const signature = Address.isP2PKH(address!)
? sign(options.message, parent.privateKey!)
: Signer.sign(parent.privateKey!.toString(), address!, options.message)

return {
hex: signature.toString("hex"),
Expand All @@ -34,15 +37,13 @@ export async function signMessage(options: SignMessageOptions) {

export function verifyMessage(options: VerifyMessageOptions) {
try {
let isValid = verify(options.message, options.address, options.signature)

if (!isValid) {
isValid = fallbackVerification(options)
if (Address.isP2PKH(options.address)) {
return !verify(options.message, options.address, options.signature) ? fallbackVerification(options) : true
}

return isValid
} catch (error) {
return fallbackVerification(options)
return Verifier.verifySignature(options.address, options.message, options.signature)
} catch (_) {
return false
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ecc from "@bitcoinerlab/secp256k1"
import BIP32Factory, { BIP32Interface } from "bip32"
import { mnemonicToSeedSync } from "bip39"
import { Address as BIP22Address, Signer } from "bip322-js"
import * as bitcoin from "bitcoinjs-lib"
import { isTaprootInput } from "bitcoinjs-lib/src/psbt/bip371"
import { sign } from "bitcoinjs-message"
Expand Down Expand Up @@ -231,8 +232,10 @@ export class Ordit {
}

signMessage(message: string) {
const legacyWallet = this.allAddresses.find((wallet) => wallet.format === "legacy") as Account
const signature = sign(message, legacyWallet.child.privateKey!, false)
const node = this.allAddresses.find((wallet) => wallet.format === this.selectedAddressType) as Account
const signature = BIP22Address.isP2PKH(node.address!)
? sign(message, node.child.privateKey!)
: Signer.sign(node.child.toWIF(), node.address!, message, getNetwork(this.#network))

return signature.toString("base64")
}
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de0fdf1

Please sign in to comment.