-
Notifications
You must be signed in to change notification settings - Fork 22
/
Wallet.ts
102 lines (92 loc) 路 2.84 KB
/
Wallet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { LCDClient } from './LCDClient';
import { Key } from '../../key';
import { CreateTxOptions } from '../lcd/api/TxAPI';
import { Tx } from '../../core/Tx';
import { SignMode as SignModeV1 } from '@terra-money/legacy.proto/cosmos/tx/signing/v1beta1/signing';
import { SignMode as SignModeV2 } from '@terra-money/terra.proto/cosmos/tx/signing/v1beta1/signing';
import { MsgAminoCustom } from '../../core';
export class Wallet {
constructor(public lcd: LCDClient, public key: Key) {}
public accountNumberAndSequence(chainID: string): Promise<{
account_number: number;
sequence: number;
}> {
return this.lcd.auth
.accountInfo(this.key.accAddress(this.lcd.config[chainID].prefix))
.then(d => {
return {
account_number: d.getAccountNumber(),
sequence: d.getSequenceNumber(),
};
});
}
public accountNumber(chainID: string): Promise<number> {
return this.lcd.auth
.accountInfo(this.key.accAddress(this.lcd.config[chainID].prefix))
.then(d => {
return d.getAccountNumber();
});
}
public sequence(chainID: string): Promise<number> {
return this.lcd.auth
.accountInfo(this.key.accAddress(this.lcd.config[chainID].prefix))
.then(d => {
return d.getSequenceNumber();
});
}
public async createTx(
options: CreateTxOptions & {
sequence?: number;
}
): Promise<Tx> {
return this.lcd.tx.create(
[
{
address: this.key.accAddress(this.lcd.config[options.chainID].prefix),
sequenceNumber: options.sequence,
publicKey: this.key.publicKey,
},
],
options
);
}
public async createAndSignTx(
options: CreateTxOptions & {
sequence?: number;
accountNumber?: number;
signMode?: SignModeV1 | SignModeV2;
chainID: string;
}
): Promise<Tx> {
let accountNumber = options.accountNumber;
let sequence = options.sequence;
if (accountNumber === undefined || sequence === undefined) {
const res = await this.accountNumberAndSequence(options.chainID);
if (accountNumber === undefined) {
accountNumber = res.account_number;
}
if (sequence === undefined) {
sequence = res.sequence;
}
}
options.sequence = sequence;
options.accountNumber = accountNumber;
const tx = await this.createTx(options);
return this.key.signTx(
tx,
{
accountNumber,
sequence,
chainID: options.chainID,
signMode:
options.signMode ||
(options.msgs.find(m => m instanceof MsgAminoCustom)
? SignModeV2.SIGN_MODE_LEGACY_AMINO_JSON
: this.lcd.config[options.chainID].isClassic
? SignModeV1.SIGN_MODE_DIRECT
: SignModeV2.SIGN_MODE_DIRECT),
},
this.lcd.config[options.chainID].isClassic
);
}
}