@exodus/solana-lib 1.2.8 → 1.2.9-build
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/lib/constants.js +19 -0
- package/lib/encode.js +67 -0
- package/lib/fee-data/index.js +15 -0
- package/lib/fee-data/solana.js +14 -0
- package/lib/helpers/spl-token.js +122 -0
- package/lib/helpers/tokenTransfer.js +78 -0
- package/lib/index.js +79 -0
- package/lib/keypair.js +38 -0
- package/lib/tokens.js +21 -0
- package/lib/transaction.js +279 -0
- package/lib/tx/create-and-sign-tx.js +15 -0
- package/{src → lib}/tx/create-unsigned-tx.js +20 -11
- package/lib/tx/index.js +53 -0
- package/lib/tx/parse-unsigned-tx.js +55 -0
- package/lib/tx/sign-unsigned-tx.js +90 -0
- package/lib/vendor/account.js +48 -0
- package/lib/vendor/index.js +113 -0
- package/lib/vendor/instruction.js +48 -0
- package/lib/vendor/message.js +167 -0
- package/lib/vendor/nonce-account.js +56 -0
- package/lib/vendor/publickey.js +211 -0
- package/lib/vendor/stake-program.js +476 -0
- package/lib/vendor/system-program.js +640 -0
- package/lib/vendor/sysvar.js +19 -0
- package/lib/vendor/transaction.js +594 -0
- package/lib/vendor/utils/blockhash.js +1 -0
- package/lib/vendor/utils/fee-calculator.js +25 -0
- package/lib/vendor/utils/layout.js +97 -0
- package/lib/vendor/utils/shortvec-encoding.js +41 -0
- package/lib/vendor/utils/to-buffer.js +18 -0
- package/package.json +4 -5
- package/src/constants.js +0 -12
- package/src/encode.js +0 -57
- package/src/fee-data/index.js +0 -1
- package/src/fee-data/solana.js +0 -9
- package/src/helpers/spl-token.js +0 -108
- package/src/helpers/tokenTransfer.js +0 -72
- package/src/index.js +0 -8
- package/src/keypair.js +0 -32
- package/src/tokens.js +0 -19
- package/src/transaction.js +0 -240
- package/src/tx/create-and-sign-tx.js +0 -8
- package/src/tx/index.js +0 -4
- package/src/tx/parse-unsigned-tx.js +0 -39
- package/src/tx/sign-unsigned-tx.js +0 -78
- package/src/vendor/account.js +0 -38
- package/src/vendor/index.js +0 -9
- package/src/vendor/instruction.js +0 -46
- package/src/vendor/message.js +0 -216
- package/src/vendor/nonce-account.js +0 -46
- package/src/vendor/publickey.js +0 -212
- package/src/vendor/stake-program.js +0 -527
- package/src/vendor/system-program.js +0 -782
- package/src/vendor/sysvar.js +0 -16
- package/src/vendor/transaction.js +0 -594
- package/src/vendor/utils/blockhash.js +0 -6
- package/src/vendor/utils/fee-calculator.js +0 -17
- package/src/vendor/utils/layout.js +0 -80
- package/src/vendor/utils/shortvec-encoding.js +0 -30
- package/src/vendor/utils/to-buffer.js +0 -9
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _assert = _interopRequireDefault(require("assert"));
|
|
9
|
+
|
|
10
|
+
var _keypair = require("./keypair");
|
|
11
|
+
|
|
12
|
+
var _encode = require("./encode");
|
|
13
|
+
|
|
14
|
+
var _tokenTransfer = require("./helpers/tokenTransfer");
|
|
15
|
+
|
|
16
|
+
var _vendor = require("./vendor");
|
|
17
|
+
|
|
18
|
+
var _constants = require("./constants");
|
|
19
|
+
|
|
20
|
+
var _tokens = _interopRequireDefault(require("./tokens"));
|
|
21
|
+
|
|
22
|
+
var _bs = _interopRequireDefault(require("bs58"));
|
|
23
|
+
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
+
|
|
26
|
+
class Tx {
|
|
27
|
+
constructor({
|
|
28
|
+
from,
|
|
29
|
+
to,
|
|
30
|
+
amount,
|
|
31
|
+
recentBlockhash,
|
|
32
|
+
// fee, // (Fee per Signature: 5000 lamports)
|
|
33
|
+
// Tokens related:
|
|
34
|
+
tokenName,
|
|
35
|
+
destinationAddressType,
|
|
36
|
+
isAssociatedTokenAccountActive,
|
|
37
|
+
// true when recipient balance !== 0
|
|
38
|
+
fromTokenAddresses // sender token addresses
|
|
39
|
+
|
|
40
|
+
} = {}) {
|
|
41
|
+
(0, _assert.default)(from, 'from is required');
|
|
42
|
+
(0, _assert.default)(to, 'to is required');
|
|
43
|
+
(0, _assert.default)(amount, 'amount is required');
|
|
44
|
+
(0, _assert.default)(typeof amount === 'number', 'amount must be a number');
|
|
45
|
+
(0, _assert.default)(recentBlockhash, 'recentBlockhash is required');
|
|
46
|
+
|
|
47
|
+
if (tokenName) {
|
|
48
|
+
(0, _assert.default)(typeof destinationAddressType !== 'undefined', 'destinationAddressType is required when sending tokens');
|
|
49
|
+
(0, _assert.default)(typeof isAssociatedTokenAccountActive !== 'undefined', 'isAssociatedTokenAccountActive is required when sending tokens'); // needed to create the recipient account
|
|
50
|
+
|
|
51
|
+
(0, _assert.default)(Array.isArray(fromTokenAddresses), 'fromTokenAddresses Array is required');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const token = _tokens.default.find(({
|
|
55
|
+
tokenName: name
|
|
56
|
+
}) => name === tokenName);
|
|
57
|
+
|
|
58
|
+
this.txObj = {
|
|
59
|
+
from,
|
|
60
|
+
to,
|
|
61
|
+
amount,
|
|
62
|
+
recentBlockhash,
|
|
63
|
+
token,
|
|
64
|
+
destinationAddressType,
|
|
65
|
+
isAssociatedTokenAccountActive,
|
|
66
|
+
fromTokenAddresses
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
if (token) {
|
|
70
|
+
// TOKEN transfer tx
|
|
71
|
+
this.buildTokenTransaction(this.txObj);
|
|
72
|
+
} else {
|
|
73
|
+
// SOL tx
|
|
74
|
+
this.buildSOLtransaction(this.txObj);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
buildSOLtransaction({
|
|
79
|
+
from,
|
|
80
|
+
to,
|
|
81
|
+
amount,
|
|
82
|
+
recentBlockhash
|
|
83
|
+
}) {
|
|
84
|
+
const txInstruction = _vendor.SystemProgram.transfer({
|
|
85
|
+
fromPubkey: new _vendor.PublicKey(from),
|
|
86
|
+
toPubkey: new _vendor.PublicKey(to),
|
|
87
|
+
lamports: amount
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
this.transaction = new _vendor.Transaction({
|
|
91
|
+
instructions: [txInstruction],
|
|
92
|
+
recentBlockhash
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
buildTokenTransaction({
|
|
97
|
+
from,
|
|
98
|
+
to,
|
|
99
|
+
amount,
|
|
100
|
+
recentBlockhash,
|
|
101
|
+
token,
|
|
102
|
+
destinationAddressType,
|
|
103
|
+
isAssociatedTokenAccountActive,
|
|
104
|
+
fromTokenAddresses
|
|
105
|
+
}) {
|
|
106
|
+
this.transaction = new _vendor.Transaction({
|
|
107
|
+
recentBlockhash
|
|
108
|
+
});
|
|
109
|
+
const isUnknown = destinationAddressType === null;
|
|
110
|
+
const isSOLaddress = destinationAddressType === 'solana'; // crete account instruction
|
|
111
|
+
|
|
112
|
+
if (isUnknown) throw new Error('Destination SOL balance cannot be zero (address not active)'); // cannot initialize without knowing the owner
|
|
113
|
+
|
|
114
|
+
if (isSOLaddress && !isAssociatedTokenAccountActive) this.transaction.add((0, _tokenTransfer.createAssociatedTokenAccount)(from, token.mintAddress, to));
|
|
115
|
+
let amountLeft = amount;
|
|
116
|
+
let amountToSend;
|
|
117
|
+
|
|
118
|
+
for (let {
|
|
119
|
+
ticker,
|
|
120
|
+
tokenAccountAddress,
|
|
121
|
+
balance
|
|
122
|
+
} of fromTokenAddresses) {
|
|
123
|
+
// need to add more of this instruction until we reach the desired balance (amount) to send
|
|
124
|
+
(0, _assert.default)(ticker === token.tokenSymbol, `Got unexpected ticker ${ticker}`);
|
|
125
|
+
if (amountLeft === 0) break;
|
|
126
|
+
balance = Number(balance);
|
|
127
|
+
|
|
128
|
+
if (balance >= amountLeft) {
|
|
129
|
+
amountToSend = amountLeft;
|
|
130
|
+
amountLeft = 0;
|
|
131
|
+
} else {
|
|
132
|
+
amountToSend = balance; // not enough balance
|
|
133
|
+
|
|
134
|
+
amountLeft -= amountToSend;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const dest = isSOLaddress ? (0, _encode.findAssociatedTokenAddress)(to, token.mintAddress) : to; // add transfer token instruction
|
|
138
|
+
|
|
139
|
+
this.transaction.add((0, _tokenTransfer.createTokenTransferInstruction)(from, tokenAccountAddress, dest, amountToSend));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
(0, _assert.default)(amountLeft === 0, `Not enough balance to send ${amount} ${token.tokenSymbol}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static createStakeAccountTransaction({
|
|
146
|
+
address,
|
|
147
|
+
amount,
|
|
148
|
+
seed = _constants.SEED,
|
|
149
|
+
pool,
|
|
150
|
+
recentBlockhash
|
|
151
|
+
}) {
|
|
152
|
+
const fromPubkey = new _vendor.PublicKey(address);
|
|
153
|
+
const stakeAddress = (0, _encode.createStakeAddress)(address, seed);
|
|
154
|
+
const stakePublicKey = new _vendor.PublicKey(stakeAddress);
|
|
155
|
+
const poolKey = new _vendor.PublicKey(pool);
|
|
156
|
+
const authorized = new _vendor.Authorized(fromPubkey, fromPubkey); // staker, withdrawer
|
|
157
|
+
|
|
158
|
+
const lockup = new _vendor.Lockup(0, 0, fromPubkey); // no lockup
|
|
159
|
+
// create account instruction
|
|
160
|
+
|
|
161
|
+
const programTx = _vendor.StakeProgram.createAccountWithSeed({
|
|
162
|
+
fromPubkey,
|
|
163
|
+
stakePubkey: stakePublicKey,
|
|
164
|
+
basePubkey: fromPubkey,
|
|
165
|
+
seed,
|
|
166
|
+
authorized,
|
|
167
|
+
lockup,
|
|
168
|
+
lamports: amount // number
|
|
169
|
+
|
|
170
|
+
}); // delegate funds instruction
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
const delegateTx = _vendor.StakeProgram.delegate({
|
|
174
|
+
stakePubkey: stakePublicKey,
|
|
175
|
+
authorizedPubkey: fromPubkey,
|
|
176
|
+
votePubkey: poolKey // pool vote key
|
|
177
|
+
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const transaction = new _vendor.Transaction({
|
|
181
|
+
recentBlockhash
|
|
182
|
+
}).add(programTx).add(delegateTx);
|
|
183
|
+
return transaction;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
static undelegate({
|
|
187
|
+
address,
|
|
188
|
+
stakeAddresses,
|
|
189
|
+
recentBlockhash
|
|
190
|
+
}) {
|
|
191
|
+
// undelegate all stake addresses
|
|
192
|
+
(0, _assert.default)(Array.isArray(stakeAddresses), 'stakeAddresses Array is required');
|
|
193
|
+
const fromPubkey = new _vendor.PublicKey(address);
|
|
194
|
+
const transaction = new _vendor.Transaction({
|
|
195
|
+
recentBlockhash
|
|
196
|
+
});
|
|
197
|
+
stakeAddresses.forEach(stakeAddress => {
|
|
198
|
+
const stakePublicKey = new _vendor.PublicKey(stakeAddress);
|
|
199
|
+
|
|
200
|
+
const programTx = _vendor.StakeProgram.deactivate({
|
|
201
|
+
stakePubkey: stakePublicKey,
|
|
202
|
+
authorizedPubkey: fromPubkey
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
transaction.add(programTx);
|
|
206
|
+
});
|
|
207
|
+
return transaction;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static withdraw({
|
|
211
|
+
address,
|
|
212
|
+
stakeAddresses,
|
|
213
|
+
amount,
|
|
214
|
+
recentBlockhash
|
|
215
|
+
}) {
|
|
216
|
+
const fromPubkey = new _vendor.PublicKey(address);
|
|
217
|
+
const stakeAddress = Array.isArray(stakeAddresses) ? stakeAddresses[0] : stakeAddresses;
|
|
218
|
+
const stakePublicKey = new _vendor.PublicKey(stakeAddress);
|
|
219
|
+
|
|
220
|
+
const transaction = _vendor.StakeProgram.withdraw({
|
|
221
|
+
stakePubkey: stakePublicKey,
|
|
222
|
+
authorizedPubkey: fromPubkey,
|
|
223
|
+
toPubkey: fromPubkey,
|
|
224
|
+
lamports: amount
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
transaction.recentBlockhash = recentBlockhash;
|
|
228
|
+
return transaction;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static sign(tx, privateKey) {
|
|
232
|
+
if (!privateKey) throw new Error('Please provide a secretKey');
|
|
233
|
+
const {
|
|
234
|
+
secretKey
|
|
235
|
+
} = (0, _keypair.getKeyPairFromPrivateKey)(privateKey);
|
|
236
|
+
const signers = [new _vendor.Account(secretKey)];
|
|
237
|
+
let transaction = tx;
|
|
238
|
+
if (tx instanceof Tx) transaction = tx.transaction;
|
|
239
|
+
transaction.sign(...signers);
|
|
240
|
+
|
|
241
|
+
if (!transaction.signature) {
|
|
242
|
+
throw new Error('!signature'); // should never happen
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
serialize() {
|
|
247
|
+
const wireTransaction = this.transaction.serialize();
|
|
248
|
+
return wireTransaction.toString('base64');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
static serialize(tx) {
|
|
252
|
+
let transaction = tx;
|
|
253
|
+
if (tx instanceof Tx) transaction = tx.transaction;
|
|
254
|
+
return transaction.serialize().toString('base64');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
getTxId() {
|
|
258
|
+
if (!this.transaction.signature) {
|
|
259
|
+
throw new Error('Cannot get txId, tx is not signed');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return _bs.default.encode(this.transaction.signature);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
static getTxId(tx) {
|
|
266
|
+
let transaction = tx;
|
|
267
|
+
if (tx instanceof Tx) transaction = tx.transaction;
|
|
268
|
+
|
|
269
|
+
if (!transaction.signature) {
|
|
270
|
+
throw new Error('Cannot get txId, tx is not signed');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return _bs.default.encode(transaction.signature);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
var _default = Tx;
|
|
279
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createAndSignTx = createAndSignTx;
|
|
7
|
+
|
|
8
|
+
var _createUnsignedTx = require("./create-unsigned-tx");
|
|
9
|
+
|
|
10
|
+
var _signUnsignedTx = require("./sign-unsigned-tx");
|
|
11
|
+
|
|
12
|
+
function createAndSignTx(input, privateKey) {
|
|
13
|
+
const unsignedTx = (0, _createUnsignedTx.createUnsignedTx)(input);
|
|
14
|
+
return (0, _signUnsignedTx.signUnsignedTx)(unsignedTx, privateKey);
|
|
15
|
+
}
|
|
@@ -1,27 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createUnsignedTx = createUnsignedTx;
|
|
7
|
+
|
|
8
|
+
function createUnsignedTx({
|
|
4
9
|
asset,
|
|
5
10
|
from,
|
|
6
11
|
to,
|
|
7
12
|
amount,
|
|
13
|
+
fee,
|
|
8
14
|
recentBlockhash,
|
|
9
15
|
// Tokens related:
|
|
10
16
|
tokenName,
|
|
11
17
|
destinationAddressType,
|
|
12
|
-
isAssociatedTokenAccountActive,
|
|
13
|
-
|
|
18
|
+
isAssociatedTokenAccountActive,
|
|
19
|
+
// true when recipient balance !== 0
|
|
20
|
+
fromTokenAddresses,
|
|
21
|
+
// sender token addresses
|
|
14
22
|
// Staking related:
|
|
15
23
|
method,
|
|
16
24
|
stakeAddresses,
|
|
17
25
|
seed,
|
|
18
|
-
pool
|
|
19
|
-
}
|
|
26
|
+
pool
|
|
27
|
+
}) {
|
|
20
28
|
return {
|
|
21
29
|
txData: {
|
|
22
30
|
from,
|
|
23
31
|
to,
|
|
24
32
|
amount: amount.toBase().toNumber(),
|
|
33
|
+
fee: fee.toBase().toNumber(),
|
|
25
34
|
recentBlockhash,
|
|
26
35
|
// Tokens related:
|
|
27
36
|
tokenName,
|
|
@@ -32,10 +41,10 @@ export function createUnsignedTx({
|
|
|
32
41
|
method,
|
|
33
42
|
stakeAddresses,
|
|
34
43
|
seed,
|
|
35
|
-
pool
|
|
44
|
+
pool
|
|
36
45
|
},
|
|
37
46
|
txMeta: {
|
|
38
|
-
assetName: asset.name
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
47
|
+
assetName: asset.name
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
package/lib/tx/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _signUnsignedTx = require("./sign-unsigned-tx");
|
|
8
|
+
|
|
9
|
+
Object.keys(_signUnsignedTx).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
Object.defineProperty(exports, key, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () {
|
|
14
|
+
return _signUnsignedTx[key];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
var _parseUnsignedTx = require("./parse-unsigned-tx");
|
|
20
|
+
|
|
21
|
+
Object.keys(_parseUnsignedTx).forEach(function (key) {
|
|
22
|
+
if (key === "default" || key === "__esModule") return;
|
|
23
|
+
Object.defineProperty(exports, key, {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () {
|
|
26
|
+
return _parseUnsignedTx[key];
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
var _createUnsignedTx = require("./create-unsigned-tx");
|
|
32
|
+
|
|
33
|
+
Object.keys(_createUnsignedTx).forEach(function (key) {
|
|
34
|
+
if (key === "default" || key === "__esModule") return;
|
|
35
|
+
Object.defineProperty(exports, key, {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () {
|
|
38
|
+
return _createUnsignedTx[key];
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
var _createAndSignTx = require("./create-and-sign-tx");
|
|
44
|
+
|
|
45
|
+
Object.keys(_createAndSignTx).forEach(function (key) {
|
|
46
|
+
if (key === "default" || key === "__esModule") return;
|
|
47
|
+
Object.defineProperty(exports, key, {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () {
|
|
50
|
+
return _createAndSignTx[key];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parseUnsignedTx = parseUnsignedTx;
|
|
7
|
+
|
|
8
|
+
var _assets = _interopRequireDefault(require("@exodus/assets"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
13
|
+
|
|
14
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
15
|
+
|
|
16
|
+
function parseUnsignedTx(unsignedTx) {
|
|
17
|
+
const asset = _assets.default.solana;
|
|
18
|
+
|
|
19
|
+
const _unsignedTx$txData = unsignedTx.txData,
|
|
20
|
+
{
|
|
21
|
+
from,
|
|
22
|
+
to,
|
|
23
|
+
recentBlockhash,
|
|
24
|
+
tokenName,
|
|
25
|
+
destinationAddressType,
|
|
26
|
+
isAssociatedTokenAccountActive,
|
|
27
|
+
fromTokenAddresses,
|
|
28
|
+
method,
|
|
29
|
+
stakeAddresses,
|
|
30
|
+
seed,
|
|
31
|
+
pool
|
|
32
|
+
} = _unsignedTx$txData,
|
|
33
|
+
txData = _objectWithoutProperties(_unsignedTx$txData, ["from", "to", "recentBlockhash", "tokenName", "destinationAddressType", "isAssociatedTokenAccountActive", "fromTokenAddresses", "method", "stakeAddresses", "seed", "pool"]);
|
|
34
|
+
|
|
35
|
+
const amount = asset.currency.baseUnit(txData.amount);
|
|
36
|
+
const fee = asset.currency.baseUnit(txData.fee);
|
|
37
|
+
return {
|
|
38
|
+
asset,
|
|
39
|
+
from: [from],
|
|
40
|
+
to,
|
|
41
|
+
amount,
|
|
42
|
+
fee,
|
|
43
|
+
recentBlockhash,
|
|
44
|
+
// token related
|
|
45
|
+
tokenName,
|
|
46
|
+
destinationAddressType,
|
|
47
|
+
isAssociatedTokenAccountActive,
|
|
48
|
+
fromTokenAddresses,
|
|
49
|
+
// staking related
|
|
50
|
+
method,
|
|
51
|
+
stakeAddresses,
|
|
52
|
+
seed,
|
|
53
|
+
pool
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.signUnsignedTx = signUnsignedTx;
|
|
7
|
+
|
|
8
|
+
var _assets = _interopRequireDefault(require("@exodus/assets"));
|
|
9
|
+
|
|
10
|
+
var _ = require("../");
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
function signUnsignedTx(unsignedTx, privateKey) {
|
|
15
|
+
const {
|
|
16
|
+
from,
|
|
17
|
+
to,
|
|
18
|
+
amount: unitAmount,
|
|
19
|
+
recentBlockhash,
|
|
20
|
+
// tokens related
|
|
21
|
+
tokenName,
|
|
22
|
+
destinationAddressType,
|
|
23
|
+
isAssociatedTokenAccountActive,
|
|
24
|
+
fromTokenAddresses,
|
|
25
|
+
// staking related
|
|
26
|
+
stakeAddresses,
|
|
27
|
+
method,
|
|
28
|
+
seed,
|
|
29
|
+
pool
|
|
30
|
+
} = unsignedTx.txData;
|
|
31
|
+
const asset = _assets.default.solana;
|
|
32
|
+
const address = from;
|
|
33
|
+
const amount = unitAmount ? asset.currency.baseUnit(unitAmount).toNumber() : unitAmount;
|
|
34
|
+
let tx;
|
|
35
|
+
|
|
36
|
+
switch (method) {
|
|
37
|
+
case 'delegate':
|
|
38
|
+
tx = _.Transaction.createStakeAccountTransaction({
|
|
39
|
+
address,
|
|
40
|
+
amount,
|
|
41
|
+
recentBlockhash,
|
|
42
|
+
seed,
|
|
43
|
+
pool
|
|
44
|
+
});
|
|
45
|
+
break;
|
|
46
|
+
|
|
47
|
+
case 'undelegate':
|
|
48
|
+
tx = _.Transaction.undelegate({
|
|
49
|
+
address,
|
|
50
|
+
stakeAddresses,
|
|
51
|
+
recentBlockhash
|
|
52
|
+
});
|
|
53
|
+
break;
|
|
54
|
+
|
|
55
|
+
case 'withdraw':
|
|
56
|
+
tx = _.Transaction.withdraw({
|
|
57
|
+
address,
|
|
58
|
+
stakeAddresses,
|
|
59
|
+
amount,
|
|
60
|
+
recentBlockhash
|
|
61
|
+
});
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
default:
|
|
65
|
+
// SOL and Token tx
|
|
66
|
+
tx = new _.Transaction({
|
|
67
|
+
from,
|
|
68
|
+
to,
|
|
69
|
+
amount,
|
|
70
|
+
recentBlockhash,
|
|
71
|
+
tokenName,
|
|
72
|
+
destinationAddressType,
|
|
73
|
+
isAssociatedTokenAccountActive,
|
|
74
|
+
fromTokenAddresses
|
|
75
|
+
});
|
|
76
|
+
break;
|
|
77
|
+
} // sign plain tx
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
_.Transaction.sign(tx, privateKey);
|
|
81
|
+
|
|
82
|
+
const rawTx = _.Transaction.serialize(tx);
|
|
83
|
+
|
|
84
|
+
const txId = _.Transaction.getTxId(tx);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
txId,
|
|
88
|
+
rawTx
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Account = void 0;
|
|
7
|
+
|
|
8
|
+
var _publickey = require("./publickey");
|
|
9
|
+
|
|
10
|
+
var _keypair = require("../keypair");
|
|
11
|
+
|
|
12
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
|
+
|
|
14
|
+
class Account {
|
|
15
|
+
/**
|
|
16
|
+
* Create a new Account object
|
|
17
|
+
*
|
|
18
|
+
* @param secretKey Secret key for the account
|
|
19
|
+
*/
|
|
20
|
+
constructor(secretKey) {
|
|
21
|
+
_defineProperty(this, "_keypair", void 0);
|
|
22
|
+
|
|
23
|
+
if (secretKey) {
|
|
24
|
+
this._keypair = (0, _keypair.getKeyPairFromPrivateKey)(Buffer.from(secretKey, 'hex'));
|
|
25
|
+
} else {
|
|
26
|
+
throw new Error('Please provide a secretKey');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The public key for this account
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
get publicKey() {
|
|
35
|
+
return new _publickey.PublicKey(this._keypair.publicKey);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The **unencrypted** secret key for this account
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
get secretKey() {
|
|
43
|
+
return this._keypair.secretKey;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
exports.Account = Account;
|