pshenmic-dpp 2.0.0-dev.26 → 2.0.0-dev.27
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/binaries/bindingsTypes.d.ts +37 -0
- package/dist/binaries/native/aarch64-apple-darwin/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/aarch64-pc-windows-msvc/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/aarch64-unknown-linux-gnu/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/aarch64-unknown-linux-musl/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/x86_64-apple-darwin/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/x86_64-pc-windows-msvc/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/x86_64-unknown-linux-gnu/pshenmic_dpp.node +0 -0
- package/dist/binaries/native/x86_64-unknown-linux-musl/pshenmic_dpp.node +0 -0
- package/dist/binaries/node.cjs +42 -21
- package/dist/binaries/node.d.cts +2 -2
- package/dist/binaries/pshenmic_dpp.android.node/arm64-v8a/libpshenmic_dpp.so +0 -0
- package/dist/binaries/pshenmic_dpp.apple.node/ios-arm64-simulator/libpshenmic_dpp.framework/libpshenmic_dpp +0 -0
- package/dist/binaries/wasm/wasmBytes.cjs +1 -1
- package/dist/src/dpp/structs/AddressTransitions/AddressCreditWithdrawalTransition.d.ts +3 -1
- package/dist/src/dpp/structs/AddressTransitions/AddressCreditWithdrawalTransition.js +6 -0
- package/dist/src/dpp/structs/AddressTransitions/AddressFundingFromAssetLockTransition.d.ts +2 -1
- package/dist/src/dpp/structs/AddressTransitions/AddressFundingFromAssetLockTransition.js +3 -0
- package/dist/src/dpp/structs/AddressTransitions/AddressFundsTransferTransition.d.ts +3 -1
- package/dist/src/dpp/structs/AddressTransitions/AddressFundsTransferTransition.js +6 -0
- package/dist/src/dpp/structs/AddressTransitions/IdentityCreateFromAddressesTransition.d.ts +2 -1
- package/dist/src/dpp/structs/AddressTransitions/IdentityCreateFromAddressesTransition.js +3 -0
- package/dist/src/dpp/structs/AddressTransitions/IdentityCreditTransferToAddressesTransition.d.ts +2 -1
- package/dist/src/dpp/structs/AddressTransitions/IdentityCreditTransferToAddressesTransition.js +3 -0
- package/dist/src/dpp/structs/AddressTransitions/IdentityTopUpFromAddressesTransition.d.ts +2 -1
- package/dist/src/dpp/structs/AddressTransitions/IdentityTopUpFromAddressesTransition.js +3 -0
- package/dist/src/dpp/structs/Batch/BatchTransition.d.ts +2 -1
- package/dist/src/dpp/structs/Batch/BatchTransition.js +3 -0
- package/dist/src/dpp/structs/DataContractTransitions/DataContractCreateTransition.d.ts +2 -1
- package/dist/src/dpp/structs/DataContractTransitions/DataContractCreateTransition.js +3 -0
- package/dist/src/dpp/structs/DataContractTransitions/DataContractUpdateTransition.d.ts +2 -1
- package/dist/src/dpp/structs/DataContractTransitions/DataContractUpdateTransition.js +3 -0
- package/dist/src/dpp/structs/IdentityTransitions/IdentityCreateTransition.d.ts +2 -1
- package/dist/src/dpp/structs/IdentityTransitions/IdentityCreateTransition.js +3 -0
- package/dist/src/dpp/structs/IdentityTransitions/IdentityCreditTransfer.d.ts +2 -1
- package/dist/src/dpp/structs/IdentityTransitions/IdentityCreditTransfer.js +3 -0
- package/dist/src/dpp/structs/IdentityTransitions/IdentityCreditWithdrawalTransition.d.ts +2 -1
- package/dist/src/dpp/structs/IdentityTransitions/IdentityCreditWithdrawalTransition.js +3 -0
- package/dist/src/dpp/structs/IdentityTransitions/IdentityTopUpTransition.d.ts +2 -1
- package/dist/src/dpp/structs/IdentityTransitions/IdentityTopUpTransition.js +3 -0
- package/dist/src/dpp/structs/IdentityTransitions/IdentityUpdateTransition.d.ts +2 -1
- package/dist/src/dpp/structs/IdentityTransitions/IdentityUpdateTransition.js +3 -0
- package/dist/src/dpp/structs/MasternodeVote/MasternodeVoteTransition.d.ts +2 -1
- package/dist/src/dpp/structs/MasternodeVote/MasternodeVoteTransition.js +3 -0
- package/dist/src/dpp/structs/PlatformAddress/PlatformAddress.d.ts +9 -1
- package/dist/src/dpp/structs/PlatformAddress/PlatformAddress.js +10 -0
- package/dist/src/dpp/structs/StateTransition.d.ts +2 -1
- package/dist/src/dpp/structs/StateTransition.js +3 -0
- package/package.json +1 -1
- package/tests/unit/AddressTransitionsFees.spec.ts +274 -0
- package/tests/unit/Shielded.spec.ts +37 -4
- package/tests/unit/StateTransitionFees.spec.ts +177 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddressCreditWithdrawalTransitionWASM,
|
|
3
|
+
AddressFundingFromAssetLockTransitionWASM,
|
|
4
|
+
AddressFundsFeeStrategyStepWASM,
|
|
5
|
+
AddressFundsTransferTransitionWASM,
|
|
6
|
+
AddressWitnessWASM,
|
|
7
|
+
AssetLockProofWASM,
|
|
8
|
+
CoreScriptWASM,
|
|
9
|
+
IdentityCreateFromAddressesTransitionWASM,
|
|
10
|
+
IdentityCreditTransferToAddressesTransitionWASM,
|
|
11
|
+
IdentityPublicKeyInCreationWASM,
|
|
12
|
+
IdentityTopUpFromAddressesTransitionWASM,
|
|
13
|
+
InputAddressWASM,
|
|
14
|
+
KeyType,
|
|
15
|
+
OutPointWASM,
|
|
16
|
+
OutputAddressNullableCreditsWASM,
|
|
17
|
+
OutputAddressWASM,
|
|
18
|
+
PlatformAddressWASM,
|
|
19
|
+
PlatformVersionWASM,
|
|
20
|
+
Purpose,
|
|
21
|
+
SecurityLevel
|
|
22
|
+
} from 'pshenmic-dpp'
|
|
23
|
+
|
|
24
|
+
// Min fee components from the platform fee version (STATE_TRANSITION_MIN_FEES_VERSION1)
|
|
25
|
+
// Storage charged by the node for one address balance entry that does not exist yet,
|
|
26
|
+
// as pinned by platform's own fee regression suite.
|
|
27
|
+
const NEW_ADDRESS_STORAGE_FEE = BigInt(6075000)
|
|
28
|
+
const INPUT_COST = BigInt(500000)
|
|
29
|
+
const OUTPUT_COST = BigInt(6000000)
|
|
30
|
+
const ADDRESS_CREDIT_WITHDRAWAL_COST = BigInt(400000000)
|
|
31
|
+
const IDENTITY_CREATE_BASE_COST = BigInt(2000000)
|
|
32
|
+
const IDENTITY_KEY_IN_CREATION_COST = BigInt(6500000)
|
|
33
|
+
const IDENTITY_TOPUP_BASE_COST = BigInt(500000)
|
|
34
|
+
const CREDIT_TRANSFER_TO_ADDRESSES_COST = BigInt(500000)
|
|
35
|
+
|
|
36
|
+
function randomBytes (length: number): Uint8Array {
|
|
37
|
+
const bytes = new Uint8Array(length)
|
|
38
|
+
for (let i = 0; i < length; i++) {
|
|
39
|
+
bytes[i] = Math.floor(Math.random() * 256)
|
|
40
|
+
}
|
|
41
|
+
return bytes
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function createTestAddress (): PlatformAddressWASM {
|
|
45
|
+
const addressBytes = new Uint8Array(21)
|
|
46
|
+
addressBytes[0] = 0x00
|
|
47
|
+
addressBytes.set(randomBytes(20), 1)
|
|
48
|
+
return PlatformAddressWASM.fromBytes(addressBytes)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function createTestInputs (count: number): InputAddressWASM[] {
|
|
52
|
+
return Array.from(
|
|
53
|
+
{ length: count },
|
|
54
|
+
() => new InputAddressWASM(createTestAddress(), 0, BigInt(500000000))
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function createTestOutputs (count: number): OutputAddressWASM[] {
|
|
59
|
+
return Array.from(
|
|
60
|
+
{ length: count },
|
|
61
|
+
() => new OutputAddressWASM(createTestAddress(), BigInt(100000))
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function createTestWitnesses (count: number): AddressWitnessWASM[] {
|
|
66
|
+
return Array.from({ length: count }, () => AddressWitnessWASM.P2PKH(randomBytes(65)))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const feeStrategy = (): AddressFundsFeeStrategyStepWASM[] =>
|
|
70
|
+
[AddressFundsFeeStrategyStepWASM.DeductFromInput(0)]
|
|
71
|
+
|
|
72
|
+
describe('AddressFundsTransferTransition fees', function () {
|
|
73
|
+
describe('estimateMinFee', function () {
|
|
74
|
+
test('should price inputs and outputs', function () {
|
|
75
|
+
expect(AddressFundsTransferTransitionWASM.estimateMinFee(1, 1))
|
|
76
|
+
.toEqual(INPUT_COST + OUTPUT_COST)
|
|
77
|
+
|
|
78
|
+
expect(AddressFundsTransferTransitionWASM.estimateMinFee(2, 3))
|
|
79
|
+
.toEqual(INPUT_COST * BigInt(2) + OUTPUT_COST * BigInt(3))
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('should charge at least one output', function () {
|
|
83
|
+
expect(AddressFundsTransferTransitionWASM.estimateMinFee(1, 0))
|
|
84
|
+
.toEqual(AddressFundsTransferTransitionWASM.estimateMinFee(1, 1))
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('should grow with the input and output counts', function () {
|
|
88
|
+
const one = AddressFundsTransferTransitionWASM.estimateMinFee(1, 1)
|
|
89
|
+
const two = AddressFundsTransferTransitionWASM.estimateMinFee(2, 2)
|
|
90
|
+
|
|
91
|
+
expect(two).toBeGreaterThan(one)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('should default to the latest platform version', function () {
|
|
95
|
+
expect(AddressFundsTransferTransitionWASM.estimateMinFee(1, 1))
|
|
96
|
+
.toEqual(AddressFundsTransferTransitionWASM.estimateMinFee(1, 1, PlatformVersionWASM.PLATFORM_V13))
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
describe('calculateMinRequiredFee', function () {
|
|
101
|
+
test('should match the estimate for the transition inputs and outputs', function () {
|
|
102
|
+
const transition = new AddressFundsTransferTransitionWASM(
|
|
103
|
+
createTestInputs(2),
|
|
104
|
+
feeStrategy(),
|
|
105
|
+
0,
|
|
106
|
+
createTestWitnesses(2),
|
|
107
|
+
createTestOutputs(1)
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
expect(transition.calculateMinRequiredFee())
|
|
111
|
+
.toEqual(AddressFundsTransferTransitionWASM.estimateMinFee(2, 1))
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('AddressCreditWithdrawalTransition fees', function () {
|
|
117
|
+
const outputScript = (): CoreScriptWASM => CoreScriptWASM.newP2PKH(randomBytes(20))
|
|
118
|
+
|
|
119
|
+
describe('estimateMinFee', function () {
|
|
120
|
+
test('should price the withdrawal, its inputs and its change output', function () {
|
|
121
|
+
expect(AddressCreditWithdrawalTransitionWASM.estimateMinFee(1, false))
|
|
122
|
+
.toEqual(ADDRESS_CREDIT_WITHDRAWAL_COST + INPUT_COST)
|
|
123
|
+
|
|
124
|
+
expect(AddressCreditWithdrawalTransitionWASM.estimateMinFee(1, true))
|
|
125
|
+
.toEqual(ADDRESS_CREDIT_WITHDRAWAL_COST + INPUT_COST + OUTPUT_COST)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('should grow with the input count', function () {
|
|
129
|
+
expect(AddressCreditWithdrawalTransitionWASM.estimateMinFee(2, false))
|
|
130
|
+
.toBeGreaterThan(AddressCreditWithdrawalTransitionWASM.estimateMinFee(1, false))
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
describe('calculateMinRequiredFee', function () {
|
|
135
|
+
test('should match the estimate without a change output', function () {
|
|
136
|
+
const transition = new AddressCreditWithdrawalTransitionWASM(
|
|
137
|
+
createTestInputs(1),
|
|
138
|
+
feeStrategy(),
|
|
139
|
+
1,
|
|
140
|
+
'Standard',
|
|
141
|
+
outputScript(),
|
|
142
|
+
0,
|
|
143
|
+
createTestWitnesses(1)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
expect(transition.calculateMinRequiredFee())
|
|
147
|
+
.toEqual(AddressCreditWithdrawalTransitionWASM.estimateMinFee(1, false))
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('should match the estimate with a change output', function () {
|
|
151
|
+
const transition = new AddressCreditWithdrawalTransitionWASM(
|
|
152
|
+
createTestInputs(1),
|
|
153
|
+
feeStrategy(),
|
|
154
|
+
1,
|
|
155
|
+
'Standard',
|
|
156
|
+
outputScript(),
|
|
157
|
+
0,
|
|
158
|
+
createTestWitnesses(1),
|
|
159
|
+
createTestOutputs(1)[0]
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
expect(transition.calculateMinRequiredFee())
|
|
163
|
+
.toEqual(AddressCreditWithdrawalTransitionWASM.estimateMinFee(1, true))
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
describe('IdentityCreateFromAddressesTransition fees', function () {
|
|
169
|
+
test('calculateMinRequiredFee should price the base cost, inputs, output and keys', function () {
|
|
170
|
+
const publicKeys = [
|
|
171
|
+
new IdentityPublicKeyInCreationWASM(
|
|
172
|
+
0,
|
|
173
|
+
Purpose.AUTHENTICATION,
|
|
174
|
+
SecurityLevel.MASTER,
|
|
175
|
+
KeyType.ECDSA_SECP256K1,
|
|
176
|
+
false,
|
|
177
|
+
randomBytes(33)
|
|
178
|
+
)
|
|
179
|
+
]
|
|
180
|
+
|
|
181
|
+
const transition = new IdentityCreateFromAddressesTransitionWASM(
|
|
182
|
+
publicKeys,
|
|
183
|
+
createTestInputs(1),
|
|
184
|
+
feeStrategy(),
|
|
185
|
+
0,
|
|
186
|
+
createTestWitnesses(1),
|
|
187
|
+
createTestOutputs(1)[0]
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
expect(transition.calculateMinRequiredFee()).toEqual(
|
|
191
|
+
IDENTITY_CREATE_BASE_COST + INPUT_COST + OUTPUT_COST + IDENTITY_KEY_IN_CREATION_COST
|
|
192
|
+
)
|
|
193
|
+
})
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
describe('IdentityTopUpFromAddressesTransition fees', function () {
|
|
197
|
+
test('calculateMinRequiredFee should price the base cost, inputs and output', function () {
|
|
198
|
+
const transition = new IdentityTopUpFromAddressesTransitionWASM(
|
|
199
|
+
randomBytes(32),
|
|
200
|
+
createTestInputs(1),
|
|
201
|
+
feeStrategy(),
|
|
202
|
+
0,
|
|
203
|
+
createTestWitnesses(1),
|
|
204
|
+
createTestOutputs(1)[0]
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
expect(transition.calculateMinRequiredFee())
|
|
208
|
+
.toEqual(IDENTITY_TOPUP_BASE_COST + INPUT_COST + OUTPUT_COST)
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
describe('IdentityCreditTransferToAddressesTransition fees', function () {
|
|
213
|
+
test('calculateMinRequiredFee should price the base cost and each recipient', function () {
|
|
214
|
+
const transition = new IdentityCreditTransferToAddressesTransitionWASM(
|
|
215
|
+
randomBytes(32),
|
|
216
|
+
createTestOutputs(2),
|
|
217
|
+
BigInt(1),
|
|
218
|
+
0
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
expect(transition.calculateMinRequiredFee())
|
|
222
|
+
.toEqual(CREDIT_TRANSFER_TO_ADDRESSES_COST + OUTPUT_COST * BigInt(2))
|
|
223
|
+
})
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
describe('AddressFundingFromAssetLockTransition fees', function () {
|
|
227
|
+
test('calculateMinRequiredFee should include the asset lock base cost', function () {
|
|
228
|
+
const outPoint = new OutPointWASM('a'.repeat(64), 0)
|
|
229
|
+
const assetLockProof = AssetLockProofWASM.createChainAssetLockProof(100, outPoint)
|
|
230
|
+
|
|
231
|
+
const transition = new AddressFundingFromAssetLockTransitionWASM(
|
|
232
|
+
assetLockProof,
|
|
233
|
+
createTestInputs(1),
|
|
234
|
+
feeStrategy(),
|
|
235
|
+
0,
|
|
236
|
+
createTestWitnesses(1),
|
|
237
|
+
[new OutputAddressNullableCreditsWASM(createTestAddress(), BigInt(100000))]
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
const fee = transition.calculateMinRequiredFee()
|
|
241
|
+
|
|
242
|
+
expect(typeof fee).toEqual('bigint')
|
|
243
|
+
expect(fee).toBeGreaterThan(INPUT_COST + OUTPUT_COST)
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
describe('new address storage fee', function () {
|
|
248
|
+
test('should match the storage the node charges for one created address', function () {
|
|
249
|
+
expect(PlatformAddressWASM.estimateStorageFeeForNewAddresses(1))
|
|
250
|
+
.toEqual(NEW_ADDRESS_STORAGE_FEE)
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('should scale with the number of created addresses', function () {
|
|
254
|
+
expect(PlatformAddressWASM.estimateStorageFeeForNewAddresses(0)).toEqual(BigInt(0))
|
|
255
|
+
expect(PlatformAddressWASM.estimateStorageFeeForNewAddresses(3))
|
|
256
|
+
.toEqual(NEW_ADDRESS_STORAGE_FEE * BigInt(3))
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
test('should default to the latest platform version', function () {
|
|
260
|
+
expect(PlatformAddressWASM.estimateStorageFeeForNewAddresses(1))
|
|
261
|
+
.toEqual(PlatformAddressWASM.estimateStorageFeeForNewAddresses(1, PlatformVersionWASM.PLATFORM_V13))
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
test('should exceed the per output component of the min fee', function () {
|
|
265
|
+
const perOutput =
|
|
266
|
+
AddressFundsTransferTransitionWASM.estimateMinFee(1, 2) -
|
|
267
|
+
AddressFundsTransferTransitionWASM.estimateMinFee(1, 1)
|
|
268
|
+
|
|
269
|
+
// The min fee prices an output slightly below what a created address costs in storage,
|
|
270
|
+
// which is why a transfer paying a brand new address is charged a little more than its
|
|
271
|
+
// minimum required fee.
|
|
272
|
+
expect(NEW_ADDRESS_STORAGE_FEE).toBeGreaterThan(perOutput)
|
|
273
|
+
})
|
|
274
|
+
})
|
|
@@ -27,7 +27,9 @@ const ACCOUNT = 0
|
|
|
27
27
|
// A funded testnet WIF reused across the builder/deposit tests.
|
|
28
28
|
const WIF = 'cUy4wbim4y9NDwC24omx8oWY5WqfmjSU2gdcZtTXza2xDCAkQXRP'
|
|
29
29
|
|
|
30
|
-
//
|
|
30
|
+
// The memo payload field is a fixed 32 bytes (MEMO_PAYLOAD_SIZE) on the wire.
|
|
31
|
+
// Text memos may be shorter and are zero-padded to it; `other` payloads are
|
|
32
|
+
// taken verbatim and so must match it exactly.
|
|
31
33
|
const MEMO_PAYLOAD_SIZE = 32
|
|
32
34
|
|
|
33
35
|
const DASH = 100_000_000_000n
|
|
@@ -62,6 +64,7 @@ describe('ShieldedMemo', function () {
|
|
|
62
64
|
|
|
63
65
|
expect(memo).toBeInstanceOf(ShieldedMemoWASM)
|
|
64
66
|
expect(memo.toBytes().length).toEqual(36) // on-wire memo size
|
|
67
|
+
expect(memo.toString()).toEqual('')
|
|
65
68
|
})
|
|
66
69
|
})
|
|
67
70
|
|
|
@@ -72,11 +75,41 @@ describe('ShieldedMemo', function () {
|
|
|
72
75
|
|
|
73
76
|
expect(memo).toBeInstanceOf(ShieldedMemoWASM)
|
|
74
77
|
expect(memo.toBytes().length).toEqual(36)
|
|
75
|
-
expect(memo.toString().
|
|
78
|
+
expect(memo.toString()).toEqual(text)
|
|
76
79
|
})
|
|
77
80
|
|
|
78
|
-
test('should
|
|
79
|
-
|
|
81
|
+
test('should accept text shorter than the payload', function () {
|
|
82
|
+
const text = 'thanks for lunch' // 16 bytes, half the payload
|
|
83
|
+
const memo = ShieldedMemoWASM.fromString(text)
|
|
84
|
+
const bytes = memo.toBytes()
|
|
85
|
+
|
|
86
|
+
// The wire memo stays a fixed 36 bytes: a 4-byte little-endian kind tag
|
|
87
|
+
// (1 = text), the utf-8 text, then zero padding out to the 32-byte
|
|
88
|
+
// payload. The padding is not part of the decoded text.
|
|
89
|
+
expect(bytes.length).toEqual(36)
|
|
90
|
+
expect(Array.from(bytes.slice(0, 4))).toEqual([1, 0, 0, 0])
|
|
91
|
+
expect(new TextDecoder().decode(bytes.slice(4, 4 + text.length))).toEqual(text)
|
|
92
|
+
expect(bytes.slice(4 + text.length).every((byte) => byte === 0)).toBe(true)
|
|
93
|
+
expect(memo.toString()).toEqual(text)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('should accept an empty string', function () {
|
|
97
|
+
const memo = ShieldedMemoWASM.fromString('')
|
|
98
|
+
|
|
99
|
+
expect(memo.toBytes().length).toEqual(36)
|
|
100
|
+
expect(memo.toString()).toEqual('')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('should count multi-byte characters by their utf-8 byte length', function () {
|
|
104
|
+
// 8 x U+1F355 = 32 bytes, exactly the payload; 9 would be 36 bytes.
|
|
105
|
+
const memo = ShieldedMemoWASM.fromString('🍕'.repeat(8))
|
|
106
|
+
|
|
107
|
+
expect(memo.toString()).toEqual('🍕'.repeat(8))
|
|
108
|
+
expect(() => ShieldedMemoWASM.fromString('🍕'.repeat(9))).toThrow()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('should reject text longer than 32 bytes', function () {
|
|
112
|
+
expect(() => ShieldedMemoWASM.fromString('A'.repeat(MEMO_PAYLOAD_SIZE + 1))).toThrow()
|
|
80
113
|
})
|
|
81
114
|
})
|
|
82
115
|
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AssetLockProofWASM,
|
|
3
|
+
BatchTransitionWASM,
|
|
4
|
+
CoreScriptWASM,
|
|
5
|
+
DataContractCreateTransitionWASM,
|
|
6
|
+
DataContractWASM,
|
|
7
|
+
IdentityCreateTransitionWASM,
|
|
8
|
+
IdentityCreditTransferWASM,
|
|
9
|
+
IdentityCreditWithdrawalTransitionWASM,
|
|
10
|
+
IdentityPublicKeyInCreationWASM,
|
|
11
|
+
IdentityTopUpTransitionWASM,
|
|
12
|
+
IdentityUpdateTransitionWASM,
|
|
13
|
+
KeyType,
|
|
14
|
+
OutPointWASM,
|
|
15
|
+
Purpose,
|
|
16
|
+
SecurityLevel,
|
|
17
|
+
StateTransitionWASM
|
|
18
|
+
} from 'pshenmic-dpp'
|
|
19
|
+
|
|
20
|
+
// Min fees from the platform fee version (STATE_TRANSITION_MIN_FEES_VERSION1)
|
|
21
|
+
const CREDIT_TRANSFER_COST = BigInt(100000)
|
|
22
|
+
const CREDIT_WITHDRAWAL_COST = BigInt(400000000)
|
|
23
|
+
const IDENTITY_UPDATE_COST = BigInt(100000)
|
|
24
|
+
const CONTRACT_CREATE_COST = BigInt(100000)
|
|
25
|
+
const DOCUMENT_BATCH_SUB_TRANSITION_COST = BigInt(100000)
|
|
26
|
+
const IDENTITY_CREATE_BASE_COST = BigInt(2000000)
|
|
27
|
+
const IDENTITY_KEY_IN_CREATION_COST = BigInt(6500000)
|
|
28
|
+
const IDENTITY_TOPUP_BASE_COST = BigInt(500000)
|
|
29
|
+
// required asset lock duff balance × CREDITS_PER_DUFF (1000)
|
|
30
|
+
const IDENTITY_CREATE_ASSET_LOCK_COST = BigInt(200000) * BigInt(1000)
|
|
31
|
+
const IDENTITY_TOPUP_ASSET_LOCK_COST = BigInt(50000) * BigInt(1000)
|
|
32
|
+
|
|
33
|
+
// A signed document batch transition holding a single document create transition
|
|
34
|
+
const BATCH_TRANSITION_BASE64 = 'AgHv88sfWzscuW2Ob/0v5d+EgqmWG7ZIvlQG7mHWBH7z9gEAAAAB7JH7W+JkM0Vne2U1zlWYVeQNzRPTpdiWRfsgfwNS2UoCCGJsb2dQb3N0KBdMgeznVQ1smUXVFNPfE2PC379wpWVH8cdu2ilzeI0Ah/i2xfrKCm5ACQAbEQkSc+GRUkdLor4Eu7Vzf6oRc9UHBmJsb2dJZAogdhC4nVm3ufPCgdfoOKXtnhqwg7O3axyTS9WB3EFvNBYPY29tbWVudHNFbmFibGVkEwEHY29udGVudAr7AUh4nM2TTWrDMBCFrxK0jkCyJMvqLnRV6A1KFhpplJgotrEVaAi+e8clpdBuvCnNcn7ezPvQ6O3G2siemKgNSGETT8ZarhUm7hpX8yStEpVCk6xlW1auA1L34Ed/GP1wpNQw9sPEnm6s4Ht57nM/UkPE5C+5UBl8OB3G/tLF37VFscvtoTtjV6iSMRU2b1nou/KZIXf3jUvrXUHRrosbv8ltKRk30JbNl2LLpnLNuPiZ5z1NOrY5jtjRqP28vbM656zTleagJHCNXnIQiUIpgozGg2/0N2s4Yji9tlN5KXj+A97FJG1AcpZ8nnANv1wHGo22ASrFXQDJde0SBzDIdQBVRQEoDDw2aLUONPkKtfF0s9bR9bqGkJW3PNYCg/FOgIqPDarWgQrRxKapBXdCJ3pR7TjUVnJrpAo6yhSc/4dv+tPu/gNBt2qtC3B1Ymxpc2hlZEF0Av0AAAGcptJlIwRzbHVnEhV0aGlzLWlzLW15LWZpcnN0LXBvc3QIc3VidGl0bGUSD1dpdGggYSBzdWJ0aXRsZQV0aXRsZRIVVGhpcyBpcyBteSBmaXJzdCBwb3N0AAABQR+H40MbUEmFXQbTPcwy9vMD8EiTwDzMpW3YIkfSIDvHij5ZIAVvqm0ZJScJzKV7EnRUi/f1tZ/wFJfd1+LDtpps'
|
|
35
|
+
|
|
36
|
+
function randomBytes (length: number): Uint8Array {
|
|
37
|
+
const bytes = new Uint8Array(length)
|
|
38
|
+
for (let i = 0; i < length; i++) {
|
|
39
|
+
bytes[i] = Math.floor(Math.random() * 256)
|
|
40
|
+
}
|
|
41
|
+
return bytes
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function createTestKey (): IdentityPublicKeyInCreationWASM {
|
|
45
|
+
return new IdentityPublicKeyInCreationWASM(
|
|
46
|
+
0,
|
|
47
|
+
Purpose.AUTHENTICATION,
|
|
48
|
+
SecurityLevel.MASTER,
|
|
49
|
+
KeyType.ECDSA_SECP256K1,
|
|
50
|
+
false,
|
|
51
|
+
randomBytes(33)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function createAssetLockProof (): AssetLockProofWASM {
|
|
56
|
+
return AssetLockProofWASM.createChainAssetLockProof(100, new OutPointWASM('a'.repeat(64), 0))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe('IdentityCreditTransferTransition fees', function () {
|
|
60
|
+
test('calculateMinRequiredFee should return the credit transfer min fee', function () {
|
|
61
|
+
const transition = new IdentityCreditTransferWASM(
|
|
62
|
+
randomBytes(32), BigInt(100000), randomBytes(32), BigInt(1)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
expect(transition.calculateMinRequiredFee()).toEqual(CREDIT_TRANSFER_COST)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('IdentityCreditWithdrawalTransition fees', function () {
|
|
70
|
+
test('calculateMinRequiredFee should return the credit withdrawal min fee', function () {
|
|
71
|
+
const transition = new IdentityCreditWithdrawalTransitionWASM(
|
|
72
|
+
randomBytes(32),
|
|
73
|
+
BigInt(1000000),
|
|
74
|
+
1,
|
|
75
|
+
'Standard',
|
|
76
|
+
BigInt(1),
|
|
77
|
+
CoreScriptWASM.newP2PKH(randomBytes(20))
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
expect(transition.calculateMinRequiredFee()).toEqual(CREDIT_WITHDRAWAL_COST)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
describe('IdentityUpdateTransition fees', function () {
|
|
85
|
+
test('calculateMinRequiredFee should return the identity update min fee', function () {
|
|
86
|
+
const transition = new IdentityUpdateTransitionWASM(
|
|
87
|
+
randomBytes(32), BigInt(1), BigInt(1), [createTestKey()], []
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
expect(transition.calculateMinRequiredFee()).toEqual(IDENTITY_UPDATE_COST)
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe('IdentityCreateTransition fees', function () {
|
|
95
|
+
test('calculateMinRequiredFee should price the base cost, asset lock and keys', function () {
|
|
96
|
+
const transition = new IdentityCreateTransitionWASM([createTestKey()], createAssetLockProof())
|
|
97
|
+
|
|
98
|
+
expect(transition.calculateMinRequiredFee()).toEqual(
|
|
99
|
+
IDENTITY_CREATE_BASE_COST + IDENTITY_CREATE_ASSET_LOCK_COST + IDENTITY_KEY_IN_CREATION_COST
|
|
100
|
+
)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('calculateMinRequiredFee should grow with the key count', function () {
|
|
104
|
+
const oneKey = new IdentityCreateTransitionWASM([createTestKey()], createAssetLockProof())
|
|
105
|
+
const twoKeys = new IdentityCreateTransitionWASM(
|
|
106
|
+
[createTestKey(), createTestKey()], createAssetLockProof()
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
expect(twoKeys.calculateMinRequiredFee() - oneKey.calculateMinRequiredFee())
|
|
110
|
+
.toEqual(IDENTITY_KEY_IN_CREATION_COST)
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe('IdentityTopUpTransition fees', function () {
|
|
115
|
+
test('calculateMinRequiredFee should price the base cost and the asset lock', function () {
|
|
116
|
+
const transition = new IdentityTopUpTransitionWASM(createAssetLockProof(), randomBytes(32))
|
|
117
|
+
|
|
118
|
+
expect(transition.calculateMinRequiredFee())
|
|
119
|
+
.toEqual(IDENTITY_TOPUP_BASE_COST + IDENTITY_TOPUP_ASSET_LOCK_COST)
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('BatchTransition fees', function () {
|
|
124
|
+
test('calculateMinRequiredFee should price each sub transition', function () {
|
|
125
|
+
const batch = BatchTransitionWASM.fromStateTransition(
|
|
126
|
+
StateTransitionWASM.fromBase64(BATCH_TRANSITION_BASE64)
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
expect(batch.calculateMinRequiredFee())
|
|
130
|
+
.toEqual(DOCUMENT_BATCH_SUB_TRANSITION_COST * BigInt(batch.transitions.length))
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
describe('StateTransition fees', function () {
|
|
135
|
+
test('calculateMinRequiredFee should match the concrete transition', function () {
|
|
136
|
+
const transition = new IdentityCreditTransferWASM(
|
|
137
|
+
randomBytes(32), BigInt(100000), randomBytes(32), BigInt(1)
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
expect(transition.toStateTransition().calculateMinRequiredFee())
|
|
141
|
+
.toEqual(transition.calculateMinRequiredFee())
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('calculateMinRequiredFee should work for a deserialized transition', function () {
|
|
145
|
+
const stateTransition = StateTransitionWASM.fromBase64(BATCH_TRANSITION_BASE64)
|
|
146
|
+
|
|
147
|
+
expect(stateTransition.calculateMinRequiredFee())
|
|
148
|
+
.toEqual(DOCUMENT_BATCH_SUB_TRANSITION_COST)
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
describe('DataContractCreateTransition fees', function () {
|
|
153
|
+
const OWNER_ID_BASE58 = 'HEAmUtC72dPcZ59yyLUgfS8pfrEWqzYfDPzTofgWxXRr'
|
|
154
|
+
const DOCUMENT_SCHEMA = {
|
|
155
|
+
note: {
|
|
156
|
+
type: 'object',
|
|
157
|
+
properties: {
|
|
158
|
+
message: {
|
|
159
|
+
type: 'string',
|
|
160
|
+
position: 0
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
additionalProperties: false
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
test('calculateMinRequiredFee should cover the base fee plus the registration cost', function () {
|
|
168
|
+
const contract = new DataContractWASM(
|
|
169
|
+
OWNER_ID_BASE58, BigInt(1), DOCUMENT_SCHEMA, undefined, undefined, true
|
|
170
|
+
)
|
|
171
|
+
const transition = new DataContractCreateTransitionWASM(contract, BigInt(1))
|
|
172
|
+
|
|
173
|
+
expect(transition.calculateMinRequiredFee()).toBeGreaterThanOrEqual(CONTRACT_CREATE_COST)
|
|
174
|
+
expect(transition.calculateMinRequiredFee())
|
|
175
|
+
.toEqual(transition.toStateTransition().calculateMinRequiredFee())
|
|
176
|
+
})
|
|
177
|
+
})
|