@exodus/solana-lib 1.2.13 → 1.2.14
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 +88 -0
- package/lib/keypair.js +38 -0
- package/lib/tokens.js +21 -0
- package/lib/transaction.js +338 -0
- package/lib/tx/create-and-sign-tx.js +15 -0
- package/{src → lib}/tx/create-unsigned-tx.js +18 -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 -9
- package/src/keypair.js +0 -32
- package/src/tokens.js +0 -19
- package/src/transaction.js +0 -292
- 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 -41
- 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,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _account = require("./account");
|
|
8
|
+
|
|
9
|
+
Object.keys(_account).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
Object.defineProperty(exports, key, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () {
|
|
14
|
+
return _account[key];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
var _instruction = require("./instruction");
|
|
20
|
+
|
|
21
|
+
Object.keys(_instruction).forEach(function (key) {
|
|
22
|
+
if (key === "default" || key === "__esModule") return;
|
|
23
|
+
Object.defineProperty(exports, key, {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () {
|
|
26
|
+
return _instruction[key];
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
var _message = require("./message");
|
|
32
|
+
|
|
33
|
+
Object.keys(_message).forEach(function (key) {
|
|
34
|
+
if (key === "default" || key === "__esModule") return;
|
|
35
|
+
Object.defineProperty(exports, key, {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () {
|
|
38
|
+
return _message[key];
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
var _nonceAccount = require("./nonce-account");
|
|
44
|
+
|
|
45
|
+
Object.keys(_nonceAccount).forEach(function (key) {
|
|
46
|
+
if (key === "default" || key === "__esModule") return;
|
|
47
|
+
Object.defineProperty(exports, key, {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () {
|
|
50
|
+
return _nonceAccount[key];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
var _publickey = require("./publickey");
|
|
56
|
+
|
|
57
|
+
Object.keys(_publickey).forEach(function (key) {
|
|
58
|
+
if (key === "default" || key === "__esModule") return;
|
|
59
|
+
Object.defineProperty(exports, key, {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
get: function () {
|
|
62
|
+
return _publickey[key];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
var _systemProgram = require("./system-program");
|
|
68
|
+
|
|
69
|
+
Object.keys(_systemProgram).forEach(function (key) {
|
|
70
|
+
if (key === "default" || key === "__esModule") return;
|
|
71
|
+
Object.defineProperty(exports, key, {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
get: function () {
|
|
74
|
+
return _systemProgram[key];
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
var _stakeProgram = require("./stake-program");
|
|
80
|
+
|
|
81
|
+
Object.keys(_stakeProgram).forEach(function (key) {
|
|
82
|
+
if (key === "default" || key === "__esModule") return;
|
|
83
|
+
Object.defineProperty(exports, key, {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
get: function () {
|
|
86
|
+
return _stakeProgram[key];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
var _sysvar = require("./sysvar");
|
|
92
|
+
|
|
93
|
+
Object.keys(_sysvar).forEach(function (key) {
|
|
94
|
+
if (key === "default" || key === "__esModule") return;
|
|
95
|
+
Object.defineProperty(exports, key, {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
get: function () {
|
|
98
|
+
return _sysvar[key];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
var _transaction = require("./transaction");
|
|
104
|
+
|
|
105
|
+
Object.keys(_transaction).forEach(function (key) {
|
|
106
|
+
if (key === "default" || key === "__esModule") return;
|
|
107
|
+
Object.defineProperty(exports, key, {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
get: function () {
|
|
110
|
+
return _transaction[key];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.encodeData = encodeData;
|
|
7
|
+
exports.decodeData = decodeData;
|
|
8
|
+
|
|
9
|
+
var BufferLayout = _interopRequireWildcard(require("@exodus/buffer-layout"));
|
|
10
|
+
|
|
11
|
+
var Layout = _interopRequireWildcard(require("./utils/layout"));
|
|
12
|
+
|
|
13
|
+
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
14
|
+
|
|
15
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Populate a buffer of instruction data using an InstructionType
|
|
19
|
+
*/
|
|
20
|
+
function encodeData(type, fields) {
|
|
21
|
+
const allocLength = type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
|
|
22
|
+
const data = Buffer.alloc(allocLength);
|
|
23
|
+
const layoutFields = Object.assign({
|
|
24
|
+
instruction: type.index
|
|
25
|
+
}, fields);
|
|
26
|
+
type.layout.encode(layoutFields, data);
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decode instruction data buffer using an InstructionType
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
function decodeData(type, buffer) {
|
|
35
|
+
let data;
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
data = type.layout.decode(buffer);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
throw new Error('invalid instruction; ' + err);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (data.instruction !== type.index) {
|
|
44
|
+
throw new Error(`invalid instruction; instruction index mismatch ${data.instruction} != ${type.index}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Message = void 0;
|
|
7
|
+
|
|
8
|
+
var _bs = _interopRequireDefault(require("bs58"));
|
|
9
|
+
|
|
10
|
+
var BufferLayout = _interopRequireWildcard(require("@exodus/buffer-layout"));
|
|
11
|
+
|
|
12
|
+
var _publickey = require("./publickey");
|
|
13
|
+
|
|
14
|
+
var Layout = _interopRequireWildcard(require("./utils/layout"));
|
|
15
|
+
|
|
16
|
+
var _transaction = require("./transaction");
|
|
17
|
+
|
|
18
|
+
var shortvec = _interopRequireWildcard(require("./utils/shortvec-encoding"));
|
|
19
|
+
|
|
20
|
+
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
21
|
+
|
|
22
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
|
+
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
28
|
+
const PUBKEY_LENGTH = 32;
|
|
29
|
+
/**
|
|
30
|
+
* List of instructions to be processed atomically
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
class Message {
|
|
34
|
+
constructor(args) {
|
|
35
|
+
_defineProperty(this, "header", void 0);
|
|
36
|
+
|
|
37
|
+
_defineProperty(this, "accountKeys", void 0);
|
|
38
|
+
|
|
39
|
+
_defineProperty(this, "recentBlockhash", void 0);
|
|
40
|
+
|
|
41
|
+
_defineProperty(this, "instructions", void 0);
|
|
42
|
+
|
|
43
|
+
this.header = args.header;
|
|
44
|
+
this.accountKeys = args.accountKeys.map(account => new _publickey.PublicKey(account));
|
|
45
|
+
this.recentBlockhash = args.recentBlockhash;
|
|
46
|
+
this.instructions = args.instructions;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
isAccountWritable(index) {
|
|
50
|
+
return index < this.header.numRequiredSignatures - this.header.numReadonlySignedAccounts || index >= this.header.numRequiredSignatures && index < this.accountKeys.length - this.header.numReadonlyUnsignedAccounts;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
findSignerIndex(signer) {
|
|
54
|
+
const index = this.accountKeys.findIndex(accountKey => {
|
|
55
|
+
return accountKey.equals(signer);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (index < 0) {
|
|
59
|
+
throw new Error(`unknown signer: ${signer.toString()}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return index;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
serialize() {
|
|
66
|
+
const numKeys = this.accountKeys.length;
|
|
67
|
+
let keyCount = [];
|
|
68
|
+
shortvec.encodeLength(keyCount, numKeys);
|
|
69
|
+
const instructions = this.instructions.map(instruction => {
|
|
70
|
+
const {
|
|
71
|
+
accounts,
|
|
72
|
+
programIdIndex
|
|
73
|
+
} = instruction;
|
|
74
|
+
|
|
75
|
+
const data = _bs.default.decode(instruction.data);
|
|
76
|
+
|
|
77
|
+
let keyIndicesCount = [];
|
|
78
|
+
shortvec.encodeLength(keyIndicesCount, accounts.length);
|
|
79
|
+
let dataCount = [];
|
|
80
|
+
shortvec.encodeLength(dataCount, data.length);
|
|
81
|
+
return {
|
|
82
|
+
programIdIndex,
|
|
83
|
+
keyIndicesCount: Buffer.from(keyIndicesCount),
|
|
84
|
+
keyIndices: Buffer.from(accounts),
|
|
85
|
+
dataLength: Buffer.from(dataCount),
|
|
86
|
+
data
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
let instructionCount = [];
|
|
90
|
+
shortvec.encodeLength(instructionCount, instructions.length);
|
|
91
|
+
let instructionBuffer = Buffer.alloc(_transaction.PACKET_DATA_SIZE);
|
|
92
|
+
Buffer.from(instructionCount).copy(instructionBuffer);
|
|
93
|
+
let instructionBufferLength = instructionCount.length;
|
|
94
|
+
instructions.forEach(instruction => {
|
|
95
|
+
const instructionLayout = BufferLayout.struct([BufferLayout.u8('programIdIndex'), BufferLayout.blob(instruction.keyIndicesCount.length, 'keyIndicesCount'), BufferLayout.seq(BufferLayout.u8('keyIndex'), instruction.keyIndices.length, 'keyIndices'), BufferLayout.blob(instruction.dataLength.length, 'dataLength'), BufferLayout.seq(BufferLayout.u8('userdatum'), instruction.data.length, 'data')]);
|
|
96
|
+
const length = instructionLayout.encode(instruction, instructionBuffer, instructionBufferLength);
|
|
97
|
+
instructionBufferLength += length;
|
|
98
|
+
});
|
|
99
|
+
instructionBuffer = instructionBuffer.slice(0, instructionBufferLength);
|
|
100
|
+
const signDataLayout = BufferLayout.struct([BufferLayout.blob(1, 'numRequiredSignatures'), BufferLayout.blob(1, 'numReadonlySignedAccounts'), BufferLayout.blob(1, 'numReadonlyUnsignedAccounts'), BufferLayout.blob(keyCount.length, 'keyCount'), BufferLayout.seq(Layout.publicKey('key'), numKeys, 'keys'), Layout.publicKey('recentBlockhash')]);
|
|
101
|
+
const transaction = {
|
|
102
|
+
numRequiredSignatures: Buffer.from([this.header.numRequiredSignatures]),
|
|
103
|
+
numReadonlySignedAccounts: Buffer.from([this.header.numReadonlySignedAccounts]),
|
|
104
|
+
numReadonlyUnsignedAccounts: Buffer.from([this.header.numReadonlyUnsignedAccounts]),
|
|
105
|
+
keyCount: Buffer.from(keyCount),
|
|
106
|
+
keys: this.accountKeys.map(key => key.toBuffer()),
|
|
107
|
+
recentBlockhash: _bs.default.decode(this.recentBlockhash)
|
|
108
|
+
};
|
|
109
|
+
let signData = Buffer.alloc(2048);
|
|
110
|
+
const length = signDataLayout.encode(transaction, signData);
|
|
111
|
+
instructionBuffer.copy(signData, length);
|
|
112
|
+
return signData.slice(0, length + instructionBuffer.length);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Decode a compiled message into a Message object.
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
static from(buffer) {
|
|
120
|
+
// Slice up wire data
|
|
121
|
+
let byteArray = [...buffer];
|
|
122
|
+
const numRequiredSignatures = byteArray.shift();
|
|
123
|
+
const numReadonlySignedAccounts = byteArray.shift();
|
|
124
|
+
const numReadonlyUnsignedAccounts = byteArray.shift();
|
|
125
|
+
const accountCount = shortvec.decodeLength(byteArray);
|
|
126
|
+
let accountKeys = [];
|
|
127
|
+
|
|
128
|
+
for (let i = 0; i < accountCount; i++) {
|
|
129
|
+
const account = byteArray.slice(0, PUBKEY_LENGTH);
|
|
130
|
+
byteArray = byteArray.slice(PUBKEY_LENGTH);
|
|
131
|
+
accountKeys.push(_bs.default.encode(Buffer.from(account)));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
|
|
135
|
+
byteArray = byteArray.slice(PUBKEY_LENGTH);
|
|
136
|
+
const instructionCount = shortvec.decodeLength(byteArray);
|
|
137
|
+
let instructions = [];
|
|
138
|
+
|
|
139
|
+
for (let i = 0; i < instructionCount; i++) {
|
|
140
|
+
let instruction = {};
|
|
141
|
+
instruction.programIdIndex = byteArray.shift();
|
|
142
|
+
const accountCount = shortvec.decodeLength(byteArray);
|
|
143
|
+
instruction.accounts = byteArray.slice(0, accountCount);
|
|
144
|
+
byteArray = byteArray.slice(accountCount);
|
|
145
|
+
const dataLength = shortvec.decodeLength(byteArray);
|
|
146
|
+
const data = byteArray.slice(0, dataLength);
|
|
147
|
+
instruction.data = _bs.default.encode(Buffer.from(data));
|
|
148
|
+
byteArray = byteArray.slice(dataLength);
|
|
149
|
+
instructions.push(instruction);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const messageArgs = {
|
|
153
|
+
header: {
|
|
154
|
+
numRequiredSignatures,
|
|
155
|
+
numReadonlySignedAccounts,
|
|
156
|
+
numReadonlyUnsignedAccounts
|
|
157
|
+
},
|
|
158
|
+
recentBlockhash: _bs.default.encode(Buffer.from(recentBlockhash)),
|
|
159
|
+
accountKeys,
|
|
160
|
+
instructions
|
|
161
|
+
};
|
|
162
|
+
return new Message(messageArgs);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
exports.Message = Message;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.NonceAccount = exports.NONCE_ACCOUNT_LENGTH = void 0;
|
|
7
|
+
|
|
8
|
+
var BufferLayout = _interopRequireWildcard(require("@exodus/buffer-layout"));
|
|
9
|
+
|
|
10
|
+
var _publickey = require("./publickey");
|
|
11
|
+
|
|
12
|
+
var Layout = _interopRequireWildcard(require("./utils/layout"));
|
|
13
|
+
|
|
14
|
+
var _feeCalculator = require("./utils/fee-calculator");
|
|
15
|
+
|
|
16
|
+
var _toBuffer = require("./utils/to-buffer");
|
|
17
|
+
|
|
18
|
+
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
19
|
+
|
|
20
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
const NonceAccountLayout = BufferLayout.struct([BufferLayout.u32('version'), BufferLayout.u32('state'), Layout.publicKey('authorizedPubkey'), Layout.publicKey('nonce'), BufferLayout.struct([_feeCalculator.FeeCalculatorLayout], 'feeCalculator')]);
|
|
25
|
+
const NONCE_ACCOUNT_LENGTH = NonceAccountLayout.span;
|
|
26
|
+
/**
|
|
27
|
+
* NonceAccount class
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
exports.NONCE_ACCOUNT_LENGTH = NONCE_ACCOUNT_LENGTH;
|
|
31
|
+
|
|
32
|
+
class NonceAccount {
|
|
33
|
+
constructor() {
|
|
34
|
+
_defineProperty(this, "authorizedPubkey", void 0);
|
|
35
|
+
|
|
36
|
+
_defineProperty(this, "nonce", void 0);
|
|
37
|
+
|
|
38
|
+
_defineProperty(this, "feeCalculator", void 0);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Deserialize NonceAccount from the account data.
|
|
43
|
+
*
|
|
44
|
+
* @param buffer account data
|
|
45
|
+
* @return NonceAccount
|
|
46
|
+
*/
|
|
47
|
+
static fromAccountData(buffer) {
|
|
48
|
+
const nonceAccount = NonceAccountLayout.decode((0, _toBuffer.toBuffer)(buffer), 0);
|
|
49
|
+
nonceAccount.authorizedPubkey = new _publickey.PublicKey(nonceAccount.authorizedPubkey);
|
|
50
|
+
nonceAccount.nonce = new _publickey.PublicKey(nonceAccount.nonce).toString();
|
|
51
|
+
return nonceAccount;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.NonceAccount = NonceAccount;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.PublicKey = exports.MAX_SEED_LENGTH = void 0;
|
|
7
|
+
|
|
8
|
+
var _bn = _interopRequireDefault(require("bn.js"));
|
|
9
|
+
|
|
10
|
+
var _bs = _interopRequireDefault(require("bs58"));
|
|
11
|
+
|
|
12
|
+
var _tweetnacl = _interopRequireDefault(require("tweetnacl"));
|
|
13
|
+
|
|
14
|
+
var _createHash = _interopRequireDefault(require("create-hash"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
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; }
|
|
19
|
+
|
|
20
|
+
// $FlowFixMe
|
|
21
|
+
let naclLowLevel = _tweetnacl.default.lowlevel;
|
|
22
|
+
/**
|
|
23
|
+
* Maximum length of derived pubkey seed
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const MAX_SEED_LENGTH = 32;
|
|
27
|
+
/**
|
|
28
|
+
* A public key
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
exports.MAX_SEED_LENGTH = MAX_SEED_LENGTH;
|
|
32
|
+
|
|
33
|
+
class PublicKey {
|
|
34
|
+
/**
|
|
35
|
+
* Create a new PublicKey object
|
|
36
|
+
*/
|
|
37
|
+
constructor(value) {
|
|
38
|
+
_defineProperty(this, "_bn", void 0);
|
|
39
|
+
|
|
40
|
+
if (typeof value === 'string') {
|
|
41
|
+
// assume base 58 encoding by default
|
|
42
|
+
const decoded = _bs.default.decode(value);
|
|
43
|
+
|
|
44
|
+
if (decoded.length !== 32) {
|
|
45
|
+
throw new Error(`Invalid public key input`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this._bn = new _bn.default(decoded);
|
|
49
|
+
} else {
|
|
50
|
+
this._bn = new _bn.default(value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (this._bn.byteLength() > 32) {
|
|
54
|
+
throw new Error(`Invalid public key input`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Checks if two publicKeys are equal
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
equals(publicKey) {
|
|
63
|
+
return this._bn.eq(publicKey._bn);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Return the base-58 representation of the public key
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
toBase58() {
|
|
71
|
+
return _bs.default.encode(this.toBuffer());
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Return the Buffer representation of the public key
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
toBuffer() {
|
|
79
|
+
const b = this._bn.toArrayLike(Buffer);
|
|
80
|
+
|
|
81
|
+
if (b.length === 32) {
|
|
82
|
+
return b;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const zeroPad = Buffer.alloc(32);
|
|
86
|
+
b.copy(zeroPad, 32 - b.length);
|
|
87
|
+
return zeroPad;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns a string representation of the public key
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
toString() {
|
|
95
|
+
return this.toBase58();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Find a valid program address
|
|
99
|
+
*
|
|
100
|
+
* Valid program addresses must fall off the ed25519 curve. This function
|
|
101
|
+
* iterates a nonce until it finds one that when combined with the seeds
|
|
102
|
+
* results in a valid program address.
|
|
103
|
+
* HACK: transformed in sync function
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
static findProgramAddress(seeds, programId) {
|
|
108
|
+
let nonce = 255;
|
|
109
|
+
let address;
|
|
110
|
+
|
|
111
|
+
while (nonce !== 0) {
|
|
112
|
+
try {
|
|
113
|
+
const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
|
|
114
|
+
address = this.createProgramAddress(seedsWithNonce, programId);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
nonce--;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return [address, nonce];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
throw new Error('Unable to find a viable program address nonce');
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Derive a public key from another key, a seed, and a program ID.
|
|
127
|
+
* HACK: transformed in sync function
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
static createWithSeed(fromPublicKey, seed, programId) {
|
|
132
|
+
const buffer = Buffer.concat([fromPublicKey.toBuffer(), Buffer.from(seed), programId.toBuffer()]);
|
|
133
|
+
const hash = (0, _createHash.default)('sha256').update(buffer).digest('hex');
|
|
134
|
+
return new PublicKey(Buffer.from(hash, 'hex'));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Derive a program address from seeds and a program ID.
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
static createProgramAddress(seeds, programId) {
|
|
142
|
+
let buffer = Buffer.alloc(0);
|
|
143
|
+
seeds.forEach(function (seed) {
|
|
144
|
+
if (seed.length > MAX_SEED_LENGTH) {
|
|
145
|
+
throw new Error('Max seed length exceeded');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
buffer = Buffer.concat([buffer, Buffer.from(seed)]);
|
|
149
|
+
});
|
|
150
|
+
buffer = Buffer.concat([buffer, programId.toBuffer(), Buffer.from('ProgramDerivedAddress')]);
|
|
151
|
+
let hash = (0, _createHash.default)('sha256').update(buffer).digest('hex');
|
|
152
|
+
let publicKeyBytes = new _bn.default(hash, 16).toArray(null, 32);
|
|
153
|
+
|
|
154
|
+
if (isOnCurve(publicKeyBytes)) {
|
|
155
|
+
throw new Error('Invalid seeds, address must fall off the curve');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return new PublicKey(publicKeyBytes);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
} // Check that a pubkey is on the curve.
|
|
162
|
+
// This function and its dependents were sourced from:
|
|
163
|
+
// https://github.com/dchest/tweetnacl-js/blob/f1ec050ceae0861f34280e62498b1d3ed9c350c6/nacl.js#L792
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
exports.PublicKey = PublicKey;
|
|
167
|
+
|
|
168
|
+
function isOnCurve(p) {
|
|
169
|
+
let r = [naclLowLevel.gf(), naclLowLevel.gf(), naclLowLevel.gf(), naclLowLevel.gf()];
|
|
170
|
+
let t = naclLowLevel.gf();
|
|
171
|
+
let chk = naclLowLevel.gf();
|
|
172
|
+
let num = naclLowLevel.gf();
|
|
173
|
+
let den = naclLowLevel.gf();
|
|
174
|
+
let den2 = naclLowLevel.gf();
|
|
175
|
+
let den4 = naclLowLevel.gf();
|
|
176
|
+
let den6 = naclLowLevel.gf();
|
|
177
|
+
naclLowLevel.set25519(r[2], gf1);
|
|
178
|
+
naclLowLevel.unpack25519(r[1], p);
|
|
179
|
+
naclLowLevel.S(num, r[1]);
|
|
180
|
+
naclLowLevel.M(den, num, naclLowLevel.D);
|
|
181
|
+
naclLowLevel.Z(num, num, r[2]);
|
|
182
|
+
naclLowLevel.A(den, r[2], den);
|
|
183
|
+
naclLowLevel.S(den2, den);
|
|
184
|
+
naclLowLevel.S(den4, den2);
|
|
185
|
+
naclLowLevel.M(den6, den4, den2);
|
|
186
|
+
naclLowLevel.M(t, den6, num);
|
|
187
|
+
naclLowLevel.M(t, t, den);
|
|
188
|
+
naclLowLevel.pow2523(t, t);
|
|
189
|
+
naclLowLevel.M(t, t, num);
|
|
190
|
+
naclLowLevel.M(t, t, den);
|
|
191
|
+
naclLowLevel.M(t, t, den);
|
|
192
|
+
naclLowLevel.M(r[0], t, den);
|
|
193
|
+
naclLowLevel.S(chk, r[0]);
|
|
194
|
+
naclLowLevel.M(chk, chk, den);
|
|
195
|
+
if (neq25519(chk, num)) naclLowLevel.M(r[0], r[0], I);
|
|
196
|
+
naclLowLevel.S(chk, r[0]);
|
|
197
|
+
naclLowLevel.M(chk, chk, den);
|
|
198
|
+
if (neq25519(chk, num)) return 0;
|
|
199
|
+
return 1;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let gf1 = naclLowLevel.gf([1]);
|
|
203
|
+
let I = naclLowLevel.gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
|
|
204
|
+
|
|
205
|
+
function neq25519(a, b) {
|
|
206
|
+
var c = new Uint8Array(32);
|
|
207
|
+
var d = new Uint8Array(32);
|
|
208
|
+
naclLowLevel.pack25519(c, a);
|
|
209
|
+
naclLowLevel.pack25519(d, b);
|
|
210
|
+
return naclLowLevel.crypto_verify_32(c, 0, d, 0);
|
|
211
|
+
}
|