Skip to content

Commit 1c064d1

Browse files
author
Fabrice Bascoulergue
authored
Merge pull request #7 from lum-network/develop
Add support for easy message signature and verification
2 parents 692c7a5 + c1b2fad commit 1c064d1

18 files changed

+378
-32
lines changed

docs/README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ const privateKey = LumUtils.generatePrivateKey();
3939

4040
// Create a wallet instance based on this fresh private key
4141
const wallet = await LumWalletFactory.fromPrivateKey(mnemonic);
42-
console.log(`Wallet address: ${wallet.address}`);
42+
console.log(`Wallet address: ${wallet.getAddress()}`);
4343

4444
// Create a wallet instance based on an hexadecimal private key (ex: user input - 0x is optional)
4545
const hexPrivateKey = '0xb8e62c34928025cdd3aef6cbebc68694b5ad9209b2aff6d3891c8e61d22d3a3b';
4646
const existingWallet = await LumWalletFactory.fromPrivateKey(LumUtils.keyFromHex(hexPrivateKey));
47-
console.log(`Existing wallet address: ${wallet.address}`);
47+
console.log(`Existing wallet address: ${wallet.getAddress()}`);
4848
```
4949

5050
#### Keystore
@@ -54,7 +54,7 @@ const privateKey = LumUtils.generatePrivateKey();
5454
// Create a keystore (or consume user input)
5555
const keystore = LumUtils.generateKeyStore(privateKey, 'some-password');
5656
const wallet = await LumWalletFactory.fromKeyStore(keystore, 'some-password');
57-
console.log(`Wallet address: ${wallet.address}`);
57+
console.log(`Wallet address: ${wallet.getAddress()}`);
5858
```
5959

6060
### Hardware wallets
@@ -107,7 +107,7 @@ const testnetClient = await LumClient.connect('http://node0.testnet.lum.network/
107107

108108
#### Get account information
109109
```typescript
110-
const account = await testnetClient.getAccount(wallet.address);
110+
const account = await testnetClient.getAccount(wallet.getAddress());
111111
if (account === null) {
112112
console.log('Account: not found');
113113
} else {
@@ -117,7 +117,7 @@ if (account === null) {
117117

118118
#### Get account balances
119119
```typescript
120-
const balances = await testnetClient.getAllBalancesUnverified(wallet.address);
120+
const balances = await testnetClient.getAllBalancesUnverified(wallet.getAddress());
121121
if (balances.length === 0) {
122122
console.log('Balances: empty account');
123123
} else {
@@ -135,8 +135,8 @@ if (balances.length === 0) {
135135
```typescript
136136
// The client search feature supports multiple searches and merge+sort the results
137137
const transactions = await testnetClient.searchTx([
138-
LumUtils.searchTxFrom(wallet.address),
139-
LumUtils.searchTxTo(wallet.address),
138+
LumUtils.searchTxFrom(wallet.getAddress()),
139+
LumUtils.searchTxTo(wallet.getAddress()),
140140
]);
141141
console.log(`Transactions: ${transactions.map((tx) => tx.hash).join(', ')}`);
142142
```
@@ -145,7 +145,7 @@ console.log(`Transactions: ${transactions.map((tx) => tx.hash).join(', ')}`);
145145
```typescript
146146
// Build transaction message (Send 100 LUM)
147147
const sendMsg = LumMessages.BuildMsgSend(
148-
wallet.address,
148+
wallet.getAddress(),
149149
toAddress,
150150
[{ denom: LumConstants.LumDenom, amount: '100' }],
151151
);
@@ -155,7 +155,7 @@ const fee = {
155155
gas: '100000',
156156
};
157157
// Fetch account number and sequence
158-
const account = await testnetClient.getAccount(wallet.address);
158+
const account = await testnetClient.getAccount(wallet.getAddress());
159159
// Create the transaction document
160160
const doc = {
161161
accountNumber: account.accountNumber,
@@ -176,10 +176,10 @@ console.log(`Broadcast success: ${LumUtils.broadcastTxCommitSuccess(broadcastRes
176176
The underlying tendermint client is directly accessible via the `.tmClient` property of the LumClient.
177177

178178
```typescript
179-
const health = await testnetClient.tmClient.health();
180-
const status = await testnetClient.tmClient.status();
181-
const genesis = await testnetClient.tmClient.genesis();
182-
const latestBlock = await testnetClient.tmClient.block();
179+
const health = await testnetClient.tmClient.health();
180+
const status = await testnetClient.tmClient.status();
181+
const genesis = await testnetClient.tmClient.genesis();
182+
const latestBlock = await testnetClient.tmClient.block();
183183
```
184184

185185
### Use all modules RPCs
@@ -189,6 +189,19 @@ The underlying query client is directly accessible via the `.queryClient` proper
189189
It allows to directly query all modules endpoints such as:
190190

191191
```typescript
192-
const supplies = await clt.queryClient.bank.unverified.totalSupply();
193-
// [{ denom: 'lum', amount: '1000000' }]
192+
const supplies = await clt.queryClient.bank.unverified.totalSupply();
193+
// [{ denom: 'lum', amount: '1000000' }]
194+
```
195+
196+
### Message signature & verification
197+
198+
#### Sign a message
199+
```typescript
200+
const message = 'Lum network is an awesome decentralized protocol';
201+
const signedPayload = await wallet.signMessage(message);
202+
// { address, publicKey, msg, sig, version, signer }
203+
const validSig = await LumUtils.verifySignMsg(signedPayload);
204+
// true
205+
const invalidSig = await LumUtils.verifySignMsg(Object.assign(signedPayload, { msg: 'Wrong message input' }));
206+
// false
194207
```

docs/lib/classes/lumledgerwallet.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [getAddress](lumledgerwallet.md#getaddress)
2626
- [getAppConfiguration](lumledgerwallet.md#getappconfiguration)
2727
- [getPublicKey](lumledgerwallet.md#getpublickey)
28+
- [signMessage](lumledgerwallet.md#signmessage)
2829
- [signTransaction](lumledgerwallet.md#signtransaction)
2930
- [signingMode](lumledgerwallet.md#signingmode)
3031
- [useAccount](lumledgerwallet.md#useaccount)
@@ -127,6 +128,22 @@ Inherited from: [LumWallet](lumwallet.md)
127128

128129
___
129130

131+
### signMessage
132+
133+
**signMessage**(`msg`: *string*): *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>
134+
135+
#### Parameters:
136+
137+
Name | Type |
138+
:------ | :------ |
139+
`msg` | *string* |
140+
141+
**Returns:** *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>
142+
143+
Inherited from: [LumWallet](lumwallet.md)
144+
145+
___
146+
130147
### signTransaction
131148

132149
**signTransaction**(`doc`: [*Doc*](../interfaces/lumtypes.doc.md)): *Promise*<Uint8Array\>

docs/lib/classes/lumpaperwallet.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [canChangeAccount](lumpaperwallet.md#canchangeaccount)
2525
- [getAddress](lumpaperwallet.md#getaddress)
2626
- [getPublicKey](lumpaperwallet.md#getpublickey)
27+
- [signMessage](lumpaperwallet.md#signmessage)
2728
- [signTransaction](lumpaperwallet.md#signtransaction)
2829
- [signingMode](lumpaperwallet.md#signingmode)
2930
- [useAccount](lumpaperwallet.md#useaccount)
@@ -120,6 +121,22 @@ Inherited from: [LumWallet](lumwallet.md)
120121

121122
___
122123

124+
### signMessage
125+
126+
**signMessage**(`msg`: *string*): *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>
127+
128+
#### Parameters:
129+
130+
Name | Type |
131+
:------ | :------ |
132+
`msg` | *string* |
133+
134+
**Returns:** *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>
135+
136+
Inherited from: [LumWallet](lumwallet.md)
137+
138+
___
139+
123140
### signTransaction
124141

125142
**signTransaction**(`doc`: [*Doc*](../interfaces/lumtypes.doc.md)): *Promise*<Uint8Array\>

docs/lib/classes/lumwallet.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [canChangeAccount](lumwallet.md#canchangeaccount)
2525
- [getAddress](lumwallet.md#getaddress)
2626
- [getPublicKey](lumwallet.md#getpublickey)
27+
- [signMessage](lumwallet.md#signmessage)
2728
- [signTransaction](lumwallet.md#signtransaction)
2829
- [signingMode](lumwallet.md#signingmode)
2930
- [useAccount](lumwallet.md#useaccount)
@@ -91,6 +92,23 @@ wallet public key (secp256k1)
9192

9293
___
9394

95+
### signMessage
96+
97+
`Abstract`**signMessage**(`msg`: *string*): *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>
98+
99+
Sign a message using a LumWallet
100+
Provided for signature generation and verification as signature will depend on the wallet payload implementation
101+
102+
#### Parameters:
103+
104+
Name | Type | Description |
105+
:------ | :------ | :------ |
106+
`msg` | *string* | message to sign |
107+
108+
**Returns:** *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>
109+
110+
___
111+
94112
### signTransaction
95113

96114
`Abstract`**signTransaction**(`doc`: [*Doc*](../interfaces/lumtypes.doc.md)): *Promise*<Uint8Array\>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Enumeration: LumMessageSigner
2+
3+
[LumConstants](../modules/lumconstants.md).LumMessageSigner
4+
5+
Signing wallets
6+
7+
## Table of contents
8+
9+
### Enumeration members
10+
11+
- [LEDGER](lumconstants.lummessagesigner.md#ledger)
12+
- [PAPER](lumconstants.lummessagesigner.md#paper)
13+
14+
## Enumeration members
15+
16+
### LEDGER
17+
18+
**LEDGER**: = "lum-sdk/ledger"
19+
20+
___
21+
22+
### PAPER
23+
24+
**PAPER**: = "lum-sdk/paper"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Interface: SignMsg
2+
3+
[LumTypes](../modules/lumtypes.md).SignMsg
4+
5+
## Table of contents
6+
7+
### Properties
8+
9+
- [address](lumtypes.signmsg.md#address)
10+
- [msg](lumtypes.signmsg.md#msg)
11+
- [publicKey](lumtypes.signmsg.md#publickey)
12+
- [sig](lumtypes.signmsg.md#sig)
13+
- [signer](lumtypes.signmsg.md#signer)
14+
- [version](lumtypes.signmsg.md#version)
15+
16+
## Properties
17+
18+
### address
19+
20+
**address**: *string*
21+
22+
Address of the signer
23+
24+
___
25+
26+
### msg
27+
28+
**msg**: *string*
29+
30+
Message
31+
32+
___
33+
34+
### publicKey
35+
36+
**publicKey**: *Uint8Array*
37+
38+
publicKey of the signer
39+
40+
___
41+
42+
### sig
43+
44+
**sig**: *Uint8Array*
45+
46+
Message signature
47+
48+
___
49+
50+
### signer
51+
52+
**signer**: [*LumMessageSigner*](../enums/lumconstants.lummessagesigner.md)
53+
54+
Signer wallet identifier
55+
56+
___
57+
58+
### version
59+
60+
**version**: *string*
61+
62+
Signing version

docs/lib/modules/lumconstants.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Table of contents
44

5+
### Enumerations
6+
7+
- [LumMessageSigner](../enums/lumconstants.lummessagesigner.md)
8+
59
### Variables
610

711
- [HDPath](lumconstants.md#hdpath)
@@ -12,6 +16,7 @@
1216
- [LumBech32PrefixValAddr](lumconstants.md#lumbech32prefixvaladdr)
1317
- [LumBech32PrefixValPub](lumconstants.md#lumbech32prefixvalpub)
1418
- [LumDenom](lumconstants.md#lumdenom)
19+
- [LumWalletSigningVersion](lumconstants.md#lumwalletsigningversion)
1520
- [PrivateKeyLength](lumconstants.md#privatekeylength)
1621

1722
### Functions
@@ -88,6 +93,14 @@ Lum Coin denomination
8893

8994
___
9095

96+
### LumWalletSigningVersion
97+
98+
`Const` **LumWalletSigningVersion**: *1*= '1'
99+
100+
Signing version of the SDK
101+
102+
___
103+
91104
### PrivateKeyLength
92105

93106
`Const` **PrivateKeyLength**: *32*= 32

docs/lib/modules/lumtypes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [LogEvent](../interfaces/lumtypes.logevent.md)
1717
- [PubKey](../interfaces/lumtypes.pubkey.md)
1818
- [SignDoc](../interfaces/lumtypes.signdoc.md)
19+
- [SignMsg](../interfaces/lumtypes.signmsg.md)
1920
- [Tx](../interfaces/lumtypes.tx.md)
2021

2122
### Variables

docs/lib/modules/lumutils.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- [sha3](lumutils.md#sha3)
4141
- [sortJSON](lumutils.md#sortjson)
4242
- [toJSON](lumutils.md#tojson)
43+
- [verifySignMsg](lumutils.md#verifysignmsg)
4344
- [verifySignature](lumutils.md#verifysignature)
4445

4546
## Functions
@@ -580,9 +581,23 @@ Name | Type | Description |
580581

581582
___
582583

584+
### verifySignMsg
585+
586+
`Const`**verifySignMsg**(`msg`: [*SignMsg*](../interfaces/lumtypes.signmsg.md)): *Promise*<boolean\>
587+
588+
#### Parameters:
589+
590+
Name | Type |
591+
:------ | :------ |
592+
`msg` | [*SignMsg*](../interfaces/lumtypes.signmsg.md) |
593+
594+
**Returns:** *Promise*<boolean\>
595+
596+
___
597+
583598
### verifySignature
584599

585-
`Const`**verifySignature**(`signature`: *Uint8Array*, `signDocBytes`: *Uint8Array*, `publicKey`: *Uint8Array*): *Promise*<boolean\>
600+
`Const`**verifySignature**(`signature`: *Uint8Array*, `signedBytes`: *Uint8Array*, `publicKey`: *Uint8Array*): *Promise*<boolean\>
586601

587602
Verify that a transaction signature is valid
588603

@@ -591,7 +606,7 @@ Verify that a transaction signature is valid
591606
Name | Type | Description |
592607
:------ | :------ | :------ |
593608
`signature` | *Uint8Array* | transaction signature (as generated by the generateSignature function) |
594-
`signDocBytes` | *Uint8Array* | sign doc bytes (as generated by the generateSignDocBytes function) |
609+
`signedBytes` | *Uint8Array* | signed bytes (as generated by the generateSignDocBytes function or by the signMessage function) |
595610
`publicKey` | *Uint8Array* | public key of the signing key pair (secp256k1) |
596611

597612
**Returns:** *Promise*<boolean\>

src/constants/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ export const getLumHdPath = (accountIndex = 0, walletIndex = 0): string => {
5454
* Private Key length
5555
*/
5656
export const PrivateKeyLength = 32;
57+
58+
/**
59+
* Signing version of the SDK
60+
*/
61+
export const LumWalletSigningVersion = '1';
62+
63+
/**
64+
* Signing wallets
65+
*/
66+
export enum LumMessageSigner {
67+
PAPER = 'lum-sdk/paper',
68+
LEDGER = 'lum-sdk/ledger',
69+
}

0 commit comments

Comments
 (0)