diff --git a/src/abi/codec/address.ts b/src/abi/codec/address.ts index 8e640d54..befe22ff 100644 --- a/src/abi/codec/address.ts +++ b/src/abi/codec/address.ts @@ -29,13 +29,13 @@ export class AddressBinaryCodec { * Encodes an AddressValue to a buffer. */ encodeNested(primitive: AddressValue): Buffer { - return primitive.valueOf().pubkey(); + return primitive.valueOf().getPublicKey(); } /** * Encodes an AddressValue to a buffer. */ encodeTopLevel(primitive: AddressValue): Buffer { - return primitive.valueOf().pubkey(); + return primitive.valueOf().getPublicKey(); } } diff --git a/src/abi/interaction.spec.ts b/src/abi/interaction.spec.ts index 61b64325..c24450dc 100644 --- a/src/abi/interaction.spec.ts +++ b/src/abi/interaction.spec.ts @@ -65,7 +65,7 @@ describe("test smart contract interactor", function () { const hexBar = "4241522d356263303866"; const hexLKMEX = "4c4b4d45582d616162393130"; const hexNFT = "4d4f532d623962346232"; - const hexContractAddress = new Address(contract.getAddress().toBech32()).hex(); + const hexContractAddress = contract.getAddress().toHex(); const hexDummyFunction = "64756d6d79"; // ESDT, single diff --git a/src/abi/query.spec.ts b/src/abi/query.spec.ts index 090166b1..b9e198cb 100644 --- a/src/abi/query.spec.ts +++ b/src/abi/query.spec.ts @@ -1,9 +1,9 @@ +import BigNumber from "bignumber.js"; import { assert } from "chai"; import { Address } from "../address"; import { ContractFunction } from "./function"; import { Query } from "./query"; import { BigUIntValue, U32Value } from "./typesystem"; -import BigNumber from "bignumber.js"; import { BytesValue } from "./typesystem/bytes"; describe("test smart contract queries", () => { diff --git a/src/abi/smartContract.ts b/src/abi/smartContract.ts index 2814336c..2a48e597 100644 --- a/src/abi/smartContract.ts +++ b/src/abi/smartContract.ts @@ -288,8 +288,7 @@ export class SmartContract implements ISmartContract { * @param nonce The owner nonce used for the deployment transaction */ static computeAddress(owner: Address, nonce: bigint): Address { - const deployer = Address.fromBech32(owner.toBech32()); const addressComputer = new AddressComputer(); - return addressComputer.computeContractAddress(deployer, BigInt(nonce.valueOf())); + return addressComputer.computeContractAddress(owner, BigInt(nonce.valueOf())); } } diff --git a/src/accountManagement/accountTransactionsFactory.ts b/src/accountManagement/accountTransactionsFactory.ts index 37b17662..2cdac3c7 100644 --- a/src/accountManagement/accountTransactionsFactory.ts +++ b/src/accountManagement/accountTransactionsFactory.ts @@ -63,7 +63,7 @@ export class AccountTransactionsFactory { createTransactionForSettingGuardian(sender: Address, options: SetGuardianInput): Transaction { const dataParts = [ "SetGuardian", - Address.fromBech32(options.guardianAddress.bech32()).toHex(), + options.guardianAddress.toHex(), Buffer.from(options.serviceID).toString("hex"), ]; diff --git a/src/address.spec.ts b/src/address.spec.ts index ee754675..bfb2dccf 100644 --- a/src/address.spec.ts +++ b/src/address.spec.ts @@ -20,12 +20,12 @@ describe("test address", () => { }); it("should create address (custom hrp)", async () => { - let address = Address.fromHex(aliceHex, "test"); + let address = Address.newFromHex(aliceHex, "test"); assert.deepEqual(address.getPublicKey(), Buffer.from(aliceHex, "hex")); assert.equal(address.getHrp(), "test"); assert.equal(address.toBech32(), "test1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ss5hqhtr"); - address = Address.fromHex(bobHex, "xerd"); + address = Address.newFromHex(bobHex, "xerd"); assert.deepEqual(address.getPublicKey(), Buffer.from(bobHex, "hex")); assert.equal(address.getHrp(), "xerd"); assert.equal(address.toBech32(), "xerd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruq9thc9j"); @@ -34,7 +34,7 @@ describe("test address", () => { it("should create empty address", async () => { const nobody = Address.empty(); - assert.isEmpty(nobody.hex()); + assert.isEmpty(nobody.toHex()); assert.isEmpty(nobody.toBech32()); assert.deepEqual(nobody.toJSON(), { bech32: "", pubkey: "" }); }); diff --git a/src/address.ts b/src/address.ts index 04686e77..4b1327b4 100644 --- a/src/address.ts +++ b/src/address.ts @@ -93,7 +93,7 @@ export class Address { } /** - * Use {@link newFromBech32} instead. + * @deprecated Use {@link newFromBech32} instead. */ static fromBech32(value: string): Address { // On this legacy flow, we do not accept addresses with custom hrp (in order to avoid behavioral breaking changes). @@ -113,7 +113,7 @@ export class Address { } /** - * Use {@link newFromHex} instead. + * @deprecated Use {@link newFromHex} instead. */ static fromHex(value: string, hrp?: string): Address { return Address.newFromHex(value, hrp); @@ -147,7 +147,7 @@ export class Address { } /** - * Use {@link toHex} instead. + * @deprecated Use {@link toHex} instead. */ hex(): string { return this.toHex(); @@ -165,7 +165,7 @@ export class Address { } /** - * Use {@link toBech32} instead. + * @deprecated Use {@link toBech32} instead. */ bech32(): string { return this.toBech32(); @@ -179,13 +179,13 @@ export class Address { return ""; } - let words = bech32.toWords(this.pubkey()); + let words = bech32.toWords(this.getPublicKey()); let address = bech32.encode(this.hrp, words); return address; } /** - * Use {@link getPublicKey} instead. + * @deprecated Use {@link getPublicKey} instead. */ pubkey(): Buffer { return this.getPublicKey(); @@ -249,7 +249,7 @@ export class Address { } /** - * Use {@link isSmartContract} instead. + * @deprecated Use {@link isSmartContract} instead. */ isContractAddress(): boolean { return this.isSmartContract(); diff --git a/src/compatibility.ts b/src/compatibility.ts index 357b724a..25d584aa 100644 --- a/src/compatibility.ts +++ b/src/compatibility.ts @@ -8,11 +8,11 @@ export class Compatibility { * For internal use only. */ static guardAddressIsSetAndNonZero(address: Address | undefined, context: string, resolution: string) { - if (!address || address.bech32() == "") { + if (!address || address.toBech32() == "") { console.warn( `${context}: address should be set; ${resolution}. In the future, this will throw an exception instead of emitting a WARN.`, ); - } else if (address.bech32() == Address.Zero().toBech32()) { + } else if (address.toBech32() == Address.Zero().toBech32()) { console.warn( `${context}: address should not be the 'zero' address (also known as the 'contracts deployment address'); ${resolution}. In the future, this will throw an exception instead of emitting a WARN.`, ); diff --git a/src/delegation/delegationTransactionsFactory.spec.ts b/src/delegation/delegationTransactionsFactory.spec.ts index 65a11930..a99916bb 100644 --- a/src/delegation/delegationTransactionsFactory.spec.ts +++ b/src/delegation/delegationTransactionsFactory.spec.ts @@ -25,7 +25,10 @@ describe("test delegation transactions factory", function () { transaction.sender, Address.newFromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2"), ); - assert.deepEqual(transaction.receiver, Address.fromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX, config.addressHrp)); + assert.deepEqual( + transaction.receiver, + Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX, config.addressHrp), + ); assert.isDefined(transaction.data); assert.deepEqual(transaction.data, Buffer.from("createNewDelegationContract@010f0cf064dd59200000@0a")); assert.equal(transaction.gasLimit, 60126500n); diff --git a/src/delegation/delegationTransactionsFactory.ts b/src/delegation/delegationTransactionsFactory.ts index bd71fcd3..94c688e9 100644 --- a/src/delegation/delegationTransactionsFactory.ts +++ b/src/delegation/delegationTransactionsFactory.ts @@ -31,7 +31,7 @@ export class DelegationTransactionsFactory { constructor(options: { config: IConfig }) { this.config = options.config; this.argSerializer = new ArgSerializer(); - this.delegationManagerAddress = Address.fromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX, this.config.addressHrp); + this.delegationManagerAddress = Address.newFromHex(DELEGATION_MANAGER_SC_ADDRESS_HEX, this.config.addressHrp); } createTransactionForNewDelegationContract( diff --git a/src/entrypoints/entrypoints.spec.ts b/src/entrypoints/entrypoints.spec.ts index f26e423e..78243760 100644 --- a/src/entrypoints/entrypoints.spec.ts +++ b/src/entrypoints/entrypoints.spec.ts @@ -42,8 +42,7 @@ describe("TestEntrypoint", () => { this.timeout(30000); const abi = await loadAbiRegistry("src/testdata/adder.abi.json"); const sender = Account.newFromPem(alicePem.pemFileText); - const accountAddress = new Address(sender.address); - sender.nonce = await entrypoint.recallAccountNonce(accountAddress); + sender.nonce = await entrypoint.recallAccountNonce(sender.address); const controller = entrypoint.createSmartContractController(abi); const bytecode = readFileSync("src/testdata/adder.wasm"); @@ -63,7 +62,7 @@ describe("TestEntrypoint", () => { assert.equal(outcome.contracts.length, 1); - const contractAddress = Address.fromBech32(outcome.contracts[0].address); + const contractAddress = Address.newFromBech32(outcome.contracts[0].address); const executeTransaction = await controller.createTransactionForExecute( sender, diff --git a/src/message.ts b/src/message.ts index 4d10df6c..03bbde1a 100644 --- a/src/message.ts +++ b/src/message.ts @@ -86,7 +86,7 @@ export class MessageComputer { let address: Address | undefined = undefined; if (packedMessage.address) { - address = Address.fromBech32(packedMessage.address); + address = Address.newFromBech32(packedMessage.address); } const version = packedMessage.version || DEFAULT_MESSAGE_VERSION; diff --git a/src/networkProviders/apiNetworkProvider.ts b/src/networkProviders/apiNetworkProvider.ts index 3d478476..5f16345e 100644 --- a/src/networkProviders/apiNetworkProvider.ts +++ b/src/networkProviders/apiNetworkProvider.ts @@ -66,7 +66,7 @@ export class ApiNetworkProvider implements INetworkProvider { } async getAccount(address: Address): Promise { - const response = await this.doGetGeneric(`accounts/${address.bech32()}`); + const response = await this.doGetGeneric(`accounts/${address.toBech32()}`); const account = AccountOnNetwork.fromHttpResponse(response); return account; } @@ -81,7 +81,7 @@ export class ApiNetworkProvider implements INetworkProvider { ): Promise { pagination = pagination || defaultPagination; - const url = `accounts/${address.bech32()}/tokens?${this.buildPaginationParams(pagination)}`; + const url = `accounts/${address.toBech32()}/tokens?${this.buildPaginationParams(pagination)}`; const response: any[] = await this.doGetGeneric(url); const tokens = response.map((item) => FungibleTokenOfAccountOnNetwork.fromHttpResponse(item)); @@ -96,7 +96,7 @@ export class ApiNetworkProvider implements INetworkProvider { ): Promise { pagination = pagination || defaultPagination; - const url = `accounts/${address.bech32()}/nfts?${this.buildPaginationParams(pagination)}`; + const url = `accounts/${address.toBech32()}/nfts?${this.buildPaginationParams(pagination)}`; const response: any[] = await this.doGetGeneric(url); const tokens = response.map((item) => NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(item)); @@ -109,7 +109,7 @@ export class ApiNetworkProvider implements INetworkProvider { address: Address, tokenIdentifier: string, ): Promise { - const response = await this.doGetGeneric(`accounts/${address.bech32()}/tokens/${tokenIdentifier}`); + const response = await this.doGetGeneric(`accounts/${address.toBech32()}/tokens/${tokenIdentifier}`); const tokenData = FungibleTokenOfAccountOnNetwork.fromHttpResponse(response); return tokenData; } @@ -120,7 +120,7 @@ export class ApiNetworkProvider implements INetworkProvider { nonce: number, ): Promise { const nonceAsHex = numberToPaddedHex(nonce); - const response = await this.doGetGeneric(`accounts/${address.bech32()}/nfts/${collection}-${nonceAsHex}`); + const response = await this.doGetGeneric(`accounts/${address.toBech32()}/nfts/${collection}-${nonceAsHex}`); const tokenData = NonFungibleTokenOfAccountOnNetwork.fromApiHttpResponse(response); return tokenData; } diff --git a/src/networkProviders/proxyNetworkProvider.ts b/src/networkProviders/proxyNetworkProvider.ts index f54ea831..63194705 100644 --- a/src/networkProviders/proxyNetworkProvider.ts +++ b/src/networkProviders/proxyNetworkProvider.ts @@ -59,13 +59,13 @@ export class ProxyNetworkProvider implements INetworkProvider { } async getAccount(address: Address): Promise { - const response = await this.doGetGeneric(`address/${address.bech32()}`); + const response = await this.doGetGeneric(`address/${address.toBech32()}`); const account = AccountOnNetwork.fromHttpResponse(response.account); return account; } async getGuardianData(address: Address): Promise { - const response = await this.doGetGeneric(`address/${address.bech32()}/guardian-data`); + const response = await this.doGetGeneric(`address/${address.toBech32()}/guardian-data`); const accountGuardian = GuardianData.fromHttpResponse(response.guardianData); return accountGuardian; } @@ -74,7 +74,7 @@ export class ProxyNetworkProvider implements INetworkProvider { address: Address, _pagination?: IPagination, ): Promise { - const url = `address/${address.bech32()}/esdt`; + const url = `address/${address.toBech32()}/esdt`; const response = await this.doGetGeneric(url); const responseItems: any[] = Object.values(response.esdts); // Skip NFTs / SFTs. @@ -90,7 +90,7 @@ export class ProxyNetworkProvider implements INetworkProvider { address: Address, _pagination?: IPagination, ): Promise { - const url = `address/${address.bech32()}/esdt`; + const url = `address/${address.toBech32()}/esdt`; const response = await this.doGetGeneric(url); const responseItems: any[] = Object.values(response.esdts); // Skip fungible tokens. @@ -108,7 +108,7 @@ export class ProxyNetworkProvider implements INetworkProvider { address: Address, tokenIdentifier: string, ): Promise { - const response = await this.doGetGeneric(`address/${address.bech32()}/esdt/${tokenIdentifier}`); + const response = await this.doGetGeneric(`address/${address.toBech32()}/esdt/${tokenIdentifier}`); const tokenData = FungibleTokenOfAccountOnNetwork.fromHttpResponse(response.tokenData); return tokenData; } @@ -119,7 +119,7 @@ export class ProxyNetworkProvider implements INetworkProvider { nonce: number, ): Promise { const response = await this.doGetGeneric( - `address/${address.bech32()}/nft/${collection}/nonce/${nonce.valueOf()}`, + `address/${address.toBech32()}/nft/${collection}/nonce/${nonce.valueOf()}`, ); const tokenData = NonFungibleTokenOfAccountOnNetwork.fromProxyHttpResponseByNonce(response.tokenData); return tokenData; @@ -185,7 +185,7 @@ export class ProxyNetworkProvider implements INetworkProvider { const encodedIdentifier = Buffer.from(identifier); const queryResponse = await this.queryContract({ - contract: Address.fromHex(ESDT_CONTRACT_ADDRESS_HEX), + contract: Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX), function: "getTokenProperties", arguments: [new Uint8Array(encodedIdentifier)], }); diff --git a/src/proto/serializer.spec.ts b/src/proto/serializer.spec.ts index 142a6570..226eb101 100644 --- a/src/proto/serializer.spec.ts +++ b/src/proto/serializer.spec.ts @@ -126,8 +126,8 @@ describe("serialize transactions", () => { const transaction = new Transaction({ nonce: 204n, value: 1000000000000000000n, - sender: Address.fromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"), - receiver: Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), + sender: Address.newFromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"), + receiver: Address.newFromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"), senderUsername: "carol", receiverUsername: "alice", gasLimit: 50000n, diff --git a/src/smartContracts/smartContractTransactionsFactory.ts b/src/smartContracts/smartContractTransactionsFactory.ts index e77ba6e9..42113e17 100644 --- a/src/smartContracts/smartContractTransactionsFactory.ts +++ b/src/smartContracts/smartContractTransactionsFactory.ts @@ -43,7 +43,7 @@ export class SmartContractTransactionsFactory { this.abi = options.abi; this.tokenComputer = new TokenComputer(); this.dataArgsBuilder = new TokenTransfersDataBuilder(); - this.contractDeployAddress = Address.fromHex(CONTRACT_DEPLOY_ADDRESS_HEX, this.config.addressHrp); + this.contractDeployAddress = Address.newFromHex(CONTRACT_DEPLOY_ADDRESS_HEX, this.config.addressHrp); } createTransactionForDeploy(sender: Address, options: resources.ContractDeployInput): Transaction { @@ -185,7 +185,7 @@ export class SmartContractTransactionsFactory { contract: Address; newOwner: Address; }): Transaction { - const dataParts = ["ChangeOwnerAddress", Address.fromBech32(options.newOwner.toBech32()).toHex()]; + const dataParts = ["ChangeOwnerAddress", options.newOwner.toHex()]; return new TransactionBuilder({ config: this.config, diff --git a/src/tokenManagement/tokenManagementTransactionsFactory.ts b/src/tokenManagement/tokenManagementTransactionsFactory.ts index b87ab9fb..bf902580 100644 --- a/src/tokenManagement/tokenManagementTransactionsFactory.ts +++ b/src/tokenManagement/tokenManagementTransactionsFactory.ts @@ -51,7 +51,7 @@ export class TokenManagementTransactionsFactory { this.argSerializer = new ArgSerializer(); this.trueAsString = "true"; this.falseAsString = "false"; - this.esdtContractAddress = Address.fromHex(ESDT_CONTRACT_ADDRESS_HEX, this.config.addressHrp); + this.esdtContractAddress = Address.newFromHex(ESDT_CONTRACT_ADDRESS_HEX, this.config.addressHrp); } createTransactionForIssuingFungible(sender: Address, options: resources.IssueFungibleInput): Transaction { diff --git a/src/tokenOperations/codec.ts b/src/tokenOperations/codec.ts index 3fc5cad6..5dcd62c1 100644 --- a/src/tokenOperations/codec.ts +++ b/src/tokenOperations/codec.ts @@ -22,7 +22,7 @@ export function bigIntToBuffer(value: BigNumber.Value): Buffer { return contractsCodecUtils.bigIntToBuffer(value); } -export { addressToHex, bigIntToHex, utf8ToHex } from "../utils.codec"; +export { bigIntToHex, utf8ToHex } from "../utils.codec"; export function bufferToHex(value: Buffer) { const hex = value.toString("hex"); diff --git a/src/transaction.local.net.spec.ts b/src/transaction.local.net.spec.ts index 9e95780b..4e8c7259 100644 --- a/src/transaction.local.net.spec.ts +++ b/src/transaction.local.net.spec.ts @@ -39,7 +39,7 @@ describe("test transaction", function () { let transactionOne = new Transaction({ sender: alice.address, receiver: bob.address, - value: TokenTransfer.newFromNativeAmount(42n).amount, + value: TokenTransfer.newFromNativeAmount(42000000000000000000n).amount, gasLimit: BigInt(network.MinGasLimit), chainID: network.ChainID, }); @@ -47,7 +47,7 @@ describe("test transaction", function () { let transactionTwo = new Transaction({ sender: alice.address, receiver: bob.address, - value: TokenTransfer.newFromNativeAmount(43n).amount, + value: TokenTransfer.newFromNativeAmount(43000000000000000000n).amount, gasLimit: BigInt(network.MinGasLimit), chainID: network.ChainID, }); @@ -73,7 +73,7 @@ describe("test transaction", function () { let newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString()); assert.deepEqual( - TokenTransfer.newFromNativeAmount(85n).amount, + TokenTransfer.newFromNativeAmount(85000000000000000000n).amount, BigInt(newBalanceOfBob.minus(initialBalanceOfBob).toString()), ); }); @@ -178,7 +178,7 @@ describe("test transaction", function () { const newBalanceOfBob = new BigNumber((await bob.getBalance(provider)).toString()); assert.deepEqual( - TokenTransfer.newFromNativeAmount(42n).amount, + TokenTransfer.newFromNativeAmount(42000000000000000000n).amount, BigInt(newBalanceOfBob.minus(initialBalanceOfBob).toString()), ); }); diff --git a/src/utils.codec.ts b/src/utils.codec.ts index ab64eed8..467f6d31 100644 --- a/src/utils.codec.ts +++ b/src/utils.codec.ts @@ -1,6 +1,5 @@ import BigNumber from "bignumber.js"; import * as contractsCodecUtils from "./abi/codec/utils"; -import { Address } from "./address"; export function numberToPaddedHex(value: bigint | number | BigNumber.Value) { let hexableNumber: { toString(radix?: number): string }; @@ -52,8 +51,3 @@ export function bigIntToHex(value: BigNumber.Value): string { return contractsCodecUtils.getHexMagnitudeOfBigInt(value); } - -export function addressToHex(address: Address): string { - const buffer = Address.fromBech32(address.toString()).pubkey(); - return buffer.toString("hex"); -}