Skip to content

Commit 6de72d9

Browse files
TinySecp256k1 --> custom prefix support added.
1 parent 374c0e0 commit 6de72d9

5 files changed

Lines changed: 34 additions & 18 deletions

File tree

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
"keywords": [
4141
"encryption",
4242
"decryption",
43+
"secp256k1",
44+
"bech32",
45+
"bs58check",
46+
"keccak256",
4347
"aes",
4448
"rsa",
4549
"x.509",

src/TinyChain/Secp256k1/Btc.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ class TinyBtcSecp256k1 extends TinySecp256k1 {
1111
* Creates an instance of TinyBtcSecp256k1.
1212
*
1313
* @param {Object} [options] - Optional parameters for the instance.
14+
* @param {string|null} [options.prefix='bc'] - Crypto prefix used during message verification.
1415
* @param {string|null} [options.msgPrefix='Bitcoin Signed Message:\n'] - Message prefix used during message signing.
1516
* @param {string|null} [options.privateKey=null] - String representation of the private key.
1617
* @param {BufferEncoding} [options.privateKeyEncoding='hex'] - Encoding used for the privateKey string.
1718
*/
1819
constructor({
20+
prefix = 'bc',
1921
msgPrefix = 'Bitcoin Signed Message:\n',
2022
privateKey = null,
2123
privateKeyEncoding = 'hex',
2224
} = {}) {
23-
super({ msgPrefix, privateKey, privateKeyEncoding });
25+
super({ prefix, msgPrefix, privateKey, privateKeyEncoding });
2426
}
2527

2628
/**
@@ -226,7 +228,7 @@ class TinyBtcSecp256k1 extends TinySecp256k1 {
226228
// Convert for bech32: witness version 0 + program (ripemd160 result)
227229
const words = bech32.toWords(ripemd160);
228230
words.unshift(0x00); // witness version
229-
return bech32.encode('bc', words); // bc = mainnet
231+
return bech32.encode(this.prefix, words); // bc = mainnet
230232
}
231233

232234
/**

src/TinyChain/Secp256k1/Eth.mjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ class TinyEthSecp256k1 extends TinySecp256k1 {
99
* Creates an instance of TinyEthSecp256k1.
1010
*
1111
* @param {Object} [options] - Optional parameters for the instance.
12+
* @param {string|null} [options.prefix='0x'] - Crypto prefix used during message verification.
1213
* @param {string|null} [options.msgPrefix='\x19Ethereum Signed Message:\n'] - Message prefix used during message signing.
1314
* @param {string|null} [options.privateKey=null] - String representation of the private key.
1415
* @param {BufferEncoding} [options.privateKeyEncoding='hex'] - Encoding used for the privateKey string.
1516
*/
1617
constructor({
18+
prefix = '0x',
1719
msgPrefix = '\x19Ethereum Signed Message:\n',
1820
privateKey = null,
1921
privateKeyEncoding = 'hex',
2022
} = {}) {
21-
super({ msgPrefix, privateKey, privateKeyEncoding });
23+
super({ prefix, msgPrefix, privateKey, privateKeyEncoding });
2224
}
2325

2426
/**
@@ -84,14 +86,14 @@ class TinyEthSecp256k1 extends TinySecp256k1 {
8486
}
8587

8688
/**
87-
* Apply EIP-55 checksum to a lowercase address.
88-
* @param {string} address - Hex string without 0x prefix.
89-
* @returns {string}
90-
*/
89+
* Apply EIP-55 checksum to a lowercase address.
90+
* @param {string} address - Hex string without 0x prefix.
91+
* @returns {string}
92+
*/
9193
#toChecksumAddress(address) {
9294
const { keccak256 } = this.getJsSha3();
9395
const hash = keccak256(address.toLowerCase());
94-
let checksumAddress = '0x';
96+
let checksumAddress = this.prefix;
9597
for (let i = 0; i < address.length; i++) {
9698
const char = address[i];
9799
const hashChar = parseInt(hash[i], 16);

src/TinyChain/Secp256k1/index.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TinySecp256k1 {
4343
/** @typedef {import('elliptic').ec} ec */
4444
/** @typedef {import('elliptic').ec.KeyPair} KeyPair */
4545
msgPrefix = 'Tinychain Signed Message:\n';
46+
prefix = '';
4647

4748
/**
4849
* Computes SHA-256 hash of the input buffer.
@@ -78,12 +79,19 @@ class TinySecp256k1 {
7879
* Creates an instance of TinySecp256k1.
7980
*
8081
* @param {Object} [options] - Optional parameters for the instance.
82+
* @param {string|null} [options.prefix=null] - Crypto prefix used during message verification.
8183
* @param {string|null} [options.msgPrefix=null] - Message prefix used during message signing.
8284
* @param {string|null} [options.privateKey=null] - String representation of the private key.
8385
* @param {BufferEncoding} [options.privateKeyEncoding='hex'] - Encoding used for the privateKey string.
8486
*/
85-
constructor({ msgPrefix = null, privateKey = null, privateKeyEncoding = 'hex' } = {}) {
87+
constructor({
88+
prefix = null,
89+
msgPrefix = null,
90+
privateKey = null,
91+
privateKeyEncoding = 'hex',
92+
} = {}) {
8693
if (typeof msgPrefix === 'string') this.msgPrefix = msgPrefix;
94+
if (typeof prefix === 'string') this.prefix = prefix;
8795
this.privateKey = privateKey ? Buffer.from(privateKey, privateKeyEncoding) : randomBytes(32);
8896
}
8997

@@ -197,7 +205,7 @@ class TinySecp256k1 {
197205
* @returns {string} The public address.
198206
*/
199207
getAddress(type = '', compressed = true) {
200-
return this.getPublicKeyHex(compressed);
208+
return `${this.prefix}${this.getPublicKeyHex(compressed)}`;
201209
}
202210

203211
/**

test/TinyChain.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,21 @@ const tinySignatureTest = async () => {
170170
};
171171

172172
const tinyBtcSignatureTest = async () => {
173-
console.log('\n🔐🔹 TinySecp256k1 Signature Test 🔹🔐\n');
173+
console.log('\n🔐🔹 TinySecp256k1 BTC Signature Test 🔹🔐\n');
174174

175175
const signer = new TinyChain.Btc256k1();
176176
await signer.init();
177177

178178
const privateKey = signer.getPrivateKeyHex();
179179
const publicKey = signer.getAddress();
180180

181-
console.log('🗝️ Keys');
181+
console.log('🗝️ Keys (BTC)');
182182
console.log('──────────────────────────────');
183183
console.log(`🔒 Private Key : ${privateKey}`);
184184
console.log(`🔓 Public Key : ${publicKey}\n`);
185185

186186
const recoverableMessage = 'Hello world';
187-
console.log('♻️ Signing message');
187+
console.log('♻️ Signing message (BTC)');
188188
console.log('──────────────────────────────');
189189
const sig = signer.signMessage(recoverableMessage);
190190
console.log(`📄 Signature (Recoverable): ${sig.toString('hex')}`);
@@ -195,25 +195,25 @@ const tinyBtcSignatureTest = async () => {
195195
console.log(`🔍 Message Signature Valid? ${isValid}\n`);
196196
console.log(`📄 Message Signature (Recoverable): ${recoveredPubKey}`);
197197

198-
console.log('✅ Test Completed!\n');
198+
console.log('✅ Test Completed! (BTC)\n');
199199
};
200200

201201
const testTinyEthSecp256k1 = async () => {
202-
console.log('\n🔐🔹 TinySecp256k1 Signature Test 🔹🔐\n');
202+
console.log('\n🔐🔹 TinySecp256k1 ETH Signature Test 🔹🔐\n');
203203

204204
const signer = new TinyChain.Eth256k1();
205205
await signer.init();
206206

207207
const privateKey = signer.getPrivateKeyHex();
208208
const publicKey = signer.getAddress();
209209

210-
console.log('🗝️ Keys');
210+
console.log('🗝️ Keys (ETH)');
211211
console.log('──────────────────────────────');
212212
console.log(`🔒 Private Key : ${privateKey}`);
213213
console.log(`🔓 Public Key : ${publicKey}\n`);
214214

215215
const recoverableMessage = 'Hello world';
216-
console.log('♻️ Signing message');
216+
console.log('♻️ Signing message (ETH)');
217217
console.log('──────────────────────────────');
218218
const sig = signer.signMessage(recoverableMessage);
219219
console.log(`📄 Signature (Recoverable): ${sig.toString('hex')}`);
@@ -224,7 +224,7 @@ const testTinyEthSecp256k1 = async () => {
224224
console.log(`🔍 Message Signature Valid? ${isValid}\n`);
225225
console.log(`📄 Message Signature (Recoverable): ${recoveredPubKey}`);
226226

227-
console.log('✅ Test Completed!\n');
227+
console.log('✅ Test Completed! (ETH)\n');
228228
};
229229

230230
const tinyChainSimulation = async () => {

0 commit comments

Comments
 (0)