|
| 1 | +const { |
| 2 | + startDapi, |
| 3 | +} = require('@dashevo/dp-services-ctl'); |
| 4 | + |
| 5 | +const { |
| 6 | + PrivateKey, |
| 7 | + PublicKey, |
| 8 | + Transaction, |
| 9 | +} = require('@dashevo/dashcore-lib'); |
| 10 | + |
| 11 | +const DashPlatformProtocol = require('@dashevo/dpp'); |
| 12 | + |
| 13 | +const GrpcErrorCodes = require('@dashevo/grpc-common/lib/server/error/GrpcErrorCodes'); |
| 14 | + |
| 15 | +const { convertSatoshiToCredits } = require( |
| 16 | + '@dashevo/dpp/lib/identity/creditsConverter', |
| 17 | +); |
| 18 | + |
| 19 | +const wait = require('../../../../../lib/utils/wait'); |
| 20 | + |
| 21 | +describe('topUpIdentity', function main() { |
| 22 | + this.timeout(200000); |
| 23 | + |
| 24 | + let removeDapi; |
| 25 | + let dapiClient; |
| 26 | + let dpp; |
| 27 | + let identity; |
| 28 | + let identityCreateTransition; |
| 29 | + let identityTopUpTransition; |
| 30 | + let coreAPI; |
| 31 | + let addressString; |
| 32 | + let publicKeyHash; |
| 33 | + let privateKey; |
| 34 | + |
| 35 | + before(async () => { |
| 36 | + const { |
| 37 | + dapiCore, |
| 38 | + dashCore, |
| 39 | + remove, |
| 40 | + } = await startDapi(); |
| 41 | + |
| 42 | + removeDapi = remove; |
| 43 | + coreAPI = dashCore.getApi(); |
| 44 | + dapiClient = dapiCore.getApi(); |
| 45 | + |
| 46 | + dpp = new DashPlatformProtocol({ |
| 47 | + dataProvider: {}, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + beforeEach(async () => { |
| 52 | + ({ result: addressString } = await coreAPI.getNewAddress()); |
| 53 | + const { result: privateKeyString } = await coreAPI.dumpPrivKey(addressString); |
| 54 | + |
| 55 | + privateKey = new PrivateKey(privateKeyString); |
| 56 | + const publicKey = new PublicKey({ |
| 57 | + ...privateKey.toPublicKey().toObject(), |
| 58 | + compressed: true, |
| 59 | + }); |
| 60 | + const pubKeyBase = publicKey.toBuffer() |
| 61 | + .toString('base64'); |
| 62 | + |
| 63 | + // eslint-disable-next-line no-underscore-dangle |
| 64 | + publicKeyHash = PublicKey.fromBuffer(Buffer.from(pubKeyBase, 'base64')) |
| 65 | + ._getID(); |
| 66 | + |
| 67 | + await coreAPI.generateToAddress(500, addressString); |
| 68 | + |
| 69 | + const { result: unspent } = await coreAPI.listUnspent(); |
| 70 | + const inputs = unspent.filter(input => input.address === addressString); |
| 71 | + |
| 72 | + const transaction = new Transaction(); |
| 73 | + |
| 74 | + transaction.from(inputs.slice(-1)[0]) |
| 75 | + .addBurnOutput(10000, publicKeyHash) |
| 76 | + .change(addressString) |
| 77 | + .fee(668) |
| 78 | + .sign(privateKey); |
| 79 | + |
| 80 | + await coreAPI.sendrawtransaction(transaction.serialize()); |
| 81 | + |
| 82 | + await coreAPI.generateToAddress(1, addressString); |
| 83 | + |
| 84 | + await wait(2000); // wait a couple of seconds for tx to be confirmed |
| 85 | + |
| 86 | + const outPoint = transaction.getOutPointBuffer(0); |
| 87 | + |
| 88 | + identity = dpp.identity.create( |
| 89 | + outPoint, |
| 90 | + [publicKey], |
| 91 | + ); |
| 92 | + |
| 93 | + identityCreateTransition = dpp.identity.createIdentityCreateTransition(identity); |
| 94 | + identityCreateTransition.signByPrivateKey(privateKey); |
| 95 | + |
| 96 | + await dapiClient.applyStateTransition(identityCreateTransition); |
| 97 | + }); |
| 98 | + |
| 99 | + after(async () => { |
| 100 | + await removeDapi(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should top up created identity', async () => { |
| 104 | + const { result: unspent } = await coreAPI.listUnspent(); |
| 105 | + const inputs = unspent.filter(input => input.address === addressString); |
| 106 | + const topUpTransaction = new Transaction(); |
| 107 | + const topUpAmount = 3000; |
| 108 | + |
| 109 | + topUpTransaction.from(inputs.slice(-1)[0]) |
| 110 | + .addBurnOutput(topUpAmount, publicKeyHash) |
| 111 | + .change(addressString) |
| 112 | + .fee(668) |
| 113 | + .sign(privateKey); |
| 114 | + |
| 115 | + await coreAPI.sendrawtransaction(topUpTransaction.serialize()); |
| 116 | + |
| 117 | + await coreAPI.generateToAddress(1, addressString); |
| 118 | + |
| 119 | + await wait(2000); // wait a couple of seconds for tx to be confirmed |
| 120 | + |
| 121 | + const topUpOutPoint = topUpTransaction.getOutPointBuffer(0); |
| 122 | + |
| 123 | + identityTopUpTransition = dpp.identity.createIdentityTopUpTransition( |
| 124 | + identity.getId(), |
| 125 | + topUpOutPoint, |
| 126 | + ); |
| 127 | + identityTopUpTransition.signByPrivateKey(privateKey); |
| 128 | + |
| 129 | + await dapiClient.applyStateTransition(identityTopUpTransition); |
| 130 | + |
| 131 | + const serializedIdentity = await dapiClient.getIdentity( |
| 132 | + identityCreateTransition.getIdentityId(), |
| 133 | + ); |
| 134 | + |
| 135 | + const receivedIdentity = dpp.identity.createFromSerialized( |
| 136 | + serializedIdentity, |
| 137 | + { skipValidation: true }, |
| 138 | + ); |
| 139 | + |
| 140 | + const balance = convertSatoshiToCredits(10000) |
| 141 | + + convertSatoshiToCredits(topUpAmount) |
| 142 | + - identityCreateTransition.calculateFee() |
| 143 | + - identityTopUpTransition.calculateFee(); |
| 144 | + |
| 145 | + expect(balance).to.equal(receivedIdentity.getBalance()); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should fail top up created identity ', async () => { |
| 149 | + const { result: unspent } = await coreAPI.listUnspent(); |
| 150 | + const inputs = unspent.filter(input => input.address === addressString); |
| 151 | + const topUpTransaction = new Transaction(); |
| 152 | + const topUpAmount = 3000; |
| 153 | + |
| 154 | + topUpTransaction.from(inputs.slice(-1)[0]) |
| 155 | + .addBurnOutput(topUpAmount, publicKeyHash) |
| 156 | + .change(addressString) |
| 157 | + .fee(668) |
| 158 | + .sign(privateKey); |
| 159 | + |
| 160 | + const topUpOutPoint = topUpTransaction.getOutPointBuffer(0); |
| 161 | + |
| 162 | + identityTopUpTransition = dpp.identity.createIdentityTopUpTransition( |
| 163 | + identity.getId(), |
| 164 | + topUpOutPoint, |
| 165 | + ); |
| 166 | + identityTopUpTransition.signByPrivateKey(privateKey); |
| 167 | + |
| 168 | + try { |
| 169 | + await dapiClient.applyStateTransition(identityTopUpTransition); |
| 170 | + |
| 171 | + expect.fail('Should fail with error'); |
| 172 | + } catch (e) { |
| 173 | + expect(e.code).to.equal(GrpcErrorCodes.INVALID_ARGUMENT); |
| 174 | + expect(e.details).to.equal('State Transition is invalid'); |
| 175 | + } |
| 176 | + |
| 177 | + const serializedIdentity = await dapiClient.getIdentity( |
| 178 | + identityCreateTransition.getIdentityId(), |
| 179 | + ); |
| 180 | + |
| 181 | + const receivedIdentity = dpp.identity.createFromSerialized( |
| 182 | + serializedIdentity, |
| 183 | + { skipValidation: true }, |
| 184 | + ); |
| 185 | + |
| 186 | + expect(convertSatoshiToCredits(10000) - identityCreateTransition.calculateFee()) |
| 187 | + .to.equal(receivedIdentity.getBalance()); |
| 188 | + }); |
| 189 | +}); |
0 commit comments