@alephium/web3 0.26.0 → 0.27.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/exchange.d.ts +7 -3
- package/dist/src/utils/exchange.js +49 -33
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +5 -4
- package/package.json +1 -1
- package/src/utils/exchange.ts +49 -29
- package/src/utils/index.ts +4 -3
|
@@ -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
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
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.
|
|
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
|
|
42
|
-
return
|
|
41
|
+
function isSimpleALPHTransferTx(tx) {
|
|
42
|
+
return isSimpleTransferTx(tx) && checkALPHOutput(tx);
|
|
43
43
|
}
|
|
44
|
-
exports.
|
|
45
|
-
function
|
|
46
|
-
|
|
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.
|
|
49
|
-
// we assume that the tx is
|
|
50
|
-
function
|
|
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.
|
|
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
|
|
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
|
|
89
|
-
return inputAddresses.slice(1).every((addr) => addr ===
|
|
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
|
|
116
|
-
|
|
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
|
|
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
|
|
126
|
-
if (
|
|
134
|
+
const sender = getSenderAddressAnyTx(tx);
|
|
135
|
+
if (sender === undefined) {
|
|
127
136
|
return false;
|
|
128
137
|
}
|
|
129
|
-
|
|
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,
|
|
9
|
+
export { validateExchangeAddress, isSimpleALPHTransferTx, isSimpleTransferTokenTx, getSenderAddress, getALPHDepositInfo } from './exchange';
|
package/dist/src/utils/index.js
CHANGED
|
@@ -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.
|
|
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, "
|
|
46
|
-
Object.defineProperty(exports, "
|
|
47
|
-
Object.defineProperty(exports, "
|
|
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
package/src/utils/exchange.ts
CHANGED
|
@@ -37,16 +37,35 @@ export function validateExchangeAddress(address: string) {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
export function
|
|
41
|
-
return
|
|
40
|
+
export function isSimpleALPHTransferTx(tx: Transaction): boolean {
|
|
41
|
+
return isSimpleTransferTx(tx) && checkALPHOutput(tx)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export function
|
|
45
|
-
|
|
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
|
|
49
|
-
export function
|
|
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
|
|
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
|
|
85
|
-
return inputAddresses.slice(1).every((addr) => addr ===
|
|
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
|
|
113
|
-
|
|
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
|
|
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
|
|
126
|
-
if (
|
|
136
|
+
const sender = getSenderAddressAnyTx(tx)
|
|
137
|
+
if (sender === undefined) {
|
|
127
138
|
return false
|
|
128
139
|
}
|
|
129
|
-
|
|
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
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -26,7 +26,8 @@ export * from './sign'
|
|
|
26
26
|
export * from './number'
|
|
27
27
|
export {
|
|
28
28
|
validateExchangeAddress,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
isSimpleALPHTransferTx,
|
|
30
|
+
isSimpleTransferTokenTx,
|
|
31
|
+
getSenderAddress,
|
|
32
|
+
getALPHDepositInfo
|
|
32
33
|
} from './exchange'
|