@alephium/web3 0.41.0 → 0.43.0
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/{utils → address}/address.js +15 -19
- package/dist/src/address/index.d.ts +1 -0
- package/dist/src/address/index.js +34 -0
- package/dist/src/api/node-provider.js +10 -9
- package/dist/src/api/types.js +1 -1
- package/dist/src/codec/array-codec.d.ts +2 -3
- package/dist/src/codec/array-codec.js +1 -2
- package/dist/src/codec/asset-output-codec.d.ts +3 -4
- package/dist/src/codec/asset-output-codec.js +10 -12
- package/dist/src/codec/bigint-codec.d.ts +2 -3
- package/dist/src/codec/bigint-codec.js +2 -3
- package/dist/src/codec/bytestring-codec.d.ts +5 -6
- package/dist/src/codec/bytestring-codec.js +5 -5
- package/dist/src/codec/codec.d.ts +2 -3
- package/dist/src/codec/compact-int-codec.d.ts +12 -13
- package/dist/src/codec/compact-int-codec.js +22 -22
- package/dist/src/codec/contract-codec.d.ts +5 -6
- package/dist/src/codec/contract-codec.js +24 -24
- package/dist/src/codec/contract-output-codec.d.ts +2 -3
- package/dist/src/codec/contract-output-codec.js +8 -10
- package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
- package/dist/src/codec/contract-output-ref-codec.js +2 -2
- package/dist/src/codec/either-codec.d.ts +2 -3
- package/dist/src/codec/either-codec.js +1 -2
- package/dist/src/codec/hash.d.ts +2 -3
- package/dist/src/codec/hash.js +17 -0
- package/dist/src/codec/input-codec.d.ts +3 -4
- package/dist/src/codec/input-codec.js +6 -6
- package/dist/src/codec/instr-codec.d.ts +2 -3
- package/dist/src/codec/instr-codec.js +1 -2
- package/dist/src/codec/lockup-script-codec.d.ts +5 -6
- package/dist/src/codec/lockup-script-codec.js +1 -2
- package/dist/src/codec/long-codec.d.ts +2 -3
- package/dist/src/codec/long-codec.js +1 -2
- package/dist/src/codec/method-codec.d.ts +2 -3
- package/dist/src/codec/method-codec.js +1 -2
- package/dist/src/codec/option-codec.d.ts +3 -4
- package/dist/src/codec/option-codec.js +2 -3
- package/dist/src/codec/script-codec.d.ts +4 -5
- package/dist/src/codec/script-codec.js +1 -3
- package/dist/src/codec/signature-codec.d.ts +3 -4
- package/dist/src/codec/signature-codec.js +17 -0
- package/dist/src/codec/signed-int-codec.d.ts +2 -3
- package/dist/src/codec/signed-int-codec.js +1 -2
- package/dist/src/codec/token-codec.d.ts +3 -4
- package/dist/src/codec/token-codec.js +3 -3
- package/dist/src/codec/transaction-codec.d.ts +4 -5
- package/dist/src/codec/transaction-codec.js +12 -13
- package/dist/src/codec/unlock-script-codec.d.ts +5 -6
- package/dist/src/codec/unlock-script-codec.js +14 -15
- package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
- package/dist/src/codec/unsigned-tx-codec.js +4 -5
- package/dist/src/contract/contract.d.ts +8 -95
- package/dist/src/contract/contract.js +31 -564
- package/dist/src/contract/ralph.d.ts +1 -1
- package/dist/src/contract/ralph.js +9 -10
- package/dist/src/{utils → exchange}/exchange.js +5 -5
- package/dist/src/exchange/index.d.ts +1 -0
- package/dist/src/exchange/index.js +25 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/signer/signer.js +4 -5
- package/dist/src/signer/tx-builder.js +3 -3
- package/dist/src/transaction/index.d.ts +1 -0
- package/dist/src/transaction/index.js +1 -0
- package/dist/src/transaction/utils.d.ts +2 -0
- package/dist/src/transaction/utils.js +34 -0
- package/dist/src/utils/index.d.ts +0 -2
- package/dist/src/utils/index.js +0 -7
- package/dist/src/utils/sign.js +1 -1
- package/dist/src/utils/utils.d.ts +1 -0
- package/dist/src/utils/utils.js +30 -11
- package/dist/src/utils/webcrypto.js +3 -4
- package/package.json +1 -2
- package/src/{utils → address}/address.ts +15 -19
- package/src/address/index.ts +19 -0
- package/src/api/node-provider.ts +2 -9
- package/src/api/types.ts +2 -2
- package/src/codec/array-codec.ts +3 -4
- package/src/codec/asset-output-codec.ts +16 -18
- package/src/codec/bigint-codec.ts +4 -5
- package/src/codec/bytestring-codec.ts +8 -8
- package/src/codec/codec.ts +2 -3
- package/src/codec/compact-int-codec.ts +36 -37
- package/src/codec/contract-codec.ts +28 -27
- package/src/codec/contract-output-codec.ts +11 -13
- package/src/codec/contract-output-ref-codec.ts +5 -5
- package/src/codec/either-codec.ts +3 -4
- package/src/codec/hash.ts +2 -3
- package/src/codec/input-codec.ts +10 -10
- package/src/codec/instr-codec.ts +3 -4
- package/src/codec/lockup-script-codec.ts +8 -9
- package/src/codec/long-codec.ts +3 -4
- package/src/codec/method-codec.ts +3 -4
- package/src/codec/option-codec.ts +4 -5
- package/src/codec/script-codec.ts +5 -7
- package/src/codec/signature-codec.ts +3 -4
- package/src/codec/signed-int-codec.ts +3 -4
- package/src/codec/token-codec.ts +6 -6
- package/src/codec/transaction-codec.ts +17 -18
- package/src/codec/unlock-script-codec.ts +26 -27
- package/src/codec/unsigned-tx-codec.ts +10 -11
- package/src/contract/contract.ts +18 -790
- package/src/contract/ralph.ts +10 -11
- package/src/{utils → exchange}/exchange.ts +3 -11
- package/src/exchange/index.ts +19 -0
- package/src/index.ts +2 -0
- package/src/signer/signer.ts +2 -3
- package/src/signer/tx-builder.ts +2 -2
- package/src/transaction/index.ts +1 -0
- package/src/transaction/utils.ts +38 -0
- package/src/utils/index.ts +0 -2
- package/src/utils/sign.ts +1 -1
- package/src/utils/utils.ts +29 -10
- package/src/utils/webcrypto.ts +3 -4
- package/webpack.config.js +1 -6
- package/dist/src/utils/error.d.ts +0 -15
- package/dist/src/utils/error.js +0 -66
- package/src/utils/error.ts +0 -77
- /package/dist/src/{utils → address}/address.d.ts +0 -0
- /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
|
@@ -25,11 +25,10 @@ const elliptic_1 = require("elliptic");
|
|
|
25
25
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
26
26
|
const constants_1 = require("../constants");
|
|
27
27
|
const blakejs_1 = __importDefault(require("blakejs"));
|
|
28
|
-
const bs58_1 = __importDefault(require("
|
|
29
|
-
const djb2_1 = __importDefault(require("
|
|
30
|
-
const utils_1 = require("
|
|
28
|
+
const bs58_1 = __importDefault(require("../utils/bs58"));
|
|
29
|
+
const djb2_1 = __importDefault(require("../utils/djb2"));
|
|
30
|
+
const utils_1 = require("../utils");
|
|
31
31
|
const lockup_script_codec_1 = require("../codec/lockup-script-codec");
|
|
32
|
-
const buffer_1 = require("buffer/");
|
|
33
32
|
const codec_1 = require("../codec");
|
|
34
33
|
const ec = new elliptic_1.ec('secp256k1');
|
|
35
34
|
var AddressType;
|
|
@@ -57,7 +56,7 @@ function decodeAndValidateAddress(address) {
|
|
|
57
56
|
if (addressType === AddressType.P2MPKH) {
|
|
58
57
|
let multisig;
|
|
59
58
|
try {
|
|
60
|
-
multisig = lockup_script_codec_1.lockupScriptCodec.decode(
|
|
59
|
+
multisig = lockup_script_codec_1.lockupScriptCodec.decode(decoded).script;
|
|
61
60
|
}
|
|
62
61
|
catch (_) {
|
|
63
62
|
throw new Error(`Invalid multisig address: ${address}`);
|
|
@@ -163,27 +162,24 @@ exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
|
|
|
163
162
|
function addressFromPublicKey(publicKey, _keyType) {
|
|
164
163
|
const keyType = _keyType ?? 'default';
|
|
165
164
|
if (keyType === 'default') {
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
const bytes = buffer_1.Buffer.concat([addressType, hash]);
|
|
165
|
+
const hash = blakejs_1.default.blake2b((0, utils_1.hexToBinUnsafe)(publicKey), undefined, 32);
|
|
166
|
+
const bytes = new Uint8Array([AddressType.P2PKH, ...hash]);
|
|
169
167
|
return bs58_1.default.encode(bytes);
|
|
170
168
|
}
|
|
171
169
|
else {
|
|
172
|
-
const lockupScript =
|
|
170
|
+
const lockupScript = (0, utils_1.hexToBinUnsafe)(`0101000000000458144020${publicKey}8685`);
|
|
173
171
|
return addressFromScript(lockupScript);
|
|
174
172
|
}
|
|
175
173
|
}
|
|
176
174
|
exports.addressFromPublicKey = addressFromPublicKey;
|
|
177
175
|
function addressFromScript(script) {
|
|
178
176
|
const scriptHash = blakejs_1.default.blake2b(script, undefined, 32);
|
|
179
|
-
|
|
180
|
-
return bs58_1.default.encode(buffer_1.Buffer.concat([addressType, scriptHash]));
|
|
177
|
+
return bs58_1.default.encode(new Uint8Array([AddressType.P2SH, ...scriptHash]));
|
|
181
178
|
}
|
|
182
179
|
exports.addressFromScript = addressFromScript;
|
|
183
180
|
function addressFromContractId(contractId) {
|
|
184
|
-
const
|
|
185
|
-
const
|
|
186
|
-
const bytes = buffer_1.Buffer.concat([addressType, hash]);
|
|
181
|
+
const hash = (0, utils_1.hexToBinUnsafe)(contractId);
|
|
182
|
+
const bytes = new Uint8Array([AddressType.P2C, ...hash]);
|
|
187
183
|
return bs58_1.default.encode(bytes);
|
|
188
184
|
}
|
|
189
185
|
exports.addressFromContractId = addressFromContractId;
|
|
@@ -194,7 +190,7 @@ function addressFromTokenId(tokenId) {
|
|
|
194
190
|
exports.addressFromTokenId = addressFromTokenId;
|
|
195
191
|
function contractIdFromTx(txId, outputIndex) {
|
|
196
192
|
const txIdBin = (0, utils_1.hexToBinUnsafe)(txId);
|
|
197
|
-
const data =
|
|
193
|
+
const data = new Uint8Array([...txIdBin, outputIndex]);
|
|
198
194
|
const hash = blakejs_1.default.blake2b(data, undefined, 32);
|
|
199
195
|
return (0, utils_1.binToHex)(hash);
|
|
200
196
|
}
|
|
@@ -203,10 +199,10 @@ function subContractId(parentContractId, pathInHex, group) {
|
|
|
203
199
|
if (group < 0 || group >= constants_1.TOTAL_NUMBER_OF_GROUPS) {
|
|
204
200
|
throw new Error(`Invalid group ${group}`);
|
|
205
201
|
}
|
|
206
|
-
const data =
|
|
207
|
-
const bytes =
|
|
208
|
-
blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
|
|
209
|
-
|
|
202
|
+
const data = (0, utils_1.concatBytes)([(0, utils_1.hexToBinUnsafe)(parentContractId), (0, utils_1.hexToBinUnsafe)(pathInHex)]);
|
|
203
|
+
const bytes = new Uint8Array([
|
|
204
|
+
...blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
|
|
205
|
+
group
|
|
210
206
|
]);
|
|
211
207
|
return (0, utils_1.binToHex)(bytes);
|
|
212
208
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './address';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
4
|
+
This file is part of the alephium project.
|
|
5
|
+
|
|
6
|
+
The library is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
The library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
20
|
+
if (k2 === undefined) k2 = k;
|
|
21
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
22
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
23
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(o, k2, desc);
|
|
26
|
+
}) : (function(o, m, k, k2) {
|
|
27
|
+
if (k2 === undefined) k2 = k;
|
|
28
|
+
o[k2] = m[k];
|
|
29
|
+
}));
|
|
30
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
31
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
__exportStar(require("./address"), exports);
|
|
@@ -21,6 +21,7 @@ exports.tryGetCallResult = exports.NodeProvider = void 0;
|
|
|
21
21
|
const types_1 = require("./types");
|
|
22
22
|
const api_alephium_1 = require("./api-alephium");
|
|
23
23
|
const utils_1 = require("../utils");
|
|
24
|
+
const address_1 = require("../address");
|
|
24
25
|
function initializeNodeApi(baseUrl, apiKey, customFetch) {
|
|
25
26
|
const nodeApi = new api_alephium_1.Api({
|
|
26
27
|
baseUrl: baseUrl,
|
|
@@ -38,8 +39,8 @@ class NodeProvider {
|
|
|
38
39
|
};
|
|
39
40
|
// Only use this when the token follows the fungible token interface, check `guessTokenType` first
|
|
40
41
|
this.fetchFungibleTokenMetaData = async (tokenId) => {
|
|
41
|
-
const address = (0,
|
|
42
|
-
const group = (0,
|
|
42
|
+
const address = (0, address_1.addressFromTokenId)(tokenId);
|
|
43
|
+
const group = (0, address_1.groupOfAddress)(address);
|
|
43
44
|
const calls = Array.from([0, 1, 2, 3], (index) => ({ methodIndex: index, group: group, address: address }));
|
|
44
45
|
const result = await this.contracts.postContractsMulticallContract({
|
|
45
46
|
calls: calls
|
|
@@ -54,8 +55,8 @@ class NodeProvider {
|
|
|
54
55
|
};
|
|
55
56
|
// Only use this when the token follows the non-fungile token interface, check `guessTokenType` first
|
|
56
57
|
this.fetchNFTMetaData = async (tokenId) => {
|
|
57
|
-
const address = (0,
|
|
58
|
-
const group = (0,
|
|
58
|
+
const address = (0, address_1.addressFromTokenId)(tokenId);
|
|
59
|
+
const group = (0, address_1.groupOfAddress)(address);
|
|
59
60
|
const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
|
|
60
61
|
const result = await this.contracts.postContractsMulticallContract({
|
|
61
62
|
calls: calls
|
|
@@ -99,8 +100,8 @@ class NodeProvider {
|
|
|
99
100
|
};
|
|
100
101
|
// Only use this when the contract follows the NFT collection interface, check `guessFollowsNFTCollectionStd` first
|
|
101
102
|
this.fetchNFTCollectionMetaData = async (collectionId) => {
|
|
102
|
-
const address = (0,
|
|
103
|
-
const group = (0,
|
|
103
|
+
const address = (0, address_1.addressFromContractId)(collectionId);
|
|
104
|
+
const group = (0, address_1.groupOfAddress)(address);
|
|
104
105
|
const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
|
|
105
106
|
const result = await this.contracts.postContractsMulticallContract({ calls });
|
|
106
107
|
const callResults = result.results.map((r) => tryGetCallResult(r));
|
|
@@ -111,8 +112,8 @@ class NodeProvider {
|
|
|
111
112
|
};
|
|
112
113
|
// Only use this when the contract follows the NFT collection with royalty interface, check `guessFollowsNFTCollectionWithRoyaltyStd` first
|
|
113
114
|
this.fetchNFTRoyaltyAmount = async (collectionId, tokenId, salePrice) => {
|
|
114
|
-
const address = (0,
|
|
115
|
-
const group = (0,
|
|
115
|
+
const address = (0, address_1.addressFromContractId)(collectionId);
|
|
116
|
+
const group = (0, address_1.groupOfAddress)(address);
|
|
116
117
|
const apiResult = await this.contracts.postContractsCallContract({
|
|
117
118
|
address: address,
|
|
118
119
|
group: group,
|
|
@@ -132,7 +133,7 @@ class NodeProvider {
|
|
|
132
133
|
return BigInt(result.returns[0].value);
|
|
133
134
|
};
|
|
134
135
|
this.guessStdInterfaceId = async (tokenId) => {
|
|
135
|
-
const address = (0,
|
|
136
|
+
const address = (0, address_1.addressFromTokenId)(tokenId);
|
|
136
137
|
const rawState = await this.contracts.getContractsAddressState(address);
|
|
137
138
|
const lastImmField = rawState.immFields.slice(-1).pop()?.value;
|
|
138
139
|
const interfaceIdPrefix = '414c5048'; // the hex of 'ALPH'
|
package/dist/src/api/types.js
CHANGED
|
@@ -85,7 +85,7 @@ function toApiByteVec(v) {
|
|
|
85
85
|
try {
|
|
86
86
|
const address = utils_1.bs58.decode(v);
|
|
87
87
|
if (address.length == 33 && address[0] == 3) {
|
|
88
|
-
return
|
|
88
|
+
return (0, utils_1.binToHex)(address.slice(1));
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
catch (_) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
import { Parser } from 'binary-parser';
|
|
3
2
|
import { DecodedCompactInt } from './compact-int-codec';
|
|
4
3
|
import { Codec } from './codec';
|
|
@@ -10,8 +9,8 @@ export declare class ArrayCodec<T> implements Codec<T[]> {
|
|
|
10
9
|
private childCodec;
|
|
11
10
|
parser: Parser;
|
|
12
11
|
constructor(childCodec: Codec<T>, parser?: Parser);
|
|
13
|
-
encode(input: T[]):
|
|
14
|
-
decode(input:
|
|
12
|
+
encode(input: T[]): Uint8Array;
|
|
13
|
+
decode(input: Uint8Array): T[];
|
|
15
14
|
static arrayParser(parser: Parser): Parser;
|
|
16
15
|
fromArray(inputs: T[]): DecodedArray<T>;
|
|
17
16
|
}
|
|
@@ -18,7 +18,6 @@ GNU Lesser General Public License for more details.
|
|
|
18
18
|
You should have received a copy of the GNU Lesser General Public License
|
|
19
19
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
20
|
*/
|
|
21
|
-
const buffer_1 = require("buffer/");
|
|
22
21
|
const binary_parser_1 = require("binary-parser");
|
|
23
22
|
const compact_int_codec_1 = require("./compact-int-codec");
|
|
24
23
|
class ArrayCodec {
|
|
@@ -31,7 +30,7 @@ class ArrayCodec {
|
|
|
31
30
|
for (const element of input) {
|
|
32
31
|
result.push(...this.childCodec.encode(element));
|
|
33
32
|
}
|
|
34
|
-
return
|
|
33
|
+
return new Uint8Array(result);
|
|
35
34
|
}
|
|
36
35
|
decode(input) {
|
|
37
36
|
const result = this.parser.parse(input);
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
import { Parser } from 'binary-parser';
|
|
3
2
|
import { ArrayCodec, DecodedArray } from './array-codec';
|
|
4
3
|
import { DecodedCompactInt } from './compact-int-codec';
|
|
@@ -10,14 +9,14 @@ import { Token } from './token-codec';
|
|
|
10
9
|
export interface AssetOutput {
|
|
11
10
|
amount: DecodedCompactInt;
|
|
12
11
|
lockupScript: LockupScript;
|
|
13
|
-
lockTime:
|
|
12
|
+
lockTime: Uint8Array;
|
|
14
13
|
tokens: DecodedArray<Token>;
|
|
15
14
|
additionalData: ByteString;
|
|
16
15
|
}
|
|
17
16
|
export declare class AssetOutputCodec implements Codec<AssetOutput> {
|
|
18
17
|
parser: Parser;
|
|
19
|
-
encode(input: AssetOutput):
|
|
20
|
-
decode(input:
|
|
18
|
+
encode(input: AssetOutput): Uint8Array;
|
|
19
|
+
decode(input: Uint8Array): AssetOutput;
|
|
21
20
|
static toFixedAssetOutputs(txIdBytes: Uint8Array, outputs: AssetOutput[]): FixedAssetOutput[];
|
|
22
21
|
static toFixedAssetOutput(txIdBytes: Uint8Array, output: AssetOutput, index: number): FixedAssetOutput;
|
|
23
22
|
static fromFixedAssetOutputs(fixedOutputs: FixedAssetOutput[]): AssetOutput[];
|
|
@@ -18,7 +18,6 @@ GNU Lesser General Public License for more details.
|
|
|
18
18
|
You should have received a copy of the GNU Lesser General Public License
|
|
19
19
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
20
|
*/
|
|
21
|
-
const buffer_1 = require("buffer/");
|
|
22
21
|
const binary_parser_1 = require("binary-parser");
|
|
23
22
|
const array_codec_1 = require("./array-codec");
|
|
24
23
|
const compact_int_codec_1 = require("./compact-int-codec");
|
|
@@ -49,12 +48,11 @@ class AssetOutputCodec {
|
|
|
49
48
|
});
|
|
50
49
|
}
|
|
51
50
|
encode(input) {
|
|
52
|
-
const amount =
|
|
51
|
+
const amount = compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount);
|
|
53
52
|
const lockupScript = lockup_script_codec_1.lockupScriptCodec.encode(input.lockupScript);
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
return buffer_1.Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData]);
|
|
53
|
+
const tokens = token_codec_1.tokensCodec.encode(input.tokens.value);
|
|
54
|
+
const additionalData = bytestring_codec_1.byteStringCodec.encode(input.additionalData);
|
|
55
|
+
return (0, utils_1.concatBytes)([amount, lockupScript, input.lockTime, tokens, additionalData]);
|
|
58
56
|
}
|
|
59
57
|
decode(input) {
|
|
60
58
|
return this.parser.parse(input);
|
|
@@ -67,13 +65,13 @@ class AssetOutputCodec {
|
|
|
67
65
|
const lockTime = Number(long_codec_1.longCodec.decode(output.lockTime));
|
|
68
66
|
const tokens = output.tokens.value.map((token) => {
|
|
69
67
|
return {
|
|
70
|
-
id: token.tokenId
|
|
68
|
+
id: (0, utils_1.binToHex)(token.tokenId),
|
|
71
69
|
amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
72
70
|
};
|
|
73
71
|
});
|
|
74
|
-
const message = output.additionalData.value
|
|
72
|
+
const message = (0, utils_1.binToHex)(output.additionalData.value);
|
|
75
73
|
const scriptType = output.lockupScript.scriptType;
|
|
76
|
-
const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)(
|
|
74
|
+
const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)((0, utils_1.concatBytes)([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
|
|
77
75
|
const outputLockupScript = output.lockupScript.script;
|
|
78
76
|
const address = utils_1.bs58.encode(lockup_script_codec_1.lockupScriptCodec.encode(output.lockupScript));
|
|
79
77
|
let hint = undefined;
|
|
@@ -105,10 +103,10 @@ class AssetOutputCodec {
|
|
|
105
103
|
static fromFixedAssetOutput(fixedOutput) {
|
|
106
104
|
const amount = compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(fixedOutput.attoAlphAmount));
|
|
107
105
|
const lockTime = long_codec_1.longCodec.encode(BigInt(fixedOutput.lockTime));
|
|
108
|
-
const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(
|
|
106
|
+
const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(utils_1.bs58.decode(fixedOutput.address));
|
|
109
107
|
const tokensValue = fixedOutput.tokens.map((token) => {
|
|
110
108
|
return {
|
|
111
|
-
tokenId:
|
|
109
|
+
tokenId: (0, utils_1.hexToBinUnsafe)(token.id),
|
|
112
110
|
amount: compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
113
111
|
};
|
|
114
112
|
});
|
|
@@ -116,7 +114,7 @@ class AssetOutputCodec {
|
|
|
116
114
|
length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(tokensValue.length),
|
|
117
115
|
value: tokensValue
|
|
118
116
|
};
|
|
119
|
-
const additionalDataValue =
|
|
117
|
+
const additionalDataValue = (0, utils_1.hexToBinUnsafe)(fixedOutput.message);
|
|
120
118
|
const additionalData = {
|
|
121
119
|
length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(additionalDataValue.length),
|
|
122
120
|
value: additionalDataValue
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
export declare class BigIntCodec {
|
|
3
|
-
static encode(value: bigint):
|
|
4
|
-
static decode(encoded:
|
|
2
|
+
static encode(value: bigint): Uint8Array;
|
|
3
|
+
static decode(encoded: Uint8Array, signed: boolean): bigint;
|
|
5
4
|
}
|
|
@@ -18,12 +18,11 @@ GNU Lesser General Public License for more details.
|
|
|
18
18
|
You should have received a copy of the GNU Lesser General Public License
|
|
19
19
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
20
|
*/
|
|
21
|
-
const buffer_1 = require("buffer/");
|
|
22
21
|
class BigIntCodec {
|
|
23
22
|
static encode(value) {
|
|
24
23
|
// Special case for zero.
|
|
25
24
|
if (value === 0n) {
|
|
26
|
-
return
|
|
25
|
+
return new Uint8Array([0]);
|
|
27
26
|
}
|
|
28
27
|
const isNegative = value < 0n;
|
|
29
28
|
let absValue = isNegative ? -value : value;
|
|
@@ -60,7 +59,7 @@ class BigIntCodec {
|
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
// The byte array needs to be reversed since we've constructed it in little-endian order.
|
|
63
|
-
return
|
|
62
|
+
return new Uint8Array(bytes.reverse());
|
|
64
63
|
}
|
|
65
64
|
static decode(encoded, signed) {
|
|
66
65
|
// Special case for zero.
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
import { Parser } from 'binary-parser';
|
|
3
2
|
import { DecodedCompactInt } from './compact-int-codec';
|
|
4
3
|
import { Codec } from './codec';
|
|
5
4
|
export interface ByteString {
|
|
6
5
|
length: DecodedCompactInt;
|
|
7
|
-
value:
|
|
6
|
+
value: Uint8Array;
|
|
8
7
|
}
|
|
9
8
|
export declare class ByteStringCodec implements Codec<ByteString> {
|
|
10
9
|
parser: Parser;
|
|
11
|
-
encode(input: ByteString):
|
|
12
|
-
decode(input:
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
encode(input: ByteString): Uint8Array;
|
|
11
|
+
decode(input: Uint8Array): ByteString;
|
|
12
|
+
encodeBytes(input: Uint8Array): Uint8Array;
|
|
13
|
+
decodeBytes(input: Uint8Array): Uint8Array;
|
|
15
14
|
}
|
|
16
15
|
export declare const byteStringCodec: ByteStringCodec;
|
|
@@ -18,9 +18,9 @@ GNU Lesser General Public License for more details.
|
|
|
18
18
|
You should have received a copy of the GNU Lesser General Public License
|
|
19
19
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
20
|
*/
|
|
21
|
-
const buffer_1 = require("buffer/");
|
|
22
21
|
const binary_parser_1 = require("binary-parser");
|
|
23
22
|
const compact_int_codec_1 = require("./compact-int-codec");
|
|
23
|
+
const utils_1 = require("../utils");
|
|
24
24
|
class ByteStringCodec {
|
|
25
25
|
constructor() {
|
|
26
26
|
this.parser = new binary_parser_1.Parser()
|
|
@@ -34,15 +34,15 @@ class ByteStringCodec {
|
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
encode(input) {
|
|
37
|
-
return
|
|
37
|
+
return (0, utils_1.concatBytes)([compact_int_codec_1.compactUnsignedIntCodec.encode(input.length), input.value]);
|
|
38
38
|
}
|
|
39
39
|
decode(input) {
|
|
40
40
|
return this.parser.parse(input);
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
return
|
|
42
|
+
encodeBytes(input) {
|
|
43
|
+
return (0, utils_1.concatBytes)([compact_int_codec_1.compactUnsignedIntCodec.encodeU32(input.length), input]);
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
decodeBytes(input) {
|
|
46
46
|
return this.decode(input).value;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
import { Parser } from 'binary-parser';
|
|
3
2
|
export interface Codec<T> {
|
|
4
3
|
parser: Parser;
|
|
5
|
-
encode(input: T):
|
|
6
|
-
decode(input:
|
|
4
|
+
encode(input: T): Uint8Array;
|
|
5
|
+
decode(input: Uint8Array): T;
|
|
7
6
|
}
|
|
8
7
|
export declare function assert(value: boolean, message: string): void;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
import { Parser } from 'binary-parser';
|
|
3
2
|
import { Codec } from './codec';
|
|
4
3
|
export declare class CompactInt {
|
|
@@ -19,12 +18,12 @@ export declare class CompactUnsignedIntCodec implements Codec<DecodedCompactInt>
|
|
|
19
18
|
private twoByteBound;
|
|
20
19
|
private fourByteBound;
|
|
21
20
|
parser: Parser;
|
|
22
|
-
encode(input: DecodedCompactInt):
|
|
23
|
-
encodeU32(value: number):
|
|
24
|
-
encodeU256(value: bigint):
|
|
25
|
-
decodeU32(input:
|
|
26
|
-
decodeU256(input:
|
|
27
|
-
decode(input:
|
|
21
|
+
encode(input: DecodedCompactInt): Uint8Array;
|
|
22
|
+
encodeU32(value: number): Uint8Array;
|
|
23
|
+
encodeU256(value: bigint): Uint8Array;
|
|
24
|
+
decodeU32(input: Uint8Array): number;
|
|
25
|
+
decodeU256(input: Uint8Array): bigint;
|
|
26
|
+
decode(input: Uint8Array): DecodedCompactInt;
|
|
28
27
|
toU32(value: DecodedCompactInt): number;
|
|
29
28
|
fromU32(value: number): DecodedCompactInt;
|
|
30
29
|
toU256(value: DecodedCompactInt): bigint;
|
|
@@ -37,12 +36,12 @@ export declare class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
37
36
|
private twoByteBound;
|
|
38
37
|
private fourByteBound;
|
|
39
38
|
parser: Parser;
|
|
40
|
-
encode(input: DecodedCompactInt):
|
|
41
|
-
decode(input:
|
|
42
|
-
decodeI32(input:
|
|
43
|
-
encodeI32(value: number):
|
|
44
|
-
encodeI256(value: bigint):
|
|
45
|
-
decodeI256(input:
|
|
39
|
+
encode(input: DecodedCompactInt): Uint8Array;
|
|
40
|
+
decode(input: Uint8Array): DecodedCompactInt;
|
|
41
|
+
decodeI32(input: Uint8Array): number;
|
|
42
|
+
encodeI32(value: number): Uint8Array;
|
|
43
|
+
encodeI256(value: bigint): Uint8Array;
|
|
44
|
+
decodeI256(input: Uint8Array): bigint;
|
|
46
45
|
toI32(value: DecodedCompactInt): number;
|
|
47
46
|
fromI32(value: number): DecodedCompactInt;
|
|
48
47
|
toI256(value: DecodedCompactInt): bigint;
|
|
@@ -18,10 +18,10 @@ GNU Lesser General Public License for more details.
|
|
|
18
18
|
You should have received a copy of the GNU Lesser General Public License
|
|
19
19
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
20
|
*/
|
|
21
|
-
const buffer_1 = require("buffer/");
|
|
22
21
|
const binary_parser_1 = require("binary-parser");
|
|
23
22
|
const codec_1 = require("./codec");
|
|
24
23
|
const bigint_codec_1 = require("./bigint-codec");
|
|
24
|
+
const utils_1 = require("../utils");
|
|
25
25
|
class CompactInt {
|
|
26
26
|
}
|
|
27
27
|
exports.CompactInt = CompactInt;
|
|
@@ -60,17 +60,17 @@ class CompactUnsignedIntCodec {
|
|
|
60
60
|
this.parser = compactIntParser;
|
|
61
61
|
}
|
|
62
62
|
encode(input) {
|
|
63
|
-
return
|
|
63
|
+
return new Uint8Array([input.mode, ...input.rest]);
|
|
64
64
|
}
|
|
65
65
|
encodeU32(value) {
|
|
66
66
|
if (value < this.oneByteBound) {
|
|
67
|
-
return
|
|
67
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff]);
|
|
68
68
|
}
|
|
69
69
|
else if (value < this.twoByteBound) {
|
|
70
|
-
return
|
|
70
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
|
|
71
71
|
}
|
|
72
72
|
else if (value < this.fourByteBound) {
|
|
73
|
-
return
|
|
73
|
+
return new Uint8Array([
|
|
74
74
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
75
75
|
(value >> 16) & 0xff,
|
|
76
76
|
(value >> 8) & 0xff,
|
|
@@ -78,7 +78,7 @@ class CompactUnsignedIntCodec {
|
|
|
78
78
|
]);
|
|
79
79
|
}
|
|
80
80
|
else {
|
|
81
|
-
return
|
|
81
|
+
return new Uint8Array([
|
|
82
82
|
CompactInt.multiBytePrefix,
|
|
83
83
|
(value >> 24) & 0xff,
|
|
84
84
|
(value >> 16) & 0xff,
|
|
@@ -99,7 +99,7 @@ class CompactUnsignedIntCodec {
|
|
|
99
99
|
}
|
|
100
100
|
(0, codec_1.assert)(bytes.length <= 32, 'Expect <= 32 bytes for U256');
|
|
101
101
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
|
|
102
|
-
return
|
|
102
|
+
return new Uint8Array([header, ...bytes]);
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
decodeU32(input) {
|
|
@@ -114,7 +114,7 @@ class CompactUnsignedIntCodec {
|
|
|
114
114
|
return this.parser.parse(input);
|
|
115
115
|
}
|
|
116
116
|
toU32(value) {
|
|
117
|
-
const body =
|
|
117
|
+
const body = new Uint8Array([value.mode, ...value.rest]);
|
|
118
118
|
return decodePositiveInt(value.mode, body);
|
|
119
119
|
}
|
|
120
120
|
fromU32(value) {
|
|
@@ -127,7 +127,7 @@ class CompactUnsignedIntCodec {
|
|
|
127
127
|
}
|
|
128
128
|
else {
|
|
129
129
|
(0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for U256');
|
|
130
|
-
return bigint_codec_1.BigIntCodec.decode(
|
|
130
|
+
return bigint_codec_1.BigIntCodec.decode(value.rest, false);
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
fromU256(value) {
|
|
@@ -145,7 +145,7 @@ class CompactSignedIntCodec {
|
|
|
145
145
|
this.parser = compactIntParser;
|
|
146
146
|
}
|
|
147
147
|
encode(input) {
|
|
148
|
-
return
|
|
148
|
+
return new Uint8Array([input.mode, ...input.rest]);
|
|
149
149
|
}
|
|
150
150
|
decode(input) {
|
|
151
151
|
return this.parser.parse(input);
|
|
@@ -157,13 +157,13 @@ class CompactSignedIntCodec {
|
|
|
157
157
|
encodeI32(value) {
|
|
158
158
|
if (value >= 0) {
|
|
159
159
|
if (value < this.oneByteBound) {
|
|
160
|
-
return
|
|
160
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff]);
|
|
161
161
|
}
|
|
162
162
|
else if (value < this.twoByteBound) {
|
|
163
|
-
return
|
|
163
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
|
|
164
164
|
}
|
|
165
165
|
else if (value < this.fourByteBound) {
|
|
166
|
-
return
|
|
166
|
+
return new Uint8Array([
|
|
167
167
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
168
168
|
(value >> 16) & 0xff,
|
|
169
169
|
(value >> 8) & 0xff,
|
|
@@ -171,7 +171,7 @@ class CompactSignedIntCodec {
|
|
|
171
171
|
]);
|
|
172
172
|
}
|
|
173
173
|
else {
|
|
174
|
-
return
|
|
174
|
+
return new Uint8Array([
|
|
175
175
|
CompactInt.multiBytePrefix,
|
|
176
176
|
(value >> 24) & 0xff,
|
|
177
177
|
(value >> 16) & 0xff,
|
|
@@ -182,13 +182,13 @@ class CompactSignedIntCodec {
|
|
|
182
182
|
}
|
|
183
183
|
else {
|
|
184
184
|
if (value >= -this.oneByteBound) {
|
|
185
|
-
return
|
|
185
|
+
return new Uint8Array([(value ^ CompactInt.oneByteNegPrefix) & 0xff]);
|
|
186
186
|
}
|
|
187
187
|
else if (value >= -this.twoByteBound) {
|
|
188
|
-
return
|
|
188
|
+
return new Uint8Array([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff]);
|
|
189
189
|
}
|
|
190
190
|
else if (value >= -this.fourByteBound) {
|
|
191
|
-
return
|
|
191
|
+
return new Uint8Array([
|
|
192
192
|
((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
|
|
193
193
|
(value >> 16) & 0xff,
|
|
194
194
|
(value >> 8) & 0xff,
|
|
@@ -196,7 +196,7 @@ class CompactSignedIntCodec {
|
|
|
196
196
|
]);
|
|
197
197
|
}
|
|
198
198
|
else {
|
|
199
|
-
return
|
|
199
|
+
return new Uint8Array([
|
|
200
200
|
CompactInt.multiBytePrefix,
|
|
201
201
|
(value >> 24) & 0xff,
|
|
202
202
|
(value >> 16) & 0xff,
|
|
@@ -213,7 +213,7 @@ class CompactSignedIntCodec {
|
|
|
213
213
|
else {
|
|
214
214
|
const bytes = bigint_codec_1.BigIntCodec.encode(value);
|
|
215
215
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
|
|
216
|
-
return
|
|
216
|
+
return new Uint8Array([header, ...bytes]);
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
decodeI256(input) {
|
|
@@ -221,7 +221,7 @@ class CompactSignedIntCodec {
|
|
|
221
221
|
return this.toI256(decoded);
|
|
222
222
|
}
|
|
223
223
|
toI32(value) {
|
|
224
|
-
const body =
|
|
224
|
+
const body = new Uint8Array([value.mode, ...value.rest]);
|
|
225
225
|
const mode = value.mode & maskRest;
|
|
226
226
|
if (fixedSize(mode)) {
|
|
227
227
|
const isPositive = (value.mode & signFlag) == 0;
|
|
@@ -251,7 +251,7 @@ class CompactSignedIntCodec {
|
|
|
251
251
|
}
|
|
252
252
|
else {
|
|
253
253
|
(0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for I256');
|
|
254
|
-
return bigint_codec_1.BigIntCodec.decode(
|
|
254
|
+
return bigint_codec_1.BigIntCodec.decode(value.rest, true);
|
|
255
255
|
}
|
|
256
256
|
}
|
|
257
257
|
fromI256(value) {
|
|
@@ -273,7 +273,7 @@ function decodePositiveInt(rawMode, body) {
|
|
|
273
273
|
return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
|
|
274
274
|
default:
|
|
275
275
|
if (body.length === 5) {
|
|
276
|
-
return Number(BigInt('0x' + body.slice(1)
|
|
276
|
+
return Number(BigInt('0x' + (0, utils_1.binToHex)(body.slice(1))));
|
|
277
277
|
}
|
|
278
278
|
else {
|
|
279
279
|
throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`);
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Buffer } from 'buffer/';
|
|
2
1
|
import { Parser } from 'binary-parser';
|
|
3
2
|
import { DecodedArray } from './array-codec';
|
|
4
3
|
import { Codec } from './codec';
|
|
@@ -7,17 +6,17 @@ import { Method } from './method-codec';
|
|
|
7
6
|
export interface HalfDecodedContract {
|
|
8
7
|
fieldLength: DecodedCompactInt;
|
|
9
8
|
methodIndexes: DecodedArray<DecodedCompactInt>;
|
|
10
|
-
methods:
|
|
9
|
+
methods: Uint8Array;
|
|
11
10
|
}
|
|
12
11
|
export interface Contract {
|
|
13
12
|
fieldLength: number;
|
|
14
13
|
methods: Method[];
|
|
15
14
|
}
|
|
16
|
-
export declare function toHalfDecoded(contract: Contract): HalfDecodedContract;
|
|
17
15
|
export declare class ContractCodec implements Codec<HalfDecodedContract> {
|
|
18
16
|
parser: Parser;
|
|
19
|
-
encode(input: HalfDecodedContract):
|
|
20
|
-
decode(input:
|
|
21
|
-
decodeContract(input:
|
|
17
|
+
encode(input: HalfDecodedContract): Uint8Array;
|
|
18
|
+
decode(input: Uint8Array): HalfDecodedContract;
|
|
19
|
+
decodeContract(input: Uint8Array): Contract;
|
|
20
|
+
encodeContract(contract: Contract): Uint8Array;
|
|
22
21
|
}
|
|
23
22
|
export declare const contractCodec: ContractCodec;
|