@alephium/web3 0.26.0 → 0.27.1

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,7 +1,11 @@
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 isDepositALPHTransaction(tx: Transaction, exchangeAddress: string): boolean;
5
- export declare function isDepositTokenTransaction(tx: Transaction, exchangeAddress: string): boolean;
6
- export declare function getDepositAddress(tx: Transaction): Address;
4
+ export declare function isSimpleALPHTransferTx(tx: Transaction): boolean;
5
+ export declare function isSimpleTransferTokenTx(tx: Transaction): boolean;
6
+ export declare function getALPHDepositInfo(tx: Transaction): {
7
+ targetAddress: Address;
8
+ depositAmount: bigint;
9
+ };
10
+ export declare function getSenderAddress(tx: Transaction): Address;
7
11
  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.getDepositAddress = exports.isDepositTokenTransaction = exports.isDepositALPHTransaction = exports.validateExchangeAddress = void 0;
20
+ exports.getAddressFromUnlockScript = exports.getSenderAddress = exports.getALPHDepositInfo = exports.isSimpleTransferTokenTx = exports.isSimpleALPHTransferTx = exports.validateExchangeAddress = void 0;
21
21
  const __1 = require("..");
22
22
  function validateExchangeAddress(address) {
23
23
  let decoded;
@@ -38,19 +38,38 @@ function validateExchangeAddress(address) {
38
38
  }
39
39
  }
40
40
  exports.validateExchangeAddress = validateExchangeAddress;
41
- function isDepositALPHTransaction(tx, exchangeAddress) {
42
- return isDepositTransaction(tx, exchangeAddress) && checkALPHOutput(tx);
41
+ function isSimpleALPHTransferTx(tx) {
42
+ return isSimpleTransferTx(tx) && checkALPHOutput(tx);
43
43
  }
44
- exports.isDepositALPHTransaction = isDepositALPHTransaction;
45
- function isDepositTokenTransaction(tx, exchangeAddress) {
46
- return isDepositTransaction(tx, exchangeAddress) && checkTokenOutput(tx, exchangeAddress);
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
56
+ function getALPHDepositInfo(tx) {
57
+ const senderAddress = getSenderAddress(tx);
58
+ const targetAddress = tx.unsigned.fixedOutputs.find((o) => o.address !== senderAddress).address;
59
+ let depositAmount = 0n;
60
+ tx.unsigned.fixedOutputs.forEach((o) => {
61
+ if (o.address === targetAddress) {
62
+ depositAmount += BigInt(o.attoAlphAmount);
63
+ }
64
+ });
65
+ return { targetAddress, depositAmount };
47
66
  }
48
- exports.isDepositTokenTransaction = isDepositTokenTransaction;
49
- // we assume that the tx is deposit transaction
50
- function getDepositAddress(tx) {
67
+ exports.getALPHDepositInfo = getALPHDepositInfo;
68
+ // we assume that the tx is a simple transfer tx, i.e. isSimpleTransferALPHTx(tx) == true
69
+ function getSenderAddress(tx) {
51
70
  return getAddressFromUnlockScript(tx.unsigned.inputs[0].unlockScript);
52
71
  }
53
- exports.getDepositAddress = getDepositAddress;
72
+ exports.getSenderAddress = getSenderAddress;
54
73
  var UnlockScriptType;
55
74
  (function (UnlockScriptType) {
56
75
  UnlockScriptType[UnlockScriptType["P2PKH"] = 0] = "P2PKH";
@@ -81,50 +100,47 @@ function getAddressFromUnlockScript(unlockScript) {
81
100
  }
82
101
  }
83
102
  exports.getAddressFromUnlockScript = getAddressFromUnlockScript;
84
- function getFromAddress(tx) {
103
+ function getSenderAddressAnyTx(tx) {
85
104
  try {
86
105
  const inputAddresses = tx.unsigned.inputs.map((i) => getAddressFromUnlockScript(i.unlockScript));
87
106
  // we have checked that the inputs is not empty
88
- const from = inputAddresses[0];
89
- return inputAddresses.slice(1).every((addr) => addr === from) ? from : undefined;
107
+ const sender = inputAddresses[0];
108
+ return inputAddresses.slice(1).every((addr) => addr === sender) ? sender : undefined;
90
109
  }
91
110
  catch (_) {
92
111
  return undefined;
93
112
  }
94
113
  }
95
- function checkOutputAddress(tx, from, to) {
96
- let fromCount = 0;
97
- let toCount = 0;
98
- tx.unsigned.fixedOutputs.forEach((o) => {
99
- if (o.address === from) {
100
- fromCount += 1;
101
- }
102
- else if (o.address === to) {
103
- toCount += 1;
104
- }
105
- });
106
- const outputCount = tx.unsigned.fixedOutputs.length;
107
- return toCount === 1 && fromCount === outputCount - 1;
108
- }
109
114
  function checkALPHOutput(tx) {
110
115
  const outputs = tx.unsigned.fixedOutputs;
111
116
  return outputs.every((o) => o.tokens.length === 0);
112
117
  }
113
118
  function checkTokenOutput(tx, to) {
114
119
  // we have checked the output address
115
- const output = tx.unsigned.fixedOutputs.find((o) => o.address === to);
116
- return output.attoAlphAmount === __1.DUST_AMOUNT.toString() && output.tokens.length === 1;
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);
117
126
  }
118
- function isDepositTransaction(tx, exchangeAddress) {
127
+ function isSimpleTransferTx(tx) {
119
128
  if (tx.contractInputs.length !== 0 ||
120
129
  tx.generatedOutputs.length !== 0 ||
121
130
  tx.unsigned.inputs.length === 0 ||
122
131
  tx.unsigned.scriptOpt !== undefined) {
123
132
  return false;
124
133
  }
125
- const from = getFromAddress(tx);
126
- if (from === undefined || from === exchangeAddress) {
134
+ const sender = getSenderAddressAnyTx(tx);
135
+ if (sender === undefined) {
127
136
  return false;
128
137
  }
129
- return checkOutputAddress(tx, from, exchangeAddress);
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)));
130
146
  }
@@ -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, isDepositALPHTransaction, isDepositTokenTransaction, getDepositAddress } from './exchange';
9
+ export { validateExchangeAddress, isSimpleALPHTransferTx, isSimpleTransferTokenTx, 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.getDepositAddress = exports.isDepositTokenTransaction = exports.isDepositALPHTransaction = exports.validateExchangeAddress = void 0;
34
+ exports.getALPHDepositInfo = exports.getSenderAddress = exports.isSimpleTransferTokenTx = exports.isSimpleALPHTransferTx = exports.validateExchangeAddress = void 0;
35
35
  __exportStar(require("./webcrypto"), exports);
36
36
  __exportStar(require("./address"), exports);
37
37
  __exportStar(require("./bs58"), exports);
@@ -42,6 +42,7 @@ __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, "isDepositALPHTransaction", { enumerable: true, get: function () { return exchange_1.isDepositALPHTransaction; } });
46
- Object.defineProperty(exports, "isDepositTokenTransaction", { enumerable: true, get: function () { return exchange_1.isDepositTokenTransaction; } });
47
- Object.defineProperty(exports, "getDepositAddress", { enumerable: true, get: function () { return exchange_1.getDepositAddress; } });
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; } });
47
+ Object.defineProperty(exports, "getSenderAddress", { enumerable: true, get: function () { return exchange_1.getSenderAddress; } });
48
+ 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.26.0",
3
+ "version": "0.27.1",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -37,16 +37,35 @@ export function validateExchangeAddress(address: string) {
37
37
  }
38
38
  }
39
39
 
40
- export function isDepositALPHTransaction(tx: Transaction, exchangeAddress: string): boolean {
41
- return isDepositTransaction(tx, exchangeAddress) && checkALPHOutput(tx)
40
+ export function isSimpleALPHTransferTx(tx: Transaction): boolean {
41
+ return isSimpleTransferTx(tx) && checkALPHOutput(tx)
42
42
  }
43
43
 
44
- export function isDepositTokenTransaction(tx: Transaction, exchangeAddress: string): boolean {
45
- return isDepositTransaction(tx, exchangeAddress) && checkTokenOutput(tx, exchangeAddress)
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)
50
+ }
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
59
+ tx.unsigned.fixedOutputs.forEach((o) => {
60
+ if (o.address === targetAddress) {
61
+ depositAmount += BigInt(o.attoAlphAmount)
62
+ }
63
+ })
64
+ return { targetAddress, depositAmount }
46
65
  }
47
66
 
48
- // we assume that the tx is deposit transaction
49
- export function getDepositAddress(tx: Transaction): Address {
67
+ // we assume that the tx is a simple transfer tx, i.e. isSimpleTransferALPHTx(tx) == true
68
+ export function getSenderAddress(tx: Transaction): Address {
50
69
  return getAddressFromUnlockScript(tx.unsigned.inputs[0].unlockScript)
51
70
  }
52
71
 
@@ -77,31 +96,17 @@ export function getAddressFromUnlockScript(unlockScript: string): Address {
77
96
  }
78
97
  }
79
98
 
80
- function getFromAddress(tx: Transaction): Address | undefined {
99
+ function getSenderAddressAnyTx(tx: Transaction): Address | undefined {
81
100
  try {
82
101
  const inputAddresses = tx.unsigned.inputs.map((i) => getAddressFromUnlockScript(i.unlockScript))
83
102
  // we have checked that the inputs is not empty
84
- const from = inputAddresses[0]
85
- return inputAddresses.slice(1).every((addr) => addr === from) ? from : undefined
103
+ const sender = inputAddresses[0]
104
+ return inputAddresses.slice(1).every((addr) => addr === sender) ? sender : undefined
86
105
  } catch (_) {
87
106
  return undefined
88
107
  }
89
108
  }
90
109
 
91
- function checkOutputAddress(tx: Transaction, from: Address, to: Address): boolean {
92
- let fromCount = 0
93
- let toCount = 0
94
- tx.unsigned.fixedOutputs.forEach((o) => {
95
- if (o.address === from) {
96
- fromCount += 1
97
- } else if (o.address === to) {
98
- toCount += 1
99
- }
100
- })
101
- const outputCount = tx.unsigned.fixedOutputs.length
102
- return toCount === 1 && fromCount === outputCount - 1
103
- }
104
-
105
110
  function checkALPHOutput(tx: Transaction): boolean {
106
111
  const outputs = tx.unsigned.fixedOutputs
107
112
  return outputs.every((o) => o.tokens.length === 0)
@@ -109,11 +114,17 @@ function checkALPHOutput(tx: Transaction): boolean {
109
114
 
110
115
  function checkTokenOutput(tx: Transaction, to: Address): boolean {
111
116
  // we have checked the output address
112
- const output = tx.unsigned.fixedOutputs.find((o) => o.address === to)!
113
- return output.attoAlphAmount === DUST_AMOUNT.toString() && output.tokens.length === 1
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
+ )
114
125
  }
115
126
 
116
- function isDepositTransaction(tx: Transaction, exchangeAddress: string): boolean {
127
+ function isSimpleTransferTx(tx: Transaction): boolean {
117
128
  if (
118
129
  tx.contractInputs.length !== 0 ||
119
130
  tx.generatedOutputs.length !== 0 ||
@@ -122,9 +133,18 @@ function isDepositTransaction(tx: Transaction, exchangeAddress: string): boolean
122
133
  ) {
123
134
  return false
124
135
  }
125
- const from = getFromAddress(tx)
126
- if (from === undefined || from === exchangeAddress) {
136
+ const sender = getSenderAddressAnyTx(tx)
137
+ if (sender === undefined) {
127
138
  return false
128
139
  }
129
- return checkOutputAddress(tx, from, exchangeAddress)
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
+ )
130
150
  }
@@ -26,7 +26,8 @@ export * from './sign'
26
26
  export * from './number'
27
27
  export {
28
28
  validateExchangeAddress,
29
- isDepositALPHTransaction,
30
- isDepositTokenTransaction,
31
- getDepositAddress
29
+ isSimpleALPHTransferTx,
30
+ isSimpleTransferTokenTx,
31
+ getSenderAddress,
32
+ getALPHDepositInfo
32
33
  } from './exchange'