Skip to content

Commit

Permalink
Added regtest support in Signer
Browse files Browse the repository at this point in the history
  • Loading branch information
ACken2 committed May 15, 2024
1 parent 524afe2 commit 653fa83
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Signer {
*/
private static checkPubKeyCorrespondToAddress(publicKey: Buffer, claimedAddress: string) {
// Derive the same address type from the provided public key
let derivedAddresses: { mainnet: string, testnet: string };
let derivedAddresses: { mainnet: string, testnet: string, regtest: string };
if (Address.isP2PKH(claimedAddress)) {
derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2pkh');
}
Expand All @@ -100,7 +100,10 @@ class Signer {
throw new Error('Unable to sign BIP-322 message for unsupported address type.'); // Unsupported address type
}
// Check if the derived address correspond to the claimedAddress
return (derivedAddresses.mainnet === claimedAddress) || (derivedAddresses.testnet === claimedAddress);
return (
(derivedAddresses.mainnet === claimedAddress) || (derivedAddresses.testnet === claimedAddress) ||
(derivedAddresses.regtest === claimedAddress)
);
}

}
Expand Down
12 changes: 12 additions & 0 deletions test/Signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ describe('Signer Test', () => {
const privateKeyTestnet = 'cTrF79uahxMC7bQGWh2931vepWPWqS8KtF8EkqgWwv3KMGZNJ2yP'; // Equivalent to L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k
const address = '14vV3aCHBeStb5bkenkNHbe2YAFinYdXgc';
const addressTestnet = 'mjSSLdHFzft9NC5NNMik7WrMQ9rRhMhNpT';
const addressRegtest = 'mjSSLdHFzft9NC5NNMik7WrMQ9rRhMhNpT';
const message = 'Hello World';

// Act
const signature = Signer.sign(privateKey, address, message);
const signatureTestnet = Signer.sign(privateKeyTestnet, addressTestnet, message, bitcoin.networks.testnet);
const signatureRegtest = Signer.sign(privateKeyTestnet, addressRegtest, message, bitcoin.networks.regtest);

// Assert
expect(bitcoinMessage.verify(message, address, signature)).to.be.true;
expect(bitcoinMessage.verify(message, addressTestnet, signatureTestnet)).to.be.true;
expect(bitcoinMessage.verify(message, addressRegtest, signatureRegtest)).to.be.true;
});

it('Can sign BIP-322 signature using nested segwit address', () => {
Expand All @@ -31,16 +34,19 @@ describe('Signer Test', () => {
const privateKeyTestnet = 'cMpadsm2xoVpWV5FywY5cAeraa1PCtSkzrBM45Ladpf9rgDu6cMz'; // Equivalent to 'KwTbAxmBXjoZM3bzbXixEr9nxLhyYSM4vp2swet58i19bw9sqk5z'
const address = '3HSVzEhCFuH9Z3wvoWTexy7BMVVp3PjS6f';
const addressTestnet = '2N8zi3ydDsMnVkqaUUe5Xav6SZqhyqEduap';
const addressRegtest = '2N8zi3ydDsMnVkqaUUe5Xav6SZqhyqEduap';
const message = 'Hello World';
const expectedSignature = 'AkgwRQIhAMd2wZSY3x0V9Kr/NClochoTXcgDaGl3OObOR17yx3QQAiBVWxqNSS+CKen7bmJTG6YfJjsggQ4Fa2RHKgBKrdQQ+gEhAxa5UDdQCHSQHfKQv14ybcYm1C9y6b12xAuukWzSnS+w';

// Act
const signature = Signer.sign(privateKey, address, message);
const signatureTestnet = Signer.sign(privateKeyTestnet, addressTestnet, message, bitcoin.networks.testnet);
const signatureRegtest = Signer.sign(privateKeyTestnet, addressRegtest, message, bitcoin.networks.regtest);

// Assert
expect(signature).to.equal(expectedSignature);
expect(signatureTestnet).to.equal(expectedSignature);
expect(signatureRegtest).to.equal(expectedSignature);
});

it('Can sign BIP-322 signature using native segwit address', () => {
Expand All @@ -50,16 +56,19 @@ describe('Signer Test', () => {
const privateKeyTestnet = 'cTrF79uahxMC7bQGWh2931vepWPWqS8KtF8EkqgWwv3KMGZNJ2yP'; // Equivalent to L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k
const address = 'bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l';
const addressTestnet = 'tb1q9vza2e8x573nczrlzms0wvx3gsqjx7vaxwd45v';
const addressRegtest = 'bcrt1q9vza2e8x573nczrlzms0wvx3gsqjx7vay85cr9';
const message = 'Hello World';
const expectedSignature = 'AkgwRQIhAOzyynlqt93lOKJr+wmmxIens//zPzl9tqIOua93wO6MAiBi5n5EyAcPScOjf1lAqIUIQtr3zKNeavYabHyR8eGhowEhAsfxIAMZZEKUPYWI4BruhAQjzFT8FSFSajuFwrDL1Yhy';

// Act
const signature = Signer.sign(privateKey, address, message);
const signatureTestnet = Signer.sign(privateKeyTestnet, addressTestnet, message, bitcoin.networks.testnet);
const signatureRegtest = Signer.sign(privateKeyTestnet, addressRegtest, message, bitcoin.networks.regtest);

// Assert
expect(signature).to.equal(expectedSignature);
expect(signatureTestnet).to.equal(expectedSignature);
expect(signatureRegtest).to.equal(expectedSignature);
});

it('Can sign BIP-322 using single-key-spend taproot address', () => {
Expand All @@ -69,16 +78,19 @@ describe('Signer Test', () => {
const privateKeyTestnet = 'cTrF79uahxMC7bQGWh2931vepWPWqS8KtF8EkqgWwv3KMGZNJ2yP'; // Equivalent to L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k
const address = "bc1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5sxq8lt3";
const addressTestnet = 'tb1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5s3g3s37';
const addressRegtest = 'bcrt1ppv609nr0vr25u07u95waq5lucwfm6tde4nydujnu8npg4q75mr5su3mkyy';
const message = "Hello World";
const expectedSignature = "AUHd69PrJQEv+oKTfZ8l+WROBHuy9HKrbFCJu7U1iK2iiEy1vMU5EfMtjc+VSHM7aU0SDbak5IUZRVno2P5mjSafAQ==";

// Act
const signature = Signer.sign(privateKey, address, message);
const signatureTestnet = Signer.sign(privateKeyTestnet, addressTestnet, message, bitcoin.networks.testnet);
const signatureRegtest = Signer.sign(privateKeyTestnet, addressRegtest, message, bitcoin.networks.regtest);

// Assert
expect(signature).to.equal(expectedSignature);
expect(signatureTestnet).to.equal(expectedSignature);
expect(signatureRegtest).to.equal(expectedSignature);
});

it('Throw when the provided private key cannot derive the given signing address', () => {
Expand Down

0 comments on commit 653fa83

Please sign in to comment.