@alephium/web3 0.30.2 → 0.31.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/api/api-alephium.d.ts +39 -25
- package/dist/src/api/api-alephium.js +49 -34
- package/dist/src/api/node-provider.js +1 -2
- package/dist/src/contract/contract.d.ts +1 -0
- package/dist/src/contract/contract.js +42 -8
- package/dist/src/utils/address.d.ts +20 -1
- package/dist/src/utils/address.js +168 -14
- package/dist/src/utils/error.d.ts +15 -0
- package/dist/src/utils/error.js +66 -0
- package/dist/src/utils/utils.d.ts +0 -19
- package/dist/src/utils/utils.js +1 -152
- package/package.json +2 -2
- package/src/api/api-alephium.ts +64 -44
- package/src/api/node-provider.ts +1 -2
- package/src/contract/contract.ts +62 -10
- package/src/utils/address.ts +167 -12
- package/src/utils/error.ts +77 -0
- package/src/utils/utils.ts +0 -155
|
@@ -655,6 +655,10 @@ export interface Reachable {
|
|
|
655
655
|
peers: string[];
|
|
656
656
|
type: string;
|
|
657
657
|
}
|
|
658
|
+
export interface Result {
|
|
659
|
+
/** @format bigint */
|
|
660
|
+
hashrate: string;
|
|
661
|
+
}
|
|
658
662
|
export interface RevealMnemonic {
|
|
659
663
|
password: string;
|
|
660
664
|
}
|
|
@@ -732,6 +736,10 @@ export interface SweepAddressTransaction {
|
|
|
732
736
|
/** @format uint256 */
|
|
733
737
|
gasPrice: string;
|
|
734
738
|
}
|
|
739
|
+
export interface TargetToHashrate {
|
|
740
|
+
/** @format hex-string */
|
|
741
|
+
target: string;
|
|
742
|
+
}
|
|
735
743
|
export interface TestContract {
|
|
736
744
|
/** @format int32 */
|
|
737
745
|
group?: number;
|
|
@@ -987,7 +995,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
|
|
|
987
995
|
}
|
|
988
996
|
/**
|
|
989
997
|
* @title Alephium API
|
|
990
|
-
* @version 2.8.
|
|
998
|
+
* @version 2.8.5
|
|
991
999
|
* @baseUrl ../
|
|
992
1000
|
*/
|
|
993
1001
|
export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
@@ -1617,10 +1625,7 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
1617
1625
|
* @summary Get contract state
|
|
1618
1626
|
* @request GET:/contracts/{address}/state
|
|
1619
1627
|
*/
|
|
1620
|
-
getContractsAddressState: (address: string,
|
|
1621
|
-
/** @format int32 */
|
|
1622
|
-
group: number;
|
|
1623
|
-
}, params?: RequestParams) => Promise<ContractState>;
|
|
1628
|
+
getContractsAddressState: (address: string, params?: RequestParams) => Promise<ContractState>;
|
|
1624
1629
|
/**
|
|
1625
1630
|
* No description
|
|
1626
1631
|
*
|
|
@@ -1687,26 +1692,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
1687
1692
|
*/
|
|
1688
1693
|
postMultisigSubmit: (data: SubmitMultisig, params?: RequestParams) => Promise<SubmitTxResult>;
|
|
1689
1694
|
};
|
|
1690
|
-
utils: {
|
|
1691
|
-
/**
|
|
1692
|
-
* No description
|
|
1693
|
-
*
|
|
1694
|
-
* @tags Utils
|
|
1695
|
-
* @name PostUtilsVerifySignature
|
|
1696
|
-
* @summary Verify the SecP256K1 signature of some data
|
|
1697
|
-
* @request POST:/utils/verify-signature
|
|
1698
|
-
*/
|
|
1699
|
-
postUtilsVerifySignature: (data: VerifySignature, params?: RequestParams) => Promise<boolean>;
|
|
1700
|
-
/**
|
|
1701
|
-
* No description
|
|
1702
|
-
*
|
|
1703
|
-
* @tags Utils
|
|
1704
|
-
* @name PutUtilsCheckHashIndexing
|
|
1705
|
-
* @summary Check and repair the indexing of block hashes
|
|
1706
|
-
* @request PUT:/utils/check-hash-indexing
|
|
1707
|
-
*/
|
|
1708
|
-
putUtilsCheckHashIndexing: (params?: RequestParams) => Promise<void>;
|
|
1709
|
-
};
|
|
1710
1695
|
miners: {
|
|
1711
1696
|
/**
|
|
1712
1697
|
* No description
|
|
@@ -1803,5 +1788,34 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
1803
1788
|
group?: number;
|
|
1804
1789
|
}, params?: RequestParams) => Promise<ContractEventsByBlockHash>;
|
|
1805
1790
|
};
|
|
1791
|
+
utils: {
|
|
1792
|
+
/**
|
|
1793
|
+
* No description
|
|
1794
|
+
*
|
|
1795
|
+
* @tags Utils
|
|
1796
|
+
* @name PostUtilsVerifySignature
|
|
1797
|
+
* @summary Verify the SecP256K1 signature of some data
|
|
1798
|
+
* @request POST:/utils/verify-signature
|
|
1799
|
+
*/
|
|
1800
|
+
postUtilsVerifySignature: (data: VerifySignature, params?: RequestParams) => Promise<boolean>;
|
|
1801
|
+
/**
|
|
1802
|
+
* No description
|
|
1803
|
+
*
|
|
1804
|
+
* @tags Utils
|
|
1805
|
+
* @name PostUtilsTargetToHashrate
|
|
1806
|
+
* @summary Convert a target to hashrate
|
|
1807
|
+
* @request POST:/utils/target-to-hashrate
|
|
1808
|
+
*/
|
|
1809
|
+
postUtilsTargetToHashrate: (data: TargetToHashrate, params?: RequestParams) => Promise<Result>;
|
|
1810
|
+
/**
|
|
1811
|
+
* No description
|
|
1812
|
+
*
|
|
1813
|
+
* @tags Utils
|
|
1814
|
+
* @name PutUtilsCheckHashIndexing
|
|
1815
|
+
* @summary Check and repair the indexing of block hashes
|
|
1816
|
+
* @request PUT:/utils/check-hash-indexing
|
|
1817
|
+
*/
|
|
1818
|
+
putUtilsCheckHashIndexing: (params?: RequestParams) => Promise<void>;
|
|
1819
|
+
};
|
|
1806
1820
|
}
|
|
1807
1821
|
export {};
|
|
@@ -151,7 +151,7 @@ class HttpClient {
|
|
|
151
151
|
exports.HttpClient = HttpClient;
|
|
152
152
|
/**
|
|
153
153
|
* @title Alephium API
|
|
154
|
-
* @version 2.8.
|
|
154
|
+
* @version 2.8.5
|
|
155
155
|
* @baseUrl ../
|
|
156
156
|
*/
|
|
157
157
|
class Api extends HttpClient {
|
|
@@ -1051,10 +1051,9 @@ class Api extends HttpClient {
|
|
|
1051
1051
|
* @summary Get contract state
|
|
1052
1052
|
* @request GET:/contracts/{address}/state
|
|
1053
1053
|
*/
|
|
1054
|
-
getContractsAddressState: (address,
|
|
1054
|
+
getContractsAddressState: (address, params = {}) => this.request({
|
|
1055
1055
|
path: `/contracts/${address}/state`,
|
|
1056
1056
|
method: 'GET',
|
|
1057
|
-
query: query,
|
|
1058
1057
|
format: 'json',
|
|
1059
1058
|
...params
|
|
1060
1059
|
}).then(utils_1.convertHttpResponse),
|
|
@@ -1173,37 +1172,6 @@ class Api extends HttpClient {
|
|
|
1173
1172
|
...params
|
|
1174
1173
|
}).then(utils_1.convertHttpResponse)
|
|
1175
1174
|
};
|
|
1176
|
-
this.utils = {
|
|
1177
|
-
/**
|
|
1178
|
-
* No description
|
|
1179
|
-
*
|
|
1180
|
-
* @tags Utils
|
|
1181
|
-
* @name PostUtilsVerifySignature
|
|
1182
|
-
* @summary Verify the SecP256K1 signature of some data
|
|
1183
|
-
* @request POST:/utils/verify-signature
|
|
1184
|
-
*/
|
|
1185
|
-
postUtilsVerifySignature: (data, params = {}) => this.request({
|
|
1186
|
-
path: `/utils/verify-signature`,
|
|
1187
|
-
method: 'POST',
|
|
1188
|
-
body: data,
|
|
1189
|
-
type: ContentType.Json,
|
|
1190
|
-
format: 'json',
|
|
1191
|
-
...params
|
|
1192
|
-
}).then(utils_1.convertHttpResponse),
|
|
1193
|
-
/**
|
|
1194
|
-
* No description
|
|
1195
|
-
*
|
|
1196
|
-
* @tags Utils
|
|
1197
|
-
* @name PutUtilsCheckHashIndexing
|
|
1198
|
-
* @summary Check and repair the indexing of block hashes
|
|
1199
|
-
* @request PUT:/utils/check-hash-indexing
|
|
1200
|
-
*/
|
|
1201
|
-
putUtilsCheckHashIndexing: (params = {}) => this.request({
|
|
1202
|
-
path: `/utils/check-hash-indexing`,
|
|
1203
|
-
method: 'PUT',
|
|
1204
|
-
...params
|
|
1205
|
-
}).then(utils_1.convertHttpResponse)
|
|
1206
|
-
};
|
|
1207
1175
|
this.miners = {
|
|
1208
1176
|
/**
|
|
1209
1177
|
* No description
|
|
@@ -1326,6 +1294,53 @@ class Api extends HttpClient {
|
|
|
1326
1294
|
...params
|
|
1327
1295
|
}).then(utils_1.convertHttpResponse)
|
|
1328
1296
|
};
|
|
1297
|
+
this.utils = {
|
|
1298
|
+
/**
|
|
1299
|
+
* No description
|
|
1300
|
+
*
|
|
1301
|
+
* @tags Utils
|
|
1302
|
+
* @name PostUtilsVerifySignature
|
|
1303
|
+
* @summary Verify the SecP256K1 signature of some data
|
|
1304
|
+
* @request POST:/utils/verify-signature
|
|
1305
|
+
*/
|
|
1306
|
+
postUtilsVerifySignature: (data, params = {}) => this.request({
|
|
1307
|
+
path: `/utils/verify-signature`,
|
|
1308
|
+
method: 'POST',
|
|
1309
|
+
body: data,
|
|
1310
|
+
type: ContentType.Json,
|
|
1311
|
+
format: 'json',
|
|
1312
|
+
...params
|
|
1313
|
+
}).then(utils_1.convertHttpResponse),
|
|
1314
|
+
/**
|
|
1315
|
+
* No description
|
|
1316
|
+
*
|
|
1317
|
+
* @tags Utils
|
|
1318
|
+
* @name PostUtilsTargetToHashrate
|
|
1319
|
+
* @summary Convert a target to hashrate
|
|
1320
|
+
* @request POST:/utils/target-to-hashrate
|
|
1321
|
+
*/
|
|
1322
|
+
postUtilsTargetToHashrate: (data, params = {}) => this.request({
|
|
1323
|
+
path: `/utils/target-to-hashrate`,
|
|
1324
|
+
method: 'POST',
|
|
1325
|
+
body: data,
|
|
1326
|
+
type: ContentType.Json,
|
|
1327
|
+
format: 'json',
|
|
1328
|
+
...params
|
|
1329
|
+
}).then(utils_1.convertHttpResponse),
|
|
1330
|
+
/**
|
|
1331
|
+
* No description
|
|
1332
|
+
*
|
|
1333
|
+
* @tags Utils
|
|
1334
|
+
* @name PutUtilsCheckHashIndexing
|
|
1335
|
+
* @summary Check and repair the indexing of block hashes
|
|
1336
|
+
* @request PUT:/utils/check-hash-indexing
|
|
1337
|
+
*/
|
|
1338
|
+
putUtilsCheckHashIndexing: (params = {}) => this.request({
|
|
1339
|
+
path: `/utils/check-hash-indexing`,
|
|
1340
|
+
method: 'PUT',
|
|
1341
|
+
...params
|
|
1342
|
+
}).then(utils_1.convertHttpResponse)
|
|
1343
|
+
};
|
|
1329
1344
|
}
|
|
1330
1345
|
}
|
|
1331
1346
|
exports.Api = Api;
|
|
@@ -134,8 +134,7 @@ class NodeProvider {
|
|
|
134
134
|
};
|
|
135
135
|
this.guessStdInterfaceId = async (tokenId) => {
|
|
136
136
|
const address = (0, utils_1.addressFromTokenId)(tokenId);
|
|
137
|
-
const
|
|
138
|
-
const rawState = await this.contracts.getContractsAddressState((0, utils_1.addressFromTokenId)(tokenId), { group });
|
|
137
|
+
const rawState = await this.contracts.getContractsAddressState(address);
|
|
139
138
|
const lastImmField = rawState.immFields.slice(-1).pop()?.value;
|
|
140
139
|
const interfaceIdPrefix = '414c5048'; // the hex of 'ALPH'
|
|
141
140
|
if (typeof lastImmField === 'string' && lastImmField.startsWith(interfaceIdPrefix)) {
|
|
@@ -84,6 +84,7 @@ export declare class Project {
|
|
|
84
84
|
static script(name: string): Script;
|
|
85
85
|
private saveArtifactsToFile;
|
|
86
86
|
contractByCodeHash(codeHash: string): Contract;
|
|
87
|
+
private static getCompileResult;
|
|
87
88
|
private static compile;
|
|
88
89
|
private static loadArtifacts;
|
|
89
90
|
private static getImportSourcePath;
|
|
@@ -55,6 +55,7 @@ const path = __importStar(require("path"));
|
|
|
55
55
|
const events_1 = require("./events");
|
|
56
56
|
const constants_1 = require("../constants");
|
|
57
57
|
const blake = __importStar(require("blakejs"));
|
|
58
|
+
const error_1 = require("../utils/error");
|
|
58
59
|
const crypto = new utils_1.WebCrypto();
|
|
59
60
|
exports.StdIdFieldName = '__stdInterfaceId';
|
|
60
61
|
var SourceKind;
|
|
@@ -125,6 +126,19 @@ class Compiled {
|
|
|
125
126
|
this.warnings = warnings;
|
|
126
127
|
}
|
|
127
128
|
}
|
|
129
|
+
function findSourceInfoAtLineNumber(sources, line) {
|
|
130
|
+
let currentLine = 0;
|
|
131
|
+
const sourceInfosWithLine = sources.map((source) => {
|
|
132
|
+
const startIndex = currentLine + 1;
|
|
133
|
+
currentLine += source.sourceCode.split('\n').length;
|
|
134
|
+
const endIndex = currentLine;
|
|
135
|
+
return { sourceInfo: source, startIndex: startIndex, endIndex: endIndex };
|
|
136
|
+
});
|
|
137
|
+
const sourceInfo = sourceInfosWithLine.find((sourceInfoWithLine) => {
|
|
138
|
+
return line >= sourceInfoWithLine.startIndex && line <= sourceInfoWithLine.endIndex;
|
|
139
|
+
});
|
|
140
|
+
return sourceInfo;
|
|
141
|
+
}
|
|
128
142
|
class ProjectArtifact {
|
|
129
143
|
static checkCompilerOptionsParameter(compilerOptions) {
|
|
130
144
|
if (Object.keys(compilerOptions).length != Object.keys(exports.DEFAULT_NODE_COMPILER_OPTIONS).length) {
|
|
@@ -302,6 +316,31 @@ class Project {
|
|
|
302
316
|
}
|
|
303
317
|
return contract.artifact;
|
|
304
318
|
}
|
|
319
|
+
static async getCompileResult(provider, compilerOptions, sources) {
|
|
320
|
+
try {
|
|
321
|
+
const sourceStr = sources.map((f) => f.sourceCode).join('\n');
|
|
322
|
+
return await provider.contracts.postContractsCompileProject({
|
|
323
|
+
code: sourceStr,
|
|
324
|
+
compilerOptions: compilerOptions
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
if (!(error instanceof Error)) {
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
const parsed = (0, error_1.parseError)(error.message);
|
|
332
|
+
if (!parsed) {
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
const sourceInfo = findSourceInfoAtLineNumber(sources, parsed.lineStart);
|
|
336
|
+
if (!sourceInfo) {
|
|
337
|
+
throw error;
|
|
338
|
+
}
|
|
339
|
+
const shiftIndex = parsed.lineStart - sourceInfo.startIndex + 1;
|
|
340
|
+
const newError = parsed.reformat(shiftIndex, sourceInfo.sourceInfo.contractRelativePath);
|
|
341
|
+
throw new Error(newError);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
305
344
|
static async compile(fullNodeVersion, provider, sourceInfos, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
|
|
306
345
|
const removeDuplicates = sourceInfos.reduce((acc, sourceInfo) => {
|
|
307
346
|
if (acc.find((info) => info.sourceCodeHash === sourceInfo.sourceCodeHash) === undefined) {
|
|
@@ -309,11 +348,7 @@ class Project {
|
|
|
309
348
|
}
|
|
310
349
|
return acc;
|
|
311
350
|
}, []);
|
|
312
|
-
const
|
|
313
|
-
const result = await provider.contracts.postContractsCompileProject({
|
|
314
|
-
code: sourceStr,
|
|
315
|
-
compilerOptions: compilerOptions
|
|
316
|
-
});
|
|
351
|
+
const result = await Project.getCompileResult(provider, compilerOptions, removeDuplicates);
|
|
317
352
|
const contracts = new Map();
|
|
318
353
|
const scripts = new Map();
|
|
319
354
|
result.contracts.forEach((contractResult) => {
|
|
@@ -996,6 +1031,7 @@ exports.ExecutableScript = ExecutableScript;
|
|
|
996
1031
|
function specialContractAddress(n) {
|
|
997
1032
|
const bytes = new Uint8Array(32).fill(0);
|
|
998
1033
|
bytes[31] = n;
|
|
1034
|
+
console.log(utils_1.addressFromContractId);
|
|
999
1035
|
return (0, utils_1.addressFromContractId)((0, utils_1.binToHex)(bytes));
|
|
1000
1036
|
}
|
|
1001
1037
|
exports.CreateContractEventAddress = specialContractAddress(-1);
|
|
@@ -1087,9 +1123,7 @@ class ContractInstance {
|
|
|
1087
1123
|
}
|
|
1088
1124
|
exports.ContractInstance = ContractInstance;
|
|
1089
1125
|
async function fetchContractState(contract, instance) {
|
|
1090
|
-
const contractState = await (0, global_1.getCurrentNodeProvider)().contracts.getContractsAddressState(instance.address
|
|
1091
|
-
group: instance.groupIndex
|
|
1092
|
-
});
|
|
1126
|
+
const contractState = await (0, global_1.getCurrentNodeProvider)().contracts.getContractsAddressState(instance.address);
|
|
1093
1127
|
const state = contract.contract.fromApiContractState(contractState);
|
|
1094
1128
|
return {
|
|
1095
1129
|
...state,
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
import { KeyType } from '../signer';
|
|
2
|
+
export declare enum AddressType {
|
|
3
|
+
P2PKH = 0,
|
|
4
|
+
P2MPKH = 1,
|
|
5
|
+
P2SH = 2,
|
|
6
|
+
P2C = 3
|
|
7
|
+
}
|
|
8
|
+
export declare function validateAddress(address: string): void;
|
|
9
|
+
export declare function groupOfAddress(address: string): number;
|
|
10
|
+
export declare function contractIdFromAddress(address: string): Uint8Array;
|
|
11
|
+
export declare function tokenIdFromAddress(address: string): Uint8Array;
|
|
12
|
+
export declare function groupOfPrivateKey(privateKey: string, keyType?: KeyType): number;
|
|
13
|
+
export declare function publicKeyFromPrivateKey(privateKey: string, _keyType?: KeyType): string;
|
|
14
|
+
export declare function addressFromPublicKey(publicKey: string, _keyType?: KeyType): string;
|
|
15
|
+
export declare function addressFromScript(script: Uint8Array): string;
|
|
16
|
+
export declare function addressFromContractId(contractId: string): string;
|
|
17
|
+
export declare function addressFromTokenId(tokenId: string): string;
|
|
18
|
+
export declare function contractIdFromTx(txId: string, outputIndex: number): string;
|
|
19
|
+
export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
|
|
20
|
+
export declare function xorByte(intValue: number): number;
|
|
@@ -20,23 +20,177 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.
|
|
23
|
+
exports.xorByte = exports.subContractId = exports.contractIdFromTx = exports.addressFromTokenId = exports.addressFromContractId = exports.addressFromScript = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.validateAddress = exports.AddressType = void 0;
|
|
24
|
+
const elliptic_1 = require("elliptic");
|
|
25
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
26
|
+
const constants_1 = require("../constants");
|
|
27
|
+
const blakejs_1 = __importDefault(require("blakejs"));
|
|
24
28
|
const bs58_1 = __importDefault(require("./bs58"));
|
|
25
29
|
const djb2_1 = __importDefault(require("./djb2"));
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
const utils_1 = require("./utils");
|
|
31
|
+
const ec = new elliptic_1.ec('secp256k1');
|
|
32
|
+
var AddressType;
|
|
33
|
+
(function (AddressType) {
|
|
34
|
+
AddressType[AddressType["P2PKH"] = 0] = "P2PKH";
|
|
35
|
+
AddressType[AddressType["P2MPKH"] = 1] = "P2MPKH";
|
|
36
|
+
AddressType[AddressType["P2SH"] = 2] = "P2SH";
|
|
37
|
+
AddressType[AddressType["P2C"] = 3] = "P2C";
|
|
38
|
+
})(AddressType = exports.AddressType || (exports.AddressType = {}));
|
|
39
|
+
function validateAddress(address) {
|
|
40
|
+
let decoded;
|
|
41
|
+
try {
|
|
42
|
+
decoded = bs58_1.default.decode(address);
|
|
43
|
+
}
|
|
44
|
+
catch (_) {
|
|
45
|
+
throw new Error('Invalid base58 string');
|
|
46
|
+
}
|
|
47
|
+
if (decoded.length === 0)
|
|
48
|
+
throw new Error('Address is empty');
|
|
49
|
+
const addressType = decoded[0];
|
|
50
|
+
if (addressType === AddressType.P2MPKH) {
|
|
51
|
+
// [1, n, ...hashes, m]
|
|
52
|
+
if ((decoded.length - 3) % 32 === 0)
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
else if (addressType === AddressType.P2PKH || addressType === AddressType.P2SH || addressType === AddressType.P2C) {
|
|
56
|
+
// [type, ...hash]
|
|
57
|
+
if (decoded.length === 33)
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
throw new Error(`Invalid address: ${address}`);
|
|
61
|
+
}
|
|
62
|
+
exports.validateAddress = validateAddress;
|
|
63
|
+
function groupOfAddress(address) {
|
|
64
|
+
validateAddress(address);
|
|
65
|
+
const decoded = bs58_1.default.decode(address);
|
|
66
|
+
const addressType = decoded[0];
|
|
67
|
+
const addressBody = decoded.slice(1);
|
|
68
|
+
if (addressType == AddressType.P2PKH) {
|
|
69
|
+
return groupOfP2pkhAddress(addressBody);
|
|
70
|
+
}
|
|
71
|
+
else if (addressType == AddressType.P2MPKH) {
|
|
72
|
+
return groupOfP2mpkhAddress(addressBody);
|
|
73
|
+
}
|
|
74
|
+
else if (addressType == AddressType.P2SH) {
|
|
75
|
+
return groupOfP2shAddress(addressBody);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// Contract Address
|
|
79
|
+
const id = contractIdFromAddress(address);
|
|
80
|
+
return id[`${id.length - 1}`];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.groupOfAddress = groupOfAddress;
|
|
84
|
+
function groupOfAddressBytes(bytes) {
|
|
85
|
+
const hint = (0, djb2_1.default)(bytes) | 1;
|
|
86
|
+
const hash = xorByte(hint);
|
|
87
|
+
const group = hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
|
|
31
88
|
return group;
|
|
32
89
|
}
|
|
33
|
-
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
90
|
+
// Pay to public key hash address
|
|
91
|
+
function groupOfP2pkhAddress(address) {
|
|
92
|
+
return groupOfAddressBytes(address);
|
|
93
|
+
}
|
|
94
|
+
// Pay to multiple public key hash address
|
|
95
|
+
function groupOfP2mpkhAddress(address) {
|
|
96
|
+
return groupOfAddressBytes(address.slice(1, 33));
|
|
97
|
+
}
|
|
98
|
+
// Pay to script hash address
|
|
99
|
+
function groupOfP2shAddress(address) {
|
|
100
|
+
return groupOfAddressBytes(address);
|
|
101
|
+
}
|
|
102
|
+
function contractIdFromAddress(address) {
|
|
103
|
+
return idFromAddress(address);
|
|
104
|
+
}
|
|
105
|
+
exports.contractIdFromAddress = contractIdFromAddress;
|
|
106
|
+
function tokenIdFromAddress(address) {
|
|
107
|
+
return idFromAddress(address);
|
|
108
|
+
}
|
|
109
|
+
exports.tokenIdFromAddress = tokenIdFromAddress;
|
|
110
|
+
function idFromAddress(address) {
|
|
111
|
+
const decoded = bs58_1.default.decode(address);
|
|
112
|
+
if (decoded.length == 0)
|
|
113
|
+
throw new Error('Address string is empty');
|
|
114
|
+
const addressType = decoded[0];
|
|
115
|
+
const addressBody = decoded.slice(1);
|
|
116
|
+
if (addressType == AddressType.P2C) {
|
|
117
|
+
return addressBody;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new Error(`Invalid contract address type: ${addressType}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function groupOfPrivateKey(privateKey, keyType) {
|
|
124
|
+
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey, keyType), keyType));
|
|
125
|
+
}
|
|
126
|
+
exports.groupOfPrivateKey = groupOfPrivateKey;
|
|
127
|
+
function publicKeyFromPrivateKey(privateKey, _keyType) {
|
|
128
|
+
const keyType = _keyType ?? 'default';
|
|
129
|
+
if (keyType === 'default') {
|
|
130
|
+
const key = ec.keyFromPrivate(privateKey);
|
|
131
|
+
return key.getPublic(true, 'hex');
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return ec.g.mul(new bn_js_1.default(privateKey, 16)).encode('hex', true).slice(2);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
|
|
138
|
+
function addressFromPublicKey(publicKey, _keyType) {
|
|
139
|
+
const keyType = _keyType ?? 'default';
|
|
140
|
+
if (keyType === 'default') {
|
|
141
|
+
const addressType = Buffer.from([AddressType.P2PKH]);
|
|
142
|
+
const hash = Buffer.from(blakejs_1.default.blake2b(Buffer.from(publicKey, 'hex'), undefined, 32));
|
|
143
|
+
const bytes = Buffer.concat([addressType, hash]);
|
|
144
|
+
return bs58_1.default.encode(bytes);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
const lockupScript = Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex');
|
|
148
|
+
return addressFromScript(lockupScript);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.addressFromPublicKey = addressFromPublicKey;
|
|
152
|
+
function addressFromScript(script) {
|
|
153
|
+
const scriptHash = blakejs_1.default.blake2b(script, undefined, 32);
|
|
154
|
+
const addressType = Buffer.from([AddressType.P2SH]);
|
|
155
|
+
return bs58_1.default.encode(Buffer.concat([addressType, scriptHash]));
|
|
156
|
+
}
|
|
157
|
+
exports.addressFromScript = addressFromScript;
|
|
158
|
+
function addressFromContractId(contractId) {
|
|
159
|
+
const addressType = Buffer.from([AddressType.P2C]);
|
|
160
|
+
const hash = Buffer.from((0, utils_1.hexToBinUnsafe)(contractId));
|
|
161
|
+
const bytes = Buffer.concat([addressType, hash]);
|
|
162
|
+
return bs58_1.default.encode(bytes);
|
|
163
|
+
}
|
|
164
|
+
exports.addressFromContractId = addressFromContractId;
|
|
165
|
+
function addressFromTokenId(tokenId) {
|
|
166
|
+
const contractId = tokenId; // contract ID is the same as token ID
|
|
167
|
+
return addressFromContractId(contractId);
|
|
168
|
+
}
|
|
169
|
+
exports.addressFromTokenId = addressFromTokenId;
|
|
170
|
+
function contractIdFromTx(txId, outputIndex) {
|
|
171
|
+
const txIdBin = (0, utils_1.hexToBinUnsafe)(txId);
|
|
172
|
+
const data = Buffer.concat([txIdBin, Buffer.from([outputIndex])]);
|
|
173
|
+
const hash = blakejs_1.default.blake2b(data, undefined, 32);
|
|
174
|
+
return (0, utils_1.binToHex)(hash);
|
|
175
|
+
}
|
|
176
|
+
exports.contractIdFromTx = contractIdFromTx;
|
|
177
|
+
function subContractId(parentContractId, pathInHex, group) {
|
|
178
|
+
if (group < 0 || group >= constants_1.TOTAL_NUMBER_OF_GROUPS) {
|
|
179
|
+
throw new Error(`Invalid group ${group}`);
|
|
180
|
+
}
|
|
181
|
+
const data = Buffer.concat([(0, utils_1.hexToBinUnsafe)(parentContractId), (0, utils_1.hexToBinUnsafe)(pathInHex)]);
|
|
182
|
+
const bytes = Buffer.concat([
|
|
183
|
+
blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
|
|
184
|
+
Buffer.from([group])
|
|
185
|
+
]);
|
|
186
|
+
return (0, utils_1.binToHex)(bytes);
|
|
39
187
|
}
|
|
40
|
-
|
|
41
|
-
|
|
188
|
+
exports.subContractId = subContractId;
|
|
189
|
+
function xorByte(intValue) {
|
|
190
|
+
const byte0 = (intValue >> 24) & 0xff;
|
|
191
|
+
const byte1 = (intValue >> 16) & 0xff;
|
|
192
|
+
const byte2 = (intValue >> 8) & 0xff;
|
|
193
|
+
const byte3 = intValue & 0xff;
|
|
194
|
+
return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
|
|
42
195
|
}
|
|
196
|
+
exports.xorByte = xorByte;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare class CompilationError {
|
|
2
|
+
lineStart: number;
|
|
3
|
+
column: number;
|
|
4
|
+
errorType: string;
|
|
5
|
+
line: number;
|
|
6
|
+
codeLine: string;
|
|
7
|
+
errorIndicator: string;
|
|
8
|
+
message: string;
|
|
9
|
+
additionalLine1?: string | undefined;
|
|
10
|
+
additionalLine2?: string | undefined;
|
|
11
|
+
constructor(lineStart: number, column: number, errorType: string, line: number, codeLine: string, errorIndicator: string, message: string, additionalLine1?: string | undefined, additionalLine2?: string | undefined);
|
|
12
|
+
reformat(line: number, file: string): string;
|
|
13
|
+
}
|
|
14
|
+
export declare function parseError(error: string): CompilationError | undefined;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.parseError = void 0;
|
|
21
|
+
class CompilationError {
|
|
22
|
+
constructor(lineStart, column, errorType, line, codeLine, errorIndicator, message, additionalLine1, additionalLine2) {
|
|
23
|
+
this.lineStart = lineStart;
|
|
24
|
+
this.column = column;
|
|
25
|
+
this.errorType = errorType;
|
|
26
|
+
this.line = line;
|
|
27
|
+
this.codeLine = codeLine;
|
|
28
|
+
this.errorIndicator = errorIndicator;
|
|
29
|
+
this.message = message;
|
|
30
|
+
this.additionalLine1 = additionalLine1;
|
|
31
|
+
this.additionalLine2 = additionalLine2;
|
|
32
|
+
}
|
|
33
|
+
reformat(line, file) {
|
|
34
|
+
const spaces = `${line}`.replace(/\d/g, ' ');
|
|
35
|
+
const newError = `${file} (${line}:${this.column}): ${this.errorType}
|
|
36
|
+
${line} |${this.codeLine}
|
|
37
|
+
${spaces} |${this.errorIndicator}
|
|
38
|
+
${spaces} |${this.message}`;
|
|
39
|
+
if (this.additionalLine1 && this.additionalLine2) {
|
|
40
|
+
return `${newError}\n${spaces} |${this.additionalLine1}\n${spaces} |${this.additionalLine2}`;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return newError;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const errorRegex = /error \((\d+):(\d+)\):\s*(.*)\n\s*(\d+)\s*\|(.*)\n.*\|(.*)\n\s*\|(.*)(?:\n\s*\|(.*)\n\s*\|(.*))?/;
|
|
48
|
+
function parseError(error) {
|
|
49
|
+
const match = error.match(errorRegex);
|
|
50
|
+
if (match) {
|
|
51
|
+
const lineStart = parseInt(match[1]);
|
|
52
|
+
const column = parseInt(match[2]);
|
|
53
|
+
const errorType = match[3];
|
|
54
|
+
const line = parseInt(match[4]);
|
|
55
|
+
const codeLine = match[5];
|
|
56
|
+
const errorIndicator = match[6];
|
|
57
|
+
const message = match[7];
|
|
58
|
+
const additionalLine1 = match[8];
|
|
59
|
+
const additionalLine2 = match[9];
|
|
60
|
+
return new CompilationError(lineStart, column, errorType, line, codeLine, errorIndicator, message, additionalLine1, additionalLine2);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.parseError = parseError;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ec as EC, SignatureInput } from 'elliptic';
|
|
2
2
|
import BN from 'bn.js';
|
|
3
|
-
import { KeyType } from '../signer';
|
|
4
3
|
import { HexString } from '../contract';
|
|
5
4
|
export declare const networkIds: readonly ["mainnet", "testnet", "devnet"];
|
|
6
5
|
export type NetworkId = (typeof networkIds)[number];
|
|
@@ -10,28 +9,10 @@ export declare function encodeSignature(signature: EC.Signature | {
|
|
|
10
9
|
}): string;
|
|
11
10
|
export declare function encodeHexSignature(rHex: string, sHex: string): string;
|
|
12
11
|
export declare function signatureDecode(ec: EC, signature: string): SignatureInput;
|
|
13
|
-
export declare function xorByte(intValue: number): number;
|
|
14
12
|
export declare function isHexString(input: string): boolean;
|
|
15
13
|
export declare function toNonNegativeBigInt(input: string): bigint | undefined;
|
|
16
|
-
export declare enum AddressType {
|
|
17
|
-
P2PKH = 0,
|
|
18
|
-
P2MPKH = 1,
|
|
19
|
-
P2SH = 2,
|
|
20
|
-
P2C = 3
|
|
21
|
-
}
|
|
22
|
-
export declare function groupOfAddress(address: string): number;
|
|
23
|
-
export declare function contractIdFromAddress(address: string): Uint8Array;
|
|
24
|
-
export declare function tokenIdFromAddress(address: string): Uint8Array;
|
|
25
14
|
export declare function hexToBinUnsafe(hex: string): Uint8Array;
|
|
26
15
|
export declare function binToHex(bin: Uint8Array): string;
|
|
27
|
-
export declare function groupOfPrivateKey(privateKey: string, keyType?: KeyType): number;
|
|
28
|
-
export declare function publicKeyFromPrivateKey(privateKey: string, _keyType?: KeyType): string;
|
|
29
|
-
export declare function addressFromPublicKey(publicKey: string, _keyType?: KeyType): string;
|
|
30
|
-
export declare function addressFromScript(script: Uint8Array): string;
|
|
31
|
-
export declare function addressFromContractId(contractId: string): string;
|
|
32
|
-
export declare function addressFromTokenId(tokenId: string): string;
|
|
33
|
-
export declare function contractIdFromTx(txId: string, outputIndex: number): string;
|
|
34
|
-
export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
|
|
35
16
|
export declare function blockChainIndex(blockHash: HexString): {
|
|
36
17
|
fromGroup: number;
|
|
37
18
|
toGroup: number;
|