Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- fixed: Encode BIP137 message signatures with the correct header byte for SegWit addresses (bip49 nested SegWit and bip84 native SegWit) and with each coin's own message prefix, so signatures verify on BIP137-compliant platforms across all UTXO currencies.

## 3.10.0 (2026-06-13)

- changed: Convert the build tooling from Yarn to npm.
Expand Down
2 changes: 1 addition & 1 deletion src/common/utxobased/engine/UtxoWalletTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function makeUtxoWalletTools(
path,
xprivKeys
})
return signMessageBase64(message, privKey)
return signMessageBase64(message, privKey, path.format, coin)
}
}

Expand Down
33 changes: 30 additions & 3 deletions src/common/utxobased/keymanager/keymanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import { EdgeLog, EdgeMemo } from 'edge-core-js/types'

import { indexAtProtected } from '../../../util/indexAtProtected'
import { undefinedIfEmptyString } from '../../../util/undefinedIfEmptyString'
import { ChangePath, CoinInfo, CoinPrefixes, FeeInfo } from '../../plugin/types'
import {
ChangePath,
CoinInfo,
CoinPrefixes,
CurrencyFormat,
FeeInfo
} from '../../plugin/types'
import { UtxoData } from '../db/types'
import { ScriptTemplate, ScriptTemplates } from '../info/scriptTemplates/types'
import { sortInputs, sortOutputs } from './bip69'
Expand Down Expand Up @@ -938,14 +944,35 @@ export function privateKeyEncodingToPubkey(
}).publicKey.toString('hex')
}

export function signMessageBase64(message: string, privateKey: string): string {
export function signMessageBase64(
message: string,
privateKey: string,
format: CurrencyFormat,
coin: string
): string {
const ECPair = getECPair()
const keyPair = ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'))
if (keyPair.privateKey == null) {
throw new Error('Address could not sign message')
}
// BIP137 encodes the address type in the signature's header byte. Native
// SegWit (bip84 / P2WPKH) and nested SegWit (bip49 / P2SH-P2WPKH) each have
// their own header range; verifiers such as Bringin reject a SegWit address
// whose message was signed with a legacy (bip44) header byte.
const segwitType =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Minor (robustness): the format -> segwitType mapping compares raw 'bip84'/'bip49' string literals rather than routing through the codebase's canonical BIP43PurposeTypeEnum / bip43PurposeNumberToTypeEnum. If a new SegWit CurrencyFormat is ever added, this ternary is easy to miss (no compiler linkage) and the new format silently falls through to undefined, producing a legacy header that BIP137 verifiers reject. Consider deriving the segwit kind from the purpose enum so the two stay in lockstep.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Leaving open as an optional cleanup. The coin-prefix fix (the operator's requested scope) does not depend on it, so this change stays minimal; routing the segwitType selection through BIP43PurposeTypeEnum is a good follow-up but out of scope for this task.

format === 'bip84'
? 'p2wpkh'
: format === 'bip49'
? 'p2sh(p2wpkh)'
: undefined
Comment thread
j0ntz marked this conversation as resolved.
// Magic-hash the message with the coin's own prefix (e.g. "Litecoin Signed
// Message:\n") so the signature verifies against that coin's addresses.
// Every UTXO coin routes through here, not just Bitcoin.
const messagePrefix = getCoinFromString(coin).prefixes.messagePrefix[0]
return bitcoinMessage
.sign(message, keyPair.privateKey, keyPair.compressed)
.sign(message, keyPair.privateKey, keyPair.compressed, messagePrefix, {
segwitType
})
.toString('base64')
}

Expand Down
44 changes: 44 additions & 0 deletions test/common/utxobased/keymanager/coins/altcointestfixtures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CurrencyFormat } from '../../../../../src/common/plugin/types'
import {
AddressTypeEnum,
BIP43PurposeTypeEnum,
Expand Down Expand Up @@ -45,6 +46,7 @@ interface XPubToPubkeyTests {
interface SignMessageTests {
wif: string
message: string
format: CurrencyFormat
signature: string
}

Expand Down Expand Up @@ -842,6 +844,33 @@ export const fixtures: Fixture = {
address: 'ltc1qjmxnz78nmc8nq77wuxh25n2es7rzm5c2rkk4wh',
scriptPubkey: '001496cd3178f3de0f307bcee1aeaa4d5987862dd30a'
}
],
// Signatures use Litecoin's "Litecoin Signed Message:\n" prefix, so they
// differ from the Bitcoin vectors above for the same key/message and
// verify against Litecoin addresses. The leading base64 char (H/I/J)
// still encodes the BIP137 address type (legacy / p2sh-p2wpkh / p2wpkh).
signMessageTests: [
{
wif: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
message: 'This is an example of a signed message.',
format: 'bip44',
signature:
'H0UZW4iWKCquBbOO32dlUOkB8RqG81jJmfkMsvK8vppNDCFE5M17LvmtxQYHq6wcimb/LFk/urTz6VvbILtSth4='
},
{
wif: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
message: 'This is an example of a signed message.',
format: 'bip49',
signature:
'I0UZW4iWKCquBbOO32dlUOkB8RqG81jJmfkMsvK8vppNDCFE5M17LvmtxQYHq6wcimb/LFk/urTz6VvbILtSth4='
},
{
wif: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
message: 'This is an example of a signed message.',
format: 'bip84',
signature:
'J0UZW4iWKCquBbOO32dlUOkB8RqG81jJmfkMsvK8vppNDCFE5M17LvmtxQYHq6wcimb/LFk/urTz6VvbILtSth4='
}
]
},
{
Expand Down Expand Up @@ -1352,8 +1381,23 @@ export const fixtures: Fixture = {
{
wif: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
message: 'This is an example of a signed message.',
format: 'bip44',
signature:
'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
},
{
wif: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
message: 'This is an example of a signed message.',
format: 'bip49',
signature:
'I9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
},
{
wif: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
message: 'This is an example of a signed message.',
format: 'bip84',
signature:
'J9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
}
]
},
Expand Down
9 changes: 7 additions & 2 deletions test/common/utxobased/keymanager/coins/keymanagertest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ describe('altcoin test fixtures', () => {
}
if (f.signMessageTests != null) {
f.signMessageTests.forEach(j => {
it(`${f.name} sign message test`, () => {
it(`${f.name} sign message test (${j.format})`, () => {
const privateKey = ECPair.fromWIF(j.wif).privateKey?.toString('hex')
if (privateKey == null) {
throw new Error('private key cannot be null')
}
const signature = signMessageBase64(j.message, privateKey)
const signature = signMessageBase64(
j.message,
privateKey,
j.format,
f.name
)
expect(signature).to.eqls(j.signature)
})
})
Expand Down
Loading