@exodus/solana-lib 1.2.14 → 1.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -5
- package/src/constants.js +12 -0
- package/src/encode.js +57 -0
- package/src/fee-data/index.js +1 -0
- package/src/fee-data/solana.js +9 -0
- package/src/helpers/spl-token.js +108 -0
- package/src/helpers/tokenTransfer.js +72 -0
- package/src/index.js +9 -0
- package/src/keypair.js +32 -0
- package/src/tokens.js +12 -0
- package/src/transaction.js +292 -0
- package/src/tx/create-and-sign-tx.js +8 -0
- package/{lib → src}/tx/create-unsigned-tx.js +11 -18
- package/src/tx/index.js +4 -0
- package/src/tx/parse-unsigned-tx.js +41 -0
- package/src/tx/sign-unsigned-tx.js +78 -0
- package/src/vendor/account.js +38 -0
- package/src/vendor/index.js +9 -0
- package/src/vendor/instruction.js +46 -0
- package/src/vendor/message.js +216 -0
- package/src/vendor/nonce-account.js +46 -0
- package/src/vendor/publickey.js +212 -0
- package/src/vendor/stake-program.js +527 -0
- package/src/vendor/system-program.js +782 -0
- package/src/vendor/sysvar.js +16 -0
- package/src/vendor/transaction.js +594 -0
- package/src/vendor/utils/blockhash.js +6 -0
- package/src/vendor/utils/fee-calculator.js +17 -0
- package/src/vendor/utils/layout.js +80 -0
- package/src/vendor/utils/shortvec-encoding.js +30 -0
- package/src/vendor/utils/to-buffer.js +9 -0
- package/lib/constants.js +0 -19
- package/lib/encode.js +0 -67
- package/lib/fee-data/index.js +0 -15
- package/lib/fee-data/solana.js +0 -14
- package/lib/helpers/spl-token.js +0 -122
- package/lib/helpers/tokenTransfer.js +0 -78
- package/lib/index.js +0 -88
- package/lib/keypair.js +0 -38
- package/lib/tokens.js +0 -21
- package/lib/transaction.js +0 -338
- package/lib/tx/create-and-sign-tx.js +0 -15
- package/lib/tx/index.js +0 -53
- package/lib/tx/parse-unsigned-tx.js +0 -55
- package/lib/tx/sign-unsigned-tx.js +0 -90
- package/lib/vendor/account.js +0 -48
- package/lib/vendor/index.js +0 -113
- package/lib/vendor/instruction.js +0 -48
- package/lib/vendor/message.js +0 -167
- package/lib/vendor/nonce-account.js +0 -56
- package/lib/vendor/publickey.js +0 -211
- package/lib/vendor/stake-program.js +0 -476
- package/lib/vendor/system-program.js +0 -640
- package/lib/vendor/sysvar.js +0 -19
- package/lib/vendor/transaction.js +0 -594
- package/lib/vendor/utils/blockhash.js +0 -1
- package/lib/vendor/utils/fee-calculator.js +0 -25
- package/lib/vendor/utils/layout.js +0 -97
- package/lib/vendor/utils/shortvec-encoding.js +0 -41
- package/lib/vendor/utils/to-buffer.js +0 -18
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import BN from 'bn.js'
|
|
3
|
+
import bs58 from 'bs58'
|
|
4
|
+
import nacl from 'tweetnacl'
|
|
5
|
+
import createHash from 'create-hash'
|
|
6
|
+
|
|
7
|
+
// $FlowFixMe
|
|
8
|
+
let naclLowLevel = nacl.lowlevel
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Maximum length of derived pubkey seed
|
|
12
|
+
*/
|
|
13
|
+
export const MAX_SEED_LENGTH = 32
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A public key
|
|
17
|
+
*/
|
|
18
|
+
export class PublicKey {
|
|
19
|
+
_bn: BN
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a new PublicKey object
|
|
23
|
+
*/
|
|
24
|
+
constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
|
|
25
|
+
if (typeof value === 'string') {
|
|
26
|
+
// assume base 58 encoding by default
|
|
27
|
+
const decoded = bs58.decode(value)
|
|
28
|
+
if (decoded.length !== 32) {
|
|
29
|
+
throw new Error(`Invalid public key input`)
|
|
30
|
+
}
|
|
31
|
+
this._bn = new BN(decoded)
|
|
32
|
+
} else {
|
|
33
|
+
this._bn = new BN(value)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (this._bn.byteLength() > 32) {
|
|
37
|
+
throw new Error(`Invalid public key input`)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Checks if two publicKeys are equal
|
|
43
|
+
*/
|
|
44
|
+
equals(publicKey: PublicKey): boolean {
|
|
45
|
+
return this._bn.eq(publicKey._bn)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Return the base-58 representation of the public key
|
|
50
|
+
*/
|
|
51
|
+
toBase58(): string {
|
|
52
|
+
return bs58.encode(this.toBuffer())
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Return the Buffer representation of the public key
|
|
57
|
+
*/
|
|
58
|
+
toBuffer(): Buffer {
|
|
59
|
+
const b = this._bn.toArrayLike(Buffer)
|
|
60
|
+
if (b.length === 32) {
|
|
61
|
+
return b
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const zeroPad = Buffer.alloc(32)
|
|
65
|
+
b.copy(zeroPad, 32 - b.length)
|
|
66
|
+
return zeroPad
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Returns a string representation of the public key
|
|
71
|
+
*/
|
|
72
|
+
toString(): string {
|
|
73
|
+
return this.toBase58()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Find a valid program address
|
|
78
|
+
*
|
|
79
|
+
* Valid program addresses must fall off the ed25519 curve. This function
|
|
80
|
+
* iterates a nonce until it finds one that when combined with the seeds
|
|
81
|
+
* results in a valid program address.
|
|
82
|
+
* HACK: transformed in sync function
|
|
83
|
+
*/
|
|
84
|
+
static findProgramAddress(seeds: Array<Buffer | Uint8Array>, programId: PublicKey) {
|
|
85
|
+
let nonce = 255
|
|
86
|
+
let address
|
|
87
|
+
while (nonce !== 0) {
|
|
88
|
+
try {
|
|
89
|
+
const seedsWithNonce = seeds.concat(Buffer.from([nonce]))
|
|
90
|
+
address = this.createProgramAddress(seedsWithNonce, programId)
|
|
91
|
+
} catch (err) {
|
|
92
|
+
nonce--
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
return [address, nonce]
|
|
96
|
+
}
|
|
97
|
+
throw new Error('Unable to find a viable program address nonce')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Derive a public key from another key, a seed, and a program ID.
|
|
102
|
+
* HACK: transformed in sync function
|
|
103
|
+
*/
|
|
104
|
+
static createWithSeed(
|
|
105
|
+
fromPublicKey: PublicKey,
|
|
106
|
+
seed: string,
|
|
107
|
+
programId: PublicKey
|
|
108
|
+
): Promise<PublicKey> {
|
|
109
|
+
const buffer = Buffer.concat([
|
|
110
|
+
fromPublicKey.toBuffer(),
|
|
111
|
+
Buffer.from(seed),
|
|
112
|
+
programId.toBuffer(),
|
|
113
|
+
])
|
|
114
|
+
const hash = createHash('sha256')
|
|
115
|
+
.update(buffer)
|
|
116
|
+
.digest('hex')
|
|
117
|
+
return new PublicKey(Buffer.from(hash, 'hex'))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Derive a program address from seeds and a program ID.
|
|
122
|
+
*/
|
|
123
|
+
static createProgramAddress(seeds: Array<Buffer | Uint8Array>, programId: PublicKey) {
|
|
124
|
+
let buffer = Buffer.alloc(0)
|
|
125
|
+
seeds.forEach(function(seed) {
|
|
126
|
+
if (seed.length > MAX_SEED_LENGTH) {
|
|
127
|
+
throw new Error('Max seed length exceeded')
|
|
128
|
+
}
|
|
129
|
+
buffer = Buffer.concat([buffer, Buffer.from(seed)])
|
|
130
|
+
})
|
|
131
|
+
buffer = Buffer.concat([buffer, programId.toBuffer(), Buffer.from('ProgramDerivedAddress')])
|
|
132
|
+
let hash = createHash('sha256')
|
|
133
|
+
.update(buffer)
|
|
134
|
+
.digest('hex')
|
|
135
|
+
let publicKeyBytes = new BN(hash, 16).toArray(null, 32)
|
|
136
|
+
if (isOnCurve(publicKeyBytes)) {
|
|
137
|
+
throw new Error('Invalid seeds, address must fall off the curve')
|
|
138
|
+
}
|
|
139
|
+
return new PublicKey(publicKeyBytes)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Check that a pubkey is on the curve.
|
|
144
|
+
// This function and its dependents were sourced from:
|
|
145
|
+
// https://github.com/dchest/tweetnacl-js/blob/f1ec050ceae0861f34280e62498b1d3ed9c350c6/nacl.js#L792
|
|
146
|
+
function isOnCurve(p) {
|
|
147
|
+
let r = [naclLowLevel.gf(), naclLowLevel.gf(), naclLowLevel.gf(), naclLowLevel.gf()]
|
|
148
|
+
|
|
149
|
+
let t = naclLowLevel.gf()
|
|
150
|
+
let chk = naclLowLevel.gf()
|
|
151
|
+
let num = naclLowLevel.gf()
|
|
152
|
+
let den = naclLowLevel.gf()
|
|
153
|
+
let den2 = naclLowLevel.gf()
|
|
154
|
+
let den4 = naclLowLevel.gf()
|
|
155
|
+
let den6 = naclLowLevel.gf()
|
|
156
|
+
|
|
157
|
+
naclLowLevel.set25519(r[2], gf1)
|
|
158
|
+
naclLowLevel.unpack25519(r[1], p)
|
|
159
|
+
naclLowLevel.S(num, r[1])
|
|
160
|
+
naclLowLevel.M(den, num, naclLowLevel.D)
|
|
161
|
+
naclLowLevel.Z(num, num, r[2])
|
|
162
|
+
naclLowLevel.A(den, r[2], den)
|
|
163
|
+
|
|
164
|
+
naclLowLevel.S(den2, den)
|
|
165
|
+
naclLowLevel.S(den4, den2)
|
|
166
|
+
naclLowLevel.M(den6, den4, den2)
|
|
167
|
+
naclLowLevel.M(t, den6, num)
|
|
168
|
+
naclLowLevel.M(t, t, den)
|
|
169
|
+
|
|
170
|
+
naclLowLevel.pow2523(t, t)
|
|
171
|
+
naclLowLevel.M(t, t, num)
|
|
172
|
+
naclLowLevel.M(t, t, den)
|
|
173
|
+
naclLowLevel.M(t, t, den)
|
|
174
|
+
naclLowLevel.M(r[0], t, den)
|
|
175
|
+
|
|
176
|
+
naclLowLevel.S(chk, r[0])
|
|
177
|
+
naclLowLevel.M(chk, chk, den)
|
|
178
|
+
if (neq25519(chk, num)) naclLowLevel.M(r[0], r[0], I)
|
|
179
|
+
|
|
180
|
+
naclLowLevel.S(chk, r[0])
|
|
181
|
+
naclLowLevel.M(chk, chk, den)
|
|
182
|
+
if (neq25519(chk, num)) return 0
|
|
183
|
+
return 1
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let gf1 = naclLowLevel.gf([1])
|
|
187
|
+
let I = naclLowLevel.gf([
|
|
188
|
+
0xa0b0,
|
|
189
|
+
0x4a0e,
|
|
190
|
+
0x1b27,
|
|
191
|
+
0xc4ee,
|
|
192
|
+
0xe478,
|
|
193
|
+
0xad2f,
|
|
194
|
+
0x1806,
|
|
195
|
+
0x2f43,
|
|
196
|
+
0xd7a7,
|
|
197
|
+
0x3dfb,
|
|
198
|
+
0x0099,
|
|
199
|
+
0x2b4d,
|
|
200
|
+
0xdf0b,
|
|
201
|
+
0x4fc1,
|
|
202
|
+
0x2480,
|
|
203
|
+
0x2b83,
|
|
204
|
+
])
|
|
205
|
+
|
|
206
|
+
function neq25519(a, b) {
|
|
207
|
+
var c = new Uint8Array(32)
|
|
208
|
+
var d = new Uint8Array(32)
|
|
209
|
+
naclLowLevel.pack25519(c, a)
|
|
210
|
+
naclLowLevel.pack25519(d, b)
|
|
211
|
+
return naclLowLevel.crypto_verify_32(c, 0, d, 0)
|
|
212
|
+
}
|