1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict'
|
12 |
|
13 | import { PrivKey as DefaultPrivKey } from './priv-key'
|
14 | import { PubKey } from './pub-key'
|
15 | import { Struct } from './struct'
|
16 | import { Bw } from './bw'
|
17 |
|
18 | class KeyPair extends Struct {
|
19 | constructor (privKey, pubKey, PrivKey = DefaultPrivKey) {
|
20 | super({ privKey, pubKey })
|
21 | this.PrivKey = PrivKey
|
22 | }
|
23 |
|
24 | fromJSON (json) {
|
25 | if (json.privKey) {
|
26 | this.privKey = this.PrivKey.fromJSON(json.privKey)
|
27 | }
|
28 | if (json.pubKey) {
|
29 | this.pubKey = PubKey.fromJSON(json.pubKey)
|
30 | }
|
31 | return this
|
32 | }
|
33 |
|
34 | fromBr (br) {
|
35 | const buflen1 = br.readUInt8()
|
36 | if (buflen1 > 0) {
|
37 | this.privKey = new this.PrivKey().fromFastBuffer(br.read(buflen1))
|
38 | }
|
39 | const buflen2 = br.readUInt8()
|
40 | if (buflen2 > 0) {
|
41 | this.pubKey = new PubKey().fromFastBuffer(br.read(buflen2))
|
42 | }
|
43 | return this
|
44 | }
|
45 |
|
46 | toBw (bw) {
|
47 | if (!bw) {
|
48 | bw = new Bw()
|
49 | }
|
50 | if (this.privKey) {
|
51 | const privKeybuf = this.privKey.toFastBuffer()
|
52 | bw.writeUInt8(privKeybuf.length)
|
53 | bw.write(privKeybuf)
|
54 | } else {
|
55 | bw.writeUInt8(0)
|
56 | }
|
57 | if (this.pubKey) {
|
58 | const pubKeybuf = this.pubKey.toFastBuffer()
|
59 | bw.writeUInt8(pubKeybuf.length)
|
60 | bw.write(pubKeybuf)
|
61 | } else {
|
62 | bw.writeUInt8(0)
|
63 | }
|
64 | return bw
|
65 | }
|
66 |
|
67 | fromString (str) {
|
68 | return this.fromJSON(JSON.parse(str))
|
69 | }
|
70 |
|
71 | toString () {
|
72 | return JSON.stringify(this.toJSON())
|
73 | }
|
74 |
|
75 | toPublic () {
|
76 | const keyPair = new KeyPair().fromObject(this)
|
77 | keyPair.privKey = undefined
|
78 | return keyPair
|
79 | }
|
80 |
|
81 | fromPrivKey (privKey) {
|
82 | this.privKey = privKey
|
83 | this.pubKey = new PubKey().fromPrivKey(privKey)
|
84 | return this
|
85 | }
|
86 |
|
87 | static fromPrivKey (privKey) {
|
88 | return new this().fromPrivKey(privKey)
|
89 | }
|
90 |
|
91 | async asyncFromPrivKey (privKey) {
|
92 | this.privKey = privKey
|
93 | this.pubKey = await new PubKey().asyncFromPrivKey(privKey)
|
94 | return this
|
95 | }
|
96 |
|
97 | static asyncFromPrivKey (privKey) {
|
98 | return new this().asyncFromPrivKey(privKey)
|
99 | }
|
100 |
|
101 | fromRandom () {
|
102 | this.privKey = new this.PrivKey().fromRandom()
|
103 | this.pubKey = new PubKey().fromPrivKey(this.privKey)
|
104 | return this
|
105 | }
|
106 |
|
107 | static fromRandom () {
|
108 | return new this().fromRandom()
|
109 | }
|
110 |
|
111 | async asyncFromRandom () {
|
112 | this.privKey = new this.PrivKey().fromRandom()
|
113 | return this.asyncFromPrivKey(this.privKey)
|
114 | }
|
115 |
|
116 | static asyncFromRandom () {
|
117 | return new this().asyncFromRandom()
|
118 | }
|
119 | }
|
120 |
|
121 | KeyPair.Mainnet = class extends KeyPair {
|
122 | constructor (privKey, pubKey) {
|
123 | super(privKey, pubKey, DefaultPrivKey.Mainnet)
|
124 | }
|
125 | }
|
126 |
|
127 | KeyPair.Testnet = class extends KeyPair {
|
128 | constructor (privKey, pubKey) {
|
129 | super(privKey, pubKey, DefaultPrivKey.Testnet)
|
130 | }
|
131 | }
|
132 |
|
133 | export { KeyPair }
|