Skip to content

Commit 03fc141

Browse files
feat: param updates
* Update params to make it easier to implement multiple chain messages * Params include: versions object, network object * Updated types and readme v1.0.0
1 parent 48eb9db commit 03fc141

File tree

5 files changed

+67
-33
lines changed

5 files changed

+67
-33
lines changed

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm install @hyperbitjs/message
1717
```javascript
1818
import { sign } from '@hyperbitjs/message';
1919

20+
// Wallet Import Format (WIF) format
2021
const privateKey = 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1';
2122
const message = 'This is an example of a signed message.';
2223

@@ -36,26 +37,34 @@ const signature = '<generated_signature_from_sign>';
3637
const isValid = verify({ address, message, signature });
3738
```
3839

39-
Message signing defaults to Bitcoin '\u0018Bitcoin Signed Message:\n'. See [@hyperbitjs/chains](https://github.com/hyperbit-dev/chains) for message prefix options provided by different blockchains.
40-
40+
### Full Example
4141
```javascript
42+
import { Mnemonic } from '@hyperbitjs/mnemonic';
4243
import { sign, verify } from '@hyperbitjs/message';
43-
import { btc } from '@hyperbitjs/chains';
44+
import { ltc } from '@hyperbitjs/chains';
45+
46+
const mnemonic = new Mnemonic({ network: ltc.main });
47+
const addresses = mnemonic.generateAddresses();
4448

45-
const { messagePrefix } = ltc.main;
49+
const { wif, address } = addresses[0].external;
50+
const network = ltc.main;
51+
const message = 'This is an example of a signed message.';
4652

4753
const signature = sign({
48-
privateKey: '...',
49-
message: '...',
50-
messagePrefix,
54+
privateKey: wif,
55+
message,
56+
network,
5157
});
5258

53-
const isVerified = verify({
54-
address: '...',
55-
message: '...',
56-
signature: '...',
57-
messagePrefix,
59+
const isValid = verify({
60+
message,
61+
address,
62+
signature,
63+
network,
5864
});
65+
66+
console.log('isValid', isValid);
67+
// Expected Output: true
5968
```
6069

6170
## Contributing

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.0",
2+
"version": "1.0.0",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",
@@ -57,6 +57,7 @@
5757
"typescript": "^4.8.4"
5858
},
5959
"dependencies": {
60+
"@hyperbitjs/chains": "^0.3.1",
6061
"bitcoinjs-message": "^2.2.0",
6162
"coinkey": "^3.0.0"
6263
},

src/index.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
1+
import { btc } from '@hyperbitjs/chains';
12
import bitcoinMessage from 'bitcoinjs-message';
23
import CoinKey from 'coinkey';
34
import { Sign, Verify } from './types';
45

5-
/**
6-
* TODO:
7-
* Fix version usage. See chains versions.public/private and https://github.com/cryptocoinjs/coinkey/blob/master/lib/coinkey.js#L9
8-
* Update to pull in network of blockchain
9-
*/
10-
116
/**
127
* Sign for proof of ownership of wallet address.
138
* @param param
149
* @param {string} param.message Hash used for signing your message.
1510
* @param {string|Buffer} param.privateKey Wallet Import Format (WIF) string or private key Buffer.
1611
* @param {boolean=} param.compressed
1712
* @param {string=} [param.messagePrefix=\u0018Bitcoin Signed Message:\n] Message prefix used by the blockchain for signing.
18-
* @param {any=} sigOptions Pass in any option after the initial object to make use of additional https://github.com/bitcoinjs/bitcoinjs-message parameters.
13+
* @param {object=} param.versions Versions object derived from Network object from @hyperbitjs/chains. Only private and public strings required
14+
* @param {object=} param.network Network object from @hyperbitjs/chains
15+
* @param {any=} params Pass in any signature option after the initial object to make use of additional https://github.com/bitcoinjs/bitcoinjs-message parameters.
1916
* @returns A base64 encoded string representation of the signature.
2017
*/
2118
export function sign(
2219
{
2320
message,
2421
privateKey,
2522
compressed = true,
26-
messagePrefix = '\u0018Bitcoin Signed Message:\n',
23+
messagePrefix,
24+
versions,
25+
network = btc.main,
2726
}: Sign,
28-
...sigOptions: any
27+
...params: any
2928
): string | null {
29+
const _messagePrefix =
30+
messagePrefix || network?.messagePrefix || btc.main.messagePrefix;
31+
const _versions = versions || network?.versions || btc.main.versions;
32+
3033
try {
31-
const privateKeyWif =
32-
typeof privateKey === 'string'
33-
? CoinKey.fromWif(privateKey).privateKey
34-
: privateKey;
34+
let _compressed = compressed;
35+
let _privateKeyWif = privateKey;
36+
37+
if (typeof privateKey === 'string') {
38+
_privateKeyWif = CoinKey.fromWif(privateKey, _versions).privateKey;
39+
_compressed =
40+
CoinKey.fromWif(privateKey, _versions).compressed === true
41+
? true
42+
: _compressed;
43+
}
44+
3545
const signature = bitcoinMessage.sign(
3646
message,
37-
privateKeyWif,
38-
compressed,
39-
messagePrefix,
40-
...sigOptions
47+
_privateKeyWif,
48+
_compressed,
49+
_messagePrefix,
50+
...params
4151
);
4252
return signature.toString('base64');
4353
} catch (e) {
44-
console.log('e', e);
4554
return null;
4655
}
4756
}
@@ -53,17 +62,22 @@ export function sign(
5362
* @param {string} param.address Address submitted for proof of ownership.
5463
* @param {string} param.signature Signature generated from address and message.
5564
* @param {string} [param.messagePrefix=\u0018Bitcoin Signed Message:\n] Message prefix used by the blockchain for verifying.
65+
* @param {object=} param.network Network object from @hyperbitjs/chains
5666
* @returns
5767
*/
5868
export function verify({
5969
message,
6070
address,
6171
signature,
62-
messagePrefix = '\u0018Bitcoin Signed Message:\n',
72+
messagePrefix,
73+
network,
6374
}: Verify): boolean {
75+
const _messagePrefix =
76+
messagePrefix || network?.messagePrefix || btc.main.messagePrefix;
77+
6478
try {
6579
const m = Buffer.from(message).toString('ascii');
66-
const result = bitcoinMessage.verify(m, address, signature, messagePrefix);
80+
const result = bitcoinMessage.verify(m, address, signature, _messagePrefix);
6781
return result;
6882
} catch {
6983
return false;

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import { Network, TestNetwork, Versions } from '@hyperbitjs/chains';
2+
13
export type Sign = {
24
message: string;
35
privateKey: string | Buffer;
46
compressed?: boolean;
57
messagePrefix?: string;
8+
versions?: Pick<Versions, 'private' | 'public'>;
9+
network?: Network | TestNetwork;
610
};
711

812
export type Verify = {
913
message: string;
1014
address: string;
1115
signature: string;
1216
messagePrefix?: string;
17+
network?: Network | TestNetwork;
1318
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,11 @@
959959
resolved "https://registry.yarnpkg.com/@esbuild/linux-loong64/-/linux-loong64-0.15.13.tgz#64e8825bf0ce769dac94ee39d92ebe6272020dfc"
960960
integrity sha512-+BoyIm4I8uJmH/QDIH0fu7MG0AEx9OXEDXnqptXCwKOlOqZiS4iraH1Nr7/ObLMokW3sOCeBNyD68ATcV9b9Ag==
961961

962+
"@hyperbitjs/chains@^0.3.1":
963+
version "0.3.1"
964+
resolved "https://registry.yarnpkg.com/@hyperbitjs/chains/-/chains-0.3.1.tgz#109ca35895e366e911f264a65de7c1dc79c4bc8f"
965+
integrity sha512-WseW+KW5I0c5QilFRVLKPcigFebSGI9VEzh5yLcM1a+uk/isJHqZ67IWNaAAArAZxguGvv1k0yYLUaUlunEoKw==
966+
962967
"@istanbuljs/load-nyc-config@^1.0.0":
963968
version "1.1.0"
964969
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"

0 commit comments

Comments
 (0)