Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
refactor: use constants to get forgeFactory address
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Oct 18, 2021
1 parent cfbb608 commit 8962c7a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
9 changes: 8 additions & 1 deletion __tests__/functional/transaction-forging/__support__/nft.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Container } from "@arkecosystem/core-interfaces";
import { Crypto, Managers } from "@arkecosystem/crypto";
import { Crypto, Identities, Managers } from "@arkecosystem/crypto";
import { Builders } from "@uns/core-nft-crypto";
import { unsCrypto } from "@uns/crypto";
import * as path from "path";
import { TransactionFactory } from "../../../helpers";
import * as Fixtures from "../../../unit/uns-crypto/__fixtures__/index";
import { defaultInclude, setUp as setup, snoozeForBlock } from "./index";
import "./nft-jest-matchers";

Expand Down Expand Up @@ -42,6 +44,11 @@ export const setUp = async (options?: any): Promise<Container.IContainer> => {
// tslint:disable-next-line
const version = require(packageFilePath).version;

jest.spyOn(unsCrypto, "isForgeFactory").mockImplementation(tokenId => tokenId === Fixtures.issUnikId);
jest.spyOn(unsCrypto, "getForgeFactoryAddress").mockReturnValue(
Identities.Address.fromPassphrase(Fixtures.issPassphrase),
);

return setup(
{
network,
Expand Down
4 changes: 0 additions & 4 deletions __tests__/integration/uns/certified-nft-mint-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ describe("certifiedNftMint handler tests for token eco v2", () => {

beforeEach(async () => {
await database.reset();
jest.spyOn(nftRepository(), "findById").mockResolvedValue({
tokenId: Fixtures.issUnikId,
ownerId: Fixtures.issuerAddress,
});

forgeFactoryWallet = walletManager.findByAddress(Identities.Address.fromPassphrase(Fixtures.issPassphrase));
forgeFactoryWallet.publicKey = Fixtures.issKeys.publicKey;
Expand Down
4 changes: 0 additions & 4 deletions __tests__/integration/uns/certified-nft-mint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ describe("certifiedNftMint handler tests", () => {

beforeEach(async () => {
await database.reset();
jest.spyOn(nftRepository(), "findById").mockResolvedValue({
tokenId: Fixtures.issUnikId,
ownerId: Fixtures.issuerAddress,
});

forgeFactoryWallet = walletManager.findByAddress(Identities.Address.fromPassphrase(Fixtures.issPassphrase));
forgeFactoryWallet.publicKey = Fixtures.issKeys.publicKey;
Expand Down
5 changes: 1 addition & 4 deletions __tests__/integration/uns/certified-nft-transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Database, State } from "@arkecosystem/core-interfaces";
import { WalletManager } from "@arkecosystem/core-state/src/wallets";
import { Identities, Networks, Utils } from "@arkecosystem/crypto";
import { DIDTypes } from "@uns/crypto";
import * as transactionHelpers from "@uns/uns-transactions/dist/handlers/utils";
import * as support from "../../functional/transaction-forging/__support__";
import * as NftSupport from "../../functional/transaction-forging/__support__/nft";
import { NFTTransactionFactory } from "../../helpers/nft-transaction-factory";
Expand Down Expand Up @@ -52,7 +51,7 @@ describe("certifiedNftTransfer handler tests", () => {
await database.reset();

factoryWallet = walletManager.findByAddress(Identities.Address.fromPassphrase(Fixtures.issPassphrase));
factoryWallet.publicKey = Identities.PublicKey.fromPassphrase(Fixtures.issPassphrase);
factoryWallet.publicKey = Fixtures.issKeys.publicKey;
walletManager.reindex(factoryWallet);

senderWallet = walletManager.findByAddress(Identities.Address.fromPassphrase(senderPassphrase));
Expand All @@ -65,8 +64,6 @@ describe("certifiedNftTransfer handler tests", () => {
recipientWallet = walletManager.findByAddress(Identities.Address.fromPassphrase(recipientPassphrase));
recipientWallet.publicKey = Identities.PublicKey.fromPassphrase(recipientPassphrase);

jest.spyOn(transactionHelpers, "getUnikOwnerAddress").mockResolvedValueOnce(factoryWallet.address);

jest.spyOn(nftRepo, "count").mockResolvedValueOnce(1);
// trick to mock private function
jest.spyOn(StateBuilder.prototype as any, "verifyNftTableConsistency").mockResolvedValueOnce({} as any);
Expand Down
42 changes: 41 additions & 1 deletion plugins/uns/uns-crypto/src/crypto/uns-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ class UnsCrypto {
* Return false if the issuer is not allowed to
*/
public verifyIssuerCredentials(issuerId: string): boolean {
return this.isForgeFactory(issuerId) || this.isUrlChecker(issuerId);
}

public isForgeFactory(tokenId: string): boolean {
const nftFactoryList: string[] = Managers.configManager.get("network.forgeFactory.unikidWhiteList");
return nftFactoryList.includes(tokenId);
}

public isUrlChecker(tokenId: string): boolean {
const urlCheckerList: string[] = Managers.configManager.getMilestone().urlCheckers || [];
return nftFactoryList.concat(urlCheckerList).includes(issuerId);
return urlCheckerList.includes(tokenId);
}

public serializeDiscloseDemand(payload: IDiscloseDemandPayload | IDiscloseDemandCertificationPayload): Buffer {
Expand All @@ -51,6 +59,38 @@ class UnsCrypto {

return buf.flip().toBuffer();
}

// This little hack is intended to reduce the call to the nft database.
// We call getUnikOwnerAddress of forgefactory at every certified transactions, especially at core bootstrap.
// Its imply that forge factory NFT should not be transferable
public getForgeFactoryAddress(tokenId: string): string | undefined {
const network: string = Managers.configManager.get("network.name");
switch (network) {
case "dalinet":
if (tokenId === "5f96dd359ab300e2c702a54760f4d74a11db076aa17575179d36e06d75c96511") {
return "DTRnY3JjFpAnCX1nFtF8M4v8X7WdvPYRxb";
}
break;
case "sandbox":
if (tokenId === "ed4eaa7299284ac4c0665d2edecc3064a33134566d94e6ffceebc1ab32251a04") {
return "SYD1xMGubmzW1qme18yxmVhpWxpoBJqv2v";
}

if (tokenId === "8ddab07ee0f49c76db32018a9305d7634264c39a89317f68a8069c161f4b8ec6") {
return "Scms9QGQnSe7SGegVAa61dR2GTUTa586R1";
}
break;
case "livenet":
if (tokenId === "fbfbe7d9e8c005f1a9937d9fd17c4ef7da2ff8037a71e6cb7847b302eda4d08a") {
return "USLEkzoncHwRdNyPVTPNJSjRXt6pYHzk43";
}
if (tokenId === "8bdd3a5358a8c6e0d96796048a87a8d83d076c73f52a0b850575e95f192376f9") {
return "UYcNuMK46RdFyGVmwrT3i11W16fE5N59j4";
}
break;
}
return undefined;
}
}

export const unsCrypto = new UnsCrypto();
5 changes: 4 additions & 1 deletion plugins/uns/uns-transactions/src/handlers/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IWalletManager } from "@arkecosystem/core-interfaces/dist/core-state";
import { Identities, Interfaces, Managers, Utils } from "@arkecosystem/crypto";
import { nftRepository, NftsManager } from "@uns/core-nft";
import { getCurrentNftAsset, getNftName, getTokenId } from "@uns/core-nft-crypto";
import { DIDTypes } from "@uns/crypto";
import { DIDTypes, unsCrypto } from "@uns/crypto";
import { VoucherAlreadyUsedError, WrongFeeError, WrongServiceCostError } from "../../errors";

export const EXPLICIT_PROP_KEY = "explicitValues";
Expand Down Expand Up @@ -64,6 +64,9 @@ export const revertExplicitValue = async (transaction: Interfaces.ITransactionDa
*
*/
export const getUnikOwnerAddress = async (tokenId: string): Promise<string> => {
if (unsCrypto.isForgeFactory(tokenId)) {
return unsCrypto.getForgeFactoryAddress(tokenId);
}
const unik = await nftRepository().findById(tokenId);
return unik.ownerId;
};
Expand Down

0 comments on commit 8962c7a

Please sign in to comment.