@alephium/web3 1.7.2 → 1.7.3

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.
@@ -1,4 +1,5 @@
1
1
  import { KeyType } from '../signer';
2
+ import { LockupScript } from '../codec/lockup-script-codec';
2
3
  export declare enum AddressType {
3
4
  P2PKH = 0,
4
5
  P2MPKH = 1,
@@ -20,4 +21,4 @@ export declare function addressFromContractId(contractId: string): string;
20
21
  export declare function addressFromTokenId(tokenId: string): string;
21
22
  export declare function contractIdFromTx(txId: string, outputIndex: number): string;
22
23
  export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
23
- export declare function xorByte(intValue: number): number;
24
+ export declare function groupOfLockupScript(lockupScript: LockupScript): number;
@@ -20,16 +20,16 @@ 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.xorByte = exports.subContractId = exports.contractIdFromTx = exports.addressFromTokenId = exports.addressFromContractId = exports.addressFromScript = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isContractAddress = exports.isAssetAddress = exports.isValidAddress = exports.validateAddress = exports.AddressType = void 0;
23
+ exports.groupOfLockupScript = exports.subContractId = exports.contractIdFromTx = exports.addressFromTokenId = exports.addressFromContractId = exports.addressFromScript = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isContractAddress = exports.isAssetAddress = exports.isValidAddress = exports.validateAddress = exports.AddressType = void 0;
24
24
  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
28
  const bs58_1 = __importDefault(require("../utils/bs58"));
29
- const djb2_1 = __importDefault(require("../utils/djb2"));
30
29
  const utils_1 = require("../utils");
31
30
  const lockup_script_codec_1 = require("../codec/lockup-script-codec");
32
31
  const codec_1 = require("../codec");
32
+ const djb2_1 = __importDefault(require("../utils/djb2"));
33
33
  const ec = new elliptic_1.ec('secp256k1');
34
34
  const PublicKeyHashSize = 32;
35
35
  var AddressType;
@@ -120,23 +120,17 @@ function groupOfAddress(address) {
120
120
  }
121
121
  }
122
122
  exports.groupOfAddress = groupOfAddress;
123
- function groupOfAddressBytes(bytes) {
124
- const hint = (0, djb2_1.default)(bytes) | 1;
125
- const hash = xorByte(hint);
126
- const group = hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
127
- return group;
128
- }
129
123
  // Pay to public key hash address
130
124
  function groupOfP2pkhAddress(address) {
131
- return groupOfAddressBytes(address);
125
+ return groupFromBytesForAssetAddress(address);
132
126
  }
133
127
  // Pay to multiple public key hash address
134
128
  function groupOfP2mpkhAddress(address) {
135
- return groupOfAddressBytes(address.slice(1, 33));
129
+ return groupFromBytesForAssetAddress(address.slice(1, 33));
136
130
  }
137
131
  // Pay to script hash address
138
132
  function groupOfP2shAddress(address) {
139
- return groupOfAddressBytes(address);
133
+ return groupFromBytesForAssetAddress(address);
140
134
  }
141
135
  function contractIdFromAddress(address) {
142
136
  return idFromAddress(address);
@@ -228,11 +222,25 @@ function subContractId(parentContractId, pathInHex, group) {
228
222
  return (0, utils_1.binToHex)(bytes);
229
223
  }
230
224
  exports.subContractId = subContractId;
231
- function xorByte(intValue) {
232
- const byte0 = (intValue >> 24) & 0xff;
233
- const byte1 = (intValue >> 16) & 0xff;
234
- const byte2 = (intValue >> 8) & 0xff;
235
- const byte3 = intValue & 0xff;
236
- return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
237
- }
238
- exports.xorByte = xorByte;
225
+ function groupOfLockupScript(lockupScript) {
226
+ if (lockupScript.kind === 'P2PKH') {
227
+ return groupFromBytesForAssetAddress(lockupScript.value);
228
+ }
229
+ else if (lockupScript.kind === 'P2MPKH') {
230
+ return groupFromBytesForAssetAddress(lockupScript.value.publicKeyHashes[0]);
231
+ }
232
+ else if (lockupScript.kind === 'P2SH') {
233
+ return groupFromBytesForAssetAddress(lockupScript.value);
234
+ }
235
+ else {
236
+ // P2C
237
+ const contractId = lockupScript.value;
238
+ return contractId[`${contractId.length - 1}`];
239
+ }
240
+ }
241
+ exports.groupOfLockupScript = groupOfLockupScript;
242
+ function groupFromBytesForAssetAddress(bytes) {
243
+ const hint = (0, djb2_1.default)(bytes) | 1;
244
+ const hash = (0, utils_1.xorByte)(hint);
245
+ return hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
246
+ }
@@ -8,5 +8,5 @@ export declare abstract class TransactionBuilder {
8
8
  buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, 'signature'>>;
9
9
  buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
10
10
  buildExecuteScriptTx(params: SignExecuteScriptTxParams, publicKey: string): Promise<Omit<SignExecuteScriptTxResult, 'signature'>>;
11
- buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, 'signature'>>;
11
+ buildUnsignedTx(params: SignUnsignedTxParams): Omit<SignUnsignedTxResult, 'signature'>;
12
12
  }
@@ -22,6 +22,9 @@ const utils_1 = require("../utils");
22
22
  const api_1 = require("../api");
23
23
  const address_1 = require("../address");
24
24
  const signer_1 = require("./signer");
25
+ const codec_1 = require("../codec");
26
+ const transaction_1 = require("../transaction");
27
+ const hash_1 = require("../codec/hash");
25
28
  class TransactionBuilder {
26
29
  static from(param0, param1, customFetch) {
27
30
  const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1, customFetch) : param0;
@@ -80,16 +83,18 @@ class TransactionBuilder {
80
83
  const response = await this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data);
81
84
  return { ...response, groupIndex: response.fromGroup, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
82
85
  }
83
- async buildUnsignedTx(params) {
84
- const data = { unsignedTx: params.unsignedTx };
85
- const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data);
86
+ buildUnsignedTx(params) {
87
+ const unsignedTxBin = (0, utils_1.hexToBinUnsafe)(params.unsignedTx);
88
+ const decoded = codec_1.unsignedTxCodec.decode(unsignedTxBin);
89
+ const txId = (0, utils_1.binToHex)((0, hash_1.blakeHash)(unsignedTxBin));
90
+ const [fromGroup, toGroup] = (0, transaction_1.groupIndexOfTransaction)(decoded);
86
91
  return {
87
- fromGroup: decoded.fromGroup,
88
- toGroup: decoded.toGroup,
92
+ fromGroup: fromGroup,
93
+ toGroup: toGroup,
89
94
  unsignedTx: params.unsignedTx,
90
- txId: decoded.unsignedTx.txId,
91
- gasAmount: decoded.unsignedTx.gasAmount,
92
- gasPrice: (0, api_1.fromApiNumber256)(decoded.unsignedTx.gasPrice)
95
+ txId: txId,
96
+ gasAmount: decoded.gasAmount,
97
+ gasPrice: decoded.gasPrice
93
98
  };
94
99
  }
95
100
  }
@@ -1,2 +1,4 @@
1
1
  import { node } from '../api';
2
+ import { UnsignedTx } from '../codec';
2
3
  export declare function waitForTxConfirmation(txId: string, confirmations: number, requestInterval: number): Promise<node.Confirmed>;
4
+ export declare function groupIndexOfTransaction(unsignedTx: UnsignedTx): [number, number];
@@ -17,8 +17,11 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.waitForTxConfirmation = void 0;
20
+ exports.groupIndexOfTransaction = exports.waitForTxConfirmation = void 0;
21
+ const address_1 = require("../address");
22
+ const constants_1 = require("../constants");
21
23
  const global_1 = require("../global");
24
+ const utils_1 = require("../utils");
22
25
  function isConfirmed(txStatus) {
23
26
  return txStatus.type === 'Confirmed';
24
27
  }
@@ -32,3 +35,22 @@ async function waitForTxConfirmation(txId, confirmations, requestInterval) {
32
35
  return waitForTxConfirmation(txId, confirmations, requestInterval);
33
36
  }
34
37
  exports.waitForTxConfirmation = waitForTxConfirmation;
38
+ function groupIndexOfTransaction(unsignedTx) {
39
+ if (unsignedTx.inputs.length === 0)
40
+ throw new Error('Empty inputs for unsignedTx');
41
+ const fromGroup = groupFromHint(unsignedTx.inputs[0].hint);
42
+ let toGroup = fromGroup;
43
+ for (const output of unsignedTx.fixedOutputs) {
44
+ const outputGroup = (0, address_1.groupOfLockupScript)(output.lockupScript);
45
+ if (outputGroup !== fromGroup) {
46
+ toGroup = outputGroup;
47
+ break;
48
+ }
49
+ }
50
+ return [fromGroup, toGroup];
51
+ }
52
+ exports.groupIndexOfTransaction = groupIndexOfTransaction;
53
+ function groupFromHint(hint) {
54
+ const hash = (0, utils_1.xorByte)(hint);
55
+ return hash % constants_1.TOTAL_NUMBER_OF_GROUPS;
56
+ }
@@ -24,6 +24,7 @@ export declare function isDevnet(networkId?: number): boolean;
24
24
  export declare function targetToDifficulty(compactedTarget: HexString): bigint;
25
25
  export declare function difficultyToTarget(diff: bigint): HexString;
26
26
  export declare function concatBytes(arrays: Uint8Array[]): Uint8Array;
27
+ export declare function xorByte(intValue: number): number;
27
28
  type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
28
29
  export type Eq<X, Y> = _Eq<{
29
30
  [P in keyof X]: X[P];
@@ -20,7 +20,7 @@ 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.assertType = exports.concatBytes = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
23
+ exports.assertType = exports.xorByte = exports.concatBytes = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
24
24
  const elliptic_1 = require("elliptic");
25
25
  const bn_js_1 = __importDefault(require("bn.js"));
26
26
  const constants_1 = require("../constants");
@@ -153,6 +153,14 @@ function concatBytes(arrays) {
153
153
  return result;
154
154
  }
155
155
  exports.concatBytes = concatBytes;
156
+ function xorByte(intValue) {
157
+ const byte0 = (intValue >> 24) & 0xff;
158
+ const byte1 = (intValue >> 16) & 0xff;
159
+ const byte2 = (intValue >> 8) & 0xff;
160
+ const byte3 = intValue & 0xff;
161
+ return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
162
+ }
163
+ exports.xorByte = xorByte;
156
164
  // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
157
165
  function assertType() { }
158
166
  exports.assertType = assertType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -83,7 +83,7 @@
83
83
  "ts-node": "^10.9.1",
84
84
  "tslib": "^2.5.0",
85
85
  "typescript": "^4.9.5",
86
- "webpack": "^5.77.0",
86
+ "webpack": "^5.94.0",
87
87
  "webpack-cli": "^4.10.0"
88
88
  },
89
89
  "engines": {
@@ -21,11 +21,12 @@ import BN from 'bn.js'
21
21
  import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
22
22
  import blake from 'blakejs'
23
23
  import bs58 from '../utils/bs58'
24
- import djb2 from '../utils/djb2'
25
- import { binToHex, concatBytes, hexToBinUnsafe, isHexString } from '../utils'
24
+ import { binToHex, concatBytes, hexToBinUnsafe, isHexString, xorByte } from '../utils'
26
25
  import { KeyType } from '../signer'
27
26
  import { P2MPKH, lockupScriptCodec } from '../codec/lockup-script-codec'
28
27
  import { i32Codec } from '../codec'
28
+ import { LockupScript } from '../codec/lockup-script-codec'
29
+ import djb2 from '../utils/djb2'
29
30
 
30
31
  const ec = new EC('secp256k1')
31
32
  const PublicKeyHashSize = 32
@@ -112,26 +113,19 @@ export function groupOfAddress(address: string): number {
112
113
  }
113
114
  }
114
115
 
115
- function groupOfAddressBytes(bytes: Uint8Array): number {
116
- const hint = djb2(bytes) | 1
117
- const hash = xorByte(hint)
118
- const group = hash % TOTAL_NUMBER_OF_GROUPS
119
- return group
120
- }
121
-
122
116
  // Pay to public key hash address
123
117
  function groupOfP2pkhAddress(address: Uint8Array): number {
124
- return groupOfAddressBytes(address)
118
+ return groupFromBytesForAssetAddress(address)
125
119
  }
126
120
 
127
121
  // Pay to multiple public key hash address
128
122
  function groupOfP2mpkhAddress(address: Uint8Array): number {
129
- return groupOfAddressBytes(address.slice(1, 33))
123
+ return groupFromBytesForAssetAddress(address.slice(1, 33))
130
124
  }
131
125
 
132
126
  // Pay to script hash address
133
127
  function groupOfP2shAddress(address: Uint8Array): number {
134
- return groupOfAddressBytes(address)
128
+ return groupFromBytesForAssetAddress(address)
135
129
  }
136
130
 
137
131
  export function contractIdFromAddress(address: string): Uint8Array {
@@ -225,10 +219,22 @@ export function subContractId(parentContractId: string, pathInHex: string, group
225
219
  return binToHex(bytes)
226
220
  }
227
221
 
228
- export function xorByte(intValue: number): number {
229
- const byte0 = (intValue >> 24) & 0xff
230
- const byte1 = (intValue >> 16) & 0xff
231
- const byte2 = (intValue >> 8) & 0xff
232
- const byte3 = intValue & 0xff
233
- return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff
222
+ export function groupOfLockupScript(lockupScript: LockupScript): number {
223
+ if (lockupScript.kind === 'P2PKH') {
224
+ return groupFromBytesForAssetAddress(lockupScript.value)
225
+ } else if (lockupScript.kind === 'P2MPKH') {
226
+ return groupFromBytesForAssetAddress(lockupScript.value.publicKeyHashes[0])
227
+ } else if (lockupScript.kind === 'P2SH') {
228
+ return groupFromBytesForAssetAddress(lockupScript.value)
229
+ } else {
230
+ // P2C
231
+ const contractId = lockupScript.value
232
+ return contractId[`${contractId.length - 1}`]
233
+ }
234
+ }
235
+
236
+ function groupFromBytesForAssetAddress(bytes: Uint8Array): number {
237
+ const hint = djb2(bytes) | 1
238
+ const hash = xorByte(hint)
239
+ return hash % TOTAL_NUMBER_OF_GROUPS
234
240
  }
@@ -16,7 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { binToHex } from '../utils'
19
+ import { binToHex, hexToBinUnsafe } from '../utils'
20
20
  import { fromApiNumber256, node, NodeProvider, toApiNumber256Optional, toApiTokens } from '../api'
21
21
  import { addressFromPublicKey, contractIdFromAddress } from '../address'
22
22
  import { toApiDestinations } from './signer'
@@ -32,6 +32,9 @@ import {
32
32
  SignUnsignedTxParams,
33
33
  SignUnsignedTxResult
34
34
  } from './types'
35
+ import { unsignedTxCodec, UnsignedTxCodec } from '../codec'
36
+ import { groupIndexOfTransaction } from '../transaction'
37
+ import { blakeHash } from '../codec/hash'
35
38
 
36
39
  export abstract class TransactionBuilder {
37
40
  abstract get nodeProvider(): NodeProvider
@@ -113,16 +116,18 @@ export abstract class TransactionBuilder {
113
116
  return { ...response, groupIndex: response.fromGroup, gasPrice: fromApiNumber256(response.gasPrice) }
114
117
  }
115
118
 
116
- async buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, 'signature'>> {
117
- const data = { unsignedTx: params.unsignedTx }
118
- const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data)
119
+ buildUnsignedTx(params: SignUnsignedTxParams): Omit<SignUnsignedTxResult, 'signature'> {
120
+ const unsignedTxBin = hexToBinUnsafe(params.unsignedTx)
121
+ const decoded = unsignedTxCodec.decode(unsignedTxBin)
122
+ const txId = binToHex(blakeHash(unsignedTxBin))
123
+ const [fromGroup, toGroup] = groupIndexOfTransaction(decoded)
119
124
  return {
120
- fromGroup: decoded.fromGroup,
121
- toGroup: decoded.toGroup,
125
+ fromGroup: fromGroup,
126
+ toGroup: toGroup,
122
127
  unsignedTx: params.unsignedTx,
123
- txId: decoded.unsignedTx.txId,
124
- gasAmount: decoded.unsignedTx.gasAmount,
125
- gasPrice: fromApiNumber256(decoded.unsignedTx.gasPrice)
128
+ txId: txId,
129
+ gasAmount: decoded.gasAmount,
130
+ gasPrice: decoded.gasPrice
126
131
  }
127
132
  }
128
133
  }
@@ -16,8 +16,12 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
+ import { groupOfLockupScript } from '../address'
19
20
  import { node } from '../api'
21
+ import { UnsignedTx } from '../codec'
22
+ import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
20
23
  import { getCurrentNodeProvider } from '../global'
24
+ import { xorByte } from '../utils'
21
25
 
22
26
  function isConfirmed(txStatus: node.TxStatus): txStatus is node.Confirmed {
23
27
  return txStatus.type === 'Confirmed'
@@ -36,3 +40,25 @@ export async function waitForTxConfirmation(
36
40
  await new Promise((r) => setTimeout(r, requestInterval))
37
41
  return waitForTxConfirmation(txId, confirmations, requestInterval)
38
42
  }
43
+
44
+ export function groupIndexOfTransaction(unsignedTx: UnsignedTx): [number, number] {
45
+ if (unsignedTx.inputs.length === 0) throw new Error('Empty inputs for unsignedTx')
46
+
47
+ const fromGroup = groupFromHint(unsignedTx.inputs[0].hint)
48
+
49
+ let toGroup = fromGroup
50
+ for (const output of unsignedTx.fixedOutputs) {
51
+ const outputGroup = groupOfLockupScript(output.lockupScript)
52
+ if (outputGroup !== fromGroup) {
53
+ toGroup = outputGroup
54
+ break
55
+ }
56
+ }
57
+
58
+ return [fromGroup, toGroup]
59
+ }
60
+
61
+ function groupFromHint(hint: number): number {
62
+ const hash = xorByte(hint)
63
+ return hash % TOTAL_NUMBER_OF_GROUPS
64
+ }
@@ -156,6 +156,14 @@ export function concatBytes(arrays: Uint8Array[]): Uint8Array {
156
156
  return result
157
157
  }
158
158
 
159
+ export function xorByte(intValue: number): number {
160
+ const byte0 = (intValue >> 24) & 0xff
161
+ const byte1 = (intValue >> 16) & 0xff
162
+ const byte2 = (intValue >> 8) & 0xff
163
+ const byte3 = intValue & 0xff
164
+ return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff
165
+ }
166
+
159
167
  type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
160
168
  export type Eq<X, Y> = _Eq<{ [P in keyof X]: X[P] }, { [P in keyof Y]: Y[P] }>
161
169
  // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars