@alephium/web3 0.27.4 → 0.28.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.
@@ -1,11 +1,10 @@
1
1
  import { Transaction } from '../api/api-alephium';
2
2
  import { Address } from '../signer';
3
3
  export declare function validateExchangeAddress(address: string): void;
4
- export declare function isSimpleALPHTransferTx(tx: Transaction): boolean;
5
- export declare function isSimpleTransferTokenTx(tx: Transaction): boolean;
4
+ export declare function isALPHTransferTx(tx: Transaction): boolean;
6
5
  export declare function getALPHDepositInfo(tx: Transaction): {
7
6
  targetAddress: Address;
8
7
  depositAmount: bigint;
9
- };
8
+ }[];
10
9
  export declare function getSenderAddress(tx: Transaction): Address;
11
10
  export declare function getAddressFromUnlockScript(unlockScript: string): Address;
@@ -17,7 +17,7 @@ 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.getAddressFromUnlockScript = exports.getSenderAddress = exports.getALPHDepositInfo = exports.isSimpleTransferTokenTx = exports.isSimpleALPHTransferTx = exports.validateExchangeAddress = void 0;
20
+ exports.getAddressFromUnlockScript = exports.getSenderAddress = exports.getALPHDepositInfo = exports.isALPHTransferTx = exports.validateExchangeAddress = void 0;
21
21
  const __1 = require("..");
22
22
  function validateExchangeAddress(address) {
23
23
  let decoded;
@@ -38,34 +38,40 @@ function validateExchangeAddress(address) {
38
38
  }
39
39
  }
40
40
  exports.validateExchangeAddress = validateExchangeAddress;
41
- function isSimpleALPHTransferTx(tx) {
42
- return isSimpleTransferTx(tx) && checkALPHOutput(tx);
41
+ function isALPHTransferTx(tx) {
42
+ return isTransferTx(tx) && checkALPHOutput(tx);
43
43
  }
44
- exports.isSimpleALPHTransferTx = isSimpleALPHTransferTx;
45
- function isSimpleTransferTokenTx(tx) {
46
- const isTransferTx = isSimpleTransferTx(tx);
47
- if (isTransferTx) {
48
- const senderAddress = getSenderAddress(tx);
49
- const targetAddress = tx.unsigned.fixedOutputs.find((o) => o.address !== senderAddress).address;
50
- return checkTokenOutput(tx, targetAddress);
51
- }
52
- return false;
53
- }
54
- exports.isSimpleTransferTokenTx = isSimpleTransferTokenTx;
55
- // we assume that the tx is a simple transfer tx, i.e. isSimpleTransferALPHTx(tx) == true
44
+ exports.isALPHTransferTx = isALPHTransferTx;
56
45
  function getALPHDepositInfo(tx) {
57
- const senderAddress = getSenderAddress(tx);
58
- const targetAddress = tx.unsigned.fixedOutputs.find((o) => o.address !== senderAddress).address;
59
- let depositAmount = 0n;
46
+ if (!isALPHTransferTx(tx)) {
47
+ return [];
48
+ }
49
+ const inputAddresses = [];
50
+ for (const input of tx.unsigned.inputs) {
51
+ try {
52
+ const address = getAddressFromUnlockScript(input.unlockScript);
53
+ if (!inputAddresses.includes(address)) {
54
+ inputAddresses.push(address);
55
+ }
56
+ }
57
+ catch (_) { }
58
+ }
59
+ const result = new Map();
60
60
  tx.unsigned.fixedOutputs.forEach((o) => {
61
- if (o.address === targetAddress) {
62
- depositAmount += BigInt(o.attoAlphAmount);
61
+ if (!inputAddresses.includes(o.address)) {
62
+ const amount = result.get(o.address);
63
+ if (amount === undefined) {
64
+ result.set(o.address, BigInt(o.attoAlphAmount));
65
+ }
66
+ else {
67
+ result.set(o.address, BigInt(o.attoAlphAmount) + amount);
68
+ }
63
69
  }
64
70
  });
65
- return { targetAddress, depositAmount };
71
+ return Array.from(result.entries()).map(([key, value]) => ({ targetAddress: key, depositAmount: value }));
66
72
  }
67
73
  exports.getALPHDepositInfo = getALPHDepositInfo;
68
- // we assume that the tx is a simple transfer tx, i.e. isSimpleTransferALPHTx(tx) == true
74
+ // we assume that the tx is a simple transfer tx, i.e. isSimpleALPHTransferTx(tx) == true
69
75
  function getSenderAddress(tx) {
70
76
  return getAddressFromUnlockScript(tx.unsigned.inputs[0].unlockScript);
71
77
  }
@@ -100,47 +106,16 @@ function getAddressFromUnlockScript(unlockScript) {
100
106
  }
101
107
  }
102
108
  exports.getAddressFromUnlockScript = getAddressFromUnlockScript;
103
- function getSenderAddressAnyTx(tx) {
104
- try {
105
- const inputAddresses = tx.unsigned.inputs.map((i) => getAddressFromUnlockScript(i.unlockScript));
106
- // we have checked that the inputs is not empty
107
- const sender = inputAddresses[0];
108
- return inputAddresses.slice(1).every((addr) => addr === sender) ? sender : undefined;
109
- }
110
- catch (_) {
111
- return undefined;
112
- }
113
- }
114
109
  function checkALPHOutput(tx) {
115
110
  const outputs = tx.unsigned.fixedOutputs;
116
111
  return outputs.every((o) => o.tokens.length === 0);
117
112
  }
118
- function checkTokenOutput(tx, to) {
119
- // we have checked the output address
120
- const outputs = tx.unsigned.fixedOutputs.filter((o) => o.address === to);
121
- if (outputs[0].tokens.length === 0) {
122
- return false;
123
- }
124
- const tokenId = outputs[0].tokens[0].id;
125
- return outputs.every((o) => BigInt(o.attoAlphAmount) === __1.DUST_AMOUNT && o.tokens.length === 1 && o.tokens[0].id === tokenId);
126
- }
127
- function isSimpleTransferTx(tx) {
113
+ function isTransferTx(tx) {
128
114
  if (tx.contractInputs.length !== 0 ||
129
115
  tx.generatedOutputs.length !== 0 ||
130
116
  tx.unsigned.inputs.length === 0 ||
131
117
  tx.unsigned.scriptOpt !== undefined) {
132
118
  return false;
133
119
  }
134
- const sender = getSenderAddressAnyTx(tx);
135
- if (sender === undefined) {
136
- return false;
137
- }
138
- const outputAddresses = [];
139
- tx.unsigned.fixedOutputs.forEach((o) => {
140
- if (!outputAddresses.includes(o.address)) {
141
- outputAddresses.push(o.address);
142
- }
143
- });
144
- return ((outputAddresses.length === 1 && outputAddresses[0] !== sender) ||
145
- (outputAddresses.length === 2 && outputAddresses.includes(sender)));
120
+ return true;
146
121
  }
@@ -6,4 +6,4 @@ export * from './utils';
6
6
  export * from './subscription';
7
7
  export * from './sign';
8
8
  export * from './number';
9
- export { validateExchangeAddress, isSimpleALPHTransferTx, isSimpleTransferTokenTx, getSenderAddress, getALPHDepositInfo } from './exchange';
9
+ export { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange';
@@ -31,7 +31,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
31
31
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.getALPHDepositInfo = exports.getSenderAddress = exports.isSimpleTransferTokenTx = exports.isSimpleALPHTransferTx = exports.validateExchangeAddress = void 0;
34
+ exports.getALPHDepositInfo = exports.getSenderAddress = exports.isALPHTransferTx = exports.validateExchangeAddress = void 0;
35
35
  __exportStar(require("./webcrypto"), exports);
36
36
  __exportStar(require("./address"), exports);
37
37
  __exportStar(require("./bs58"), exports);
@@ -42,7 +42,6 @@ __exportStar(require("./sign"), exports);
42
42
  __exportStar(require("./number"), exports);
43
43
  var exchange_1 = require("./exchange");
44
44
  Object.defineProperty(exports, "validateExchangeAddress", { enumerable: true, get: function () { return exchange_1.validateExchangeAddress; } });
45
- Object.defineProperty(exports, "isSimpleALPHTransferTx", { enumerable: true, get: function () { return exchange_1.isSimpleALPHTransferTx; } });
46
- Object.defineProperty(exports, "isSimpleTransferTokenTx", { enumerable: true, get: function () { return exchange_1.isSimpleTransferTokenTx; } });
45
+ Object.defineProperty(exports, "isALPHTransferTx", { enumerable: true, get: function () { return exchange_1.isALPHTransferTx; } });
47
46
  Object.defineProperty(exports, "getSenderAddress", { enumerable: true, get: function () { return exchange_1.getSenderAddress; } });
48
47
  Object.defineProperty(exports, "getALPHDepositInfo", { enumerable: true, get: function () { return exchange_1.getALPHDepositInfo; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.27.4",
3
+ "version": "0.28.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -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 { AddressType, DUST_AMOUNT, addressFromPublicKey, addressFromScript, binToHex, bs58, hexToBinUnsafe } from '..'
19
+ import { AddressType, addressFromPublicKey, addressFromScript, binToHex, bs58, hexToBinUnsafe } from '..'
20
20
  import { Transaction } from '../api/api-alephium'
21
21
  import { Address } from '../signer'
22
22
 
@@ -37,34 +37,38 @@ export function validateExchangeAddress(address: string) {
37
37
  }
38
38
  }
39
39
 
40
- export function isSimpleALPHTransferTx(tx: Transaction): boolean {
41
- return isSimpleTransferTx(tx) && checkALPHOutput(tx)
40
+ export function isALPHTransferTx(tx: Transaction): boolean {
41
+ return isTransferTx(tx) && checkALPHOutput(tx)
42
42
  }
43
43
 
44
- export function isSimpleTransferTokenTx(tx: Transaction): boolean {
45
- const isTransferTx = isSimpleTransferTx(tx)
46
- if (isTransferTx) {
47
- const senderAddress = getSenderAddress(tx)
48
- const targetAddress = tx.unsigned.fixedOutputs.find((o) => o.address !== senderAddress)!.address
49
- return checkTokenOutput(tx, targetAddress)
44
+ export function getALPHDepositInfo(tx: Transaction): { targetAddress: Address; depositAmount: bigint }[] {
45
+ if (!isALPHTransferTx(tx)) {
46
+ return []
50
47
  }
51
- return false
52
- }
53
-
54
- // we assume that the tx is a simple transfer tx, i.e. isSimpleTransferALPHTx(tx) == true
55
- export function getALPHDepositInfo(tx: Transaction): { targetAddress: Address; depositAmount: bigint } {
56
- const senderAddress = getSenderAddress(tx)
57
- const targetAddress = tx.unsigned.fixedOutputs.find((o) => o.address !== senderAddress)!.address
58
- let depositAmount = 0n
48
+ const inputAddresses: Address[] = []
49
+ for (const input of tx.unsigned.inputs) {
50
+ try {
51
+ const address = getAddressFromUnlockScript(input.unlockScript)
52
+ if (!inputAddresses.includes(address)) {
53
+ inputAddresses.push(address)
54
+ }
55
+ } catch (_) {}
56
+ }
57
+ const result = new Map<Address, bigint>()
59
58
  tx.unsigned.fixedOutputs.forEach((o) => {
60
- if (o.address === targetAddress) {
61
- depositAmount += BigInt(o.attoAlphAmount)
59
+ if (!inputAddresses.includes(o.address)) {
60
+ const amount = result.get(o.address)
61
+ if (amount === undefined) {
62
+ result.set(o.address, BigInt(o.attoAlphAmount))
63
+ } else {
64
+ result.set(o.address, BigInt(o.attoAlphAmount) + amount)
65
+ }
62
66
  }
63
67
  })
64
- return { targetAddress, depositAmount }
68
+ return Array.from(result.entries()).map(([key, value]) => ({ targetAddress: key, depositAmount: value }))
65
69
  }
66
70
 
67
- // we assume that the tx is a simple transfer tx, i.e. isSimpleTransferALPHTx(tx) == true
71
+ // we assume that the tx is a simple transfer tx, i.e. isSimpleALPHTransferTx(tx) == true
68
72
  export function getSenderAddress(tx: Transaction): Address {
69
73
  return getAddressFromUnlockScript(tx.unsigned.inputs[0].unlockScript)
70
74
  }
@@ -96,35 +100,12 @@ export function getAddressFromUnlockScript(unlockScript: string): Address {
96
100
  }
97
101
  }
98
102
 
99
- function getSenderAddressAnyTx(tx: Transaction): Address | undefined {
100
- try {
101
- const inputAddresses = tx.unsigned.inputs.map((i) => getAddressFromUnlockScript(i.unlockScript))
102
- // we have checked that the inputs is not empty
103
- const sender = inputAddresses[0]
104
- return inputAddresses.slice(1).every((addr) => addr === sender) ? sender : undefined
105
- } catch (_) {
106
- return undefined
107
- }
108
- }
109
-
110
103
  function checkALPHOutput(tx: Transaction): boolean {
111
104
  const outputs = tx.unsigned.fixedOutputs
112
105
  return outputs.every((o) => o.tokens.length === 0)
113
106
  }
114
107
 
115
- function checkTokenOutput(tx: Transaction, to: Address): boolean {
116
- // we have checked the output address
117
- const outputs = tx.unsigned.fixedOutputs.filter((o) => o.address === to)
118
- if (outputs[0].tokens.length === 0) {
119
- return false
120
- }
121
- const tokenId = outputs[0].tokens[0].id
122
- return outputs.every(
123
- (o) => BigInt(o.attoAlphAmount) === DUST_AMOUNT && o.tokens.length === 1 && o.tokens[0].id === tokenId
124
- )
125
- }
126
-
127
- function isSimpleTransferTx(tx: Transaction): boolean {
108
+ function isTransferTx(tx: Transaction): boolean {
128
109
  if (
129
110
  tx.contractInputs.length !== 0 ||
130
111
  tx.generatedOutputs.length !== 0 ||
@@ -133,18 +114,5 @@ function isSimpleTransferTx(tx: Transaction): boolean {
133
114
  ) {
134
115
  return false
135
116
  }
136
- const sender = getSenderAddressAnyTx(tx)
137
- if (sender === undefined) {
138
- return false
139
- }
140
- const outputAddresses: Address[] = []
141
- tx.unsigned.fixedOutputs.forEach((o) => {
142
- if (!outputAddresses.includes(o.address)) {
143
- outputAddresses.push(o.address)
144
- }
145
- })
146
- return (
147
- (outputAddresses.length === 1 && outputAddresses[0] !== sender) ||
148
- (outputAddresses.length === 2 && outputAddresses.includes(sender))
149
- )
117
+ return true
150
118
  }
@@ -24,10 +24,4 @@ export * from './utils'
24
24
  export * from './subscription'
25
25
  export * from './sign'
26
26
  export * from './number'
27
- export {
28
- validateExchangeAddress,
29
- isSimpleALPHTransferTx,
30
- isSimpleTransferTokenTx,
31
- getSenderAddress,
32
- getALPHDepositInfo
33
- } from './exchange'
27
+ export { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange'