Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIVE-13040: polkadot code cleanup #7812

Merged
merged 10 commits into from
Sep 30, 2024
7 changes: 7 additions & 0 deletions .changeset/tasty-brooms-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ledgerhq/hw-app-polkadot": minor
"@ledgerhq/coin-polkadot": minor
"@ledgerhq/live-common": minor
---

polkadot code cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe("Polkadot Api", () => {
metadataHash: {
url: "https://api.zondax.ch/polkadot/node/metadata/hash",
},
runtimeUpgraded: false,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe("buildTransaction", () => {
metadataHash: {
url: "https://api.zondax.ch/polkadot/node/metadata/hash",
},
runtimeUpgraded: false,
};
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PolkadotAccount, Transaction } from "../types";
import { craftTransaction, type CreateExtrinsicArg } from "../logic";
import { isFirstBond, getNonce } from "./utils";
import coinConfig from "../config";

export const extractExtrinsicArg = (
account: PolkadotAccount,
Expand Down Expand Up @@ -29,12 +28,10 @@ export const buildTransaction = async (
transaction: Transaction,
forceLatestParams = false,
) => {
const runtimeUpgraded = coinConfig.getCoinConfig().runtimeUpgraded;
return craftTransaction(
account.freshAddress,
getNonce(account),
extractExtrinsicArg(account, transaction),
forceLatestParams,
runtimeUpgraded,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe("getEstimatedFees", () => {
metadataHash: {
url: "https://api.zondax.ch/polkadot/node/metadata/hash",
},
runtimeUpgraded: false,
};
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe("prepareTransaction", () => {
metadataHash: {
url: "https://api.zondax.ch/polkadot/node/metadata/hash",
},
runtimeUpgraded: false,
};
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe("signOperation", () => {
metadataHash: {
url: "https://api.zondax.ch/polkadot/node/metadata/hash",
},
runtimeUpgraded: false,
};
});
});
Expand Down
10 changes: 2 additions & 8 deletions libs/coin-modules/coin-polkadot/src/bridge/signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { buildTransaction } from "./buildTransaction";
import { calculateAmount } from "./utils";
import { signExtrinsic } from "../logic";
import polkadotAPI from "../network";
import coinConfig from "../config";

/**
* Sign Transaction with Ledger hardware
Expand All @@ -21,7 +20,6 @@ export const buildSignOperation =
({ account, deviceId, transaction }) =>
new Observable(o => {
async function main() {
const runtimeUpgraded = coinConfig.getCoinConfig().runtimeUpgraded;
o.next({
type: "device-signature-requested",
});
Expand All @@ -46,13 +44,9 @@ export const buildSignOperation =
.toU8a({
method: true,
});
let metadata = "";
if (runtimeUpgraded) {
const payloadString = Buffer.from(payload).toString("hex");
metadata = await polkadotAPI.shortenMetadata(payloadString);
}
const payloadString = Buffer.from(payload).toString("hex");
const metadata = await polkadotAPI.shortenMetadata(payloadString);
const r = await signerContext(deviceId, signer =>
// FIXME: the type of payload Uint8Array is not compatible with the signature of sign which accept a string
signer.sign(account.freshAddressPath, payload, metadata),
);

Expand Down
1 change: 0 additions & 1 deletion libs/coin-modules/coin-polkadot/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type PolkadotConfig = {
metadataHash: {
url: string;
};
runtimeUpgraded: boolean;
};

export type PolkadotCoinConfig = CurrencyConfig & PolkadotConfig;
Expand Down
34 changes: 7 additions & 27 deletions libs/coin-modules/coin-polkadot/src/logic/craftTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { loadPolkadotCrypto } from "./polkadot-crypto";
import polkadotAPI from "../network";
import { getAbandonSeedAddress } from "@ledgerhq/cryptoassets/index";
import { hexToU8a } from "@polkadot/util";
import { CoreTransasctionInfo, TransasctionPayloadInfo } from "../types";
import { TransasctionPayloadInfo } from "../types";

const EXTRINSIC_VERSION = 4;
// Default values for tx parameters, if the user doesn't specify any
Expand Down Expand Up @@ -169,7 +169,6 @@ export async function craftTransaction(
nonceToUse: number,
extractExtrinsicArg: CreateExtrinsicArg,
forceLatestParams: boolean = false,
runtimeUpgraded: boolean = false,
): Promise<CoreTransaction> {
await loadPolkadotCrypto();

Expand All @@ -180,16 +179,13 @@ export async function craftTransaction(
// Get the correct extrinsics params depending on transaction
const extrinsicParams = getExtrinsicParams(extractExtrinsicArg);

const blockNumber = registry.createType("BlockNumber", info.blockNumber).toHex();
const era = registry
.createType("ExtrinsicEra", {
current: info.blockNumber,
period: DEFAULTS.eraPeriod,
})
.toHex();
const nonce = registry.createType("Compact<Index>", nonceToUse).toHex();
const specVersion = registry.createType("u32", info.specVersion).toHex();
const tip = registry.createType("Compact<Balance>", DEFAULTS.tip).toHex();
const transactionVersion = registry.createType("u32", info.transactionVersion).toHex();
const methodFunction = extrinsics[extrinsicParams.pallet][extrinsicParams.name];
const methodArgs = methodFunction.meta.args;
Expand All @@ -209,36 +205,20 @@ export async function craftTransaction(
).toHex();

const { blockHash, genesisHash } = info;
let unsigned: CoreTransasctionInfo | TransasctionPayloadInfo = {
const metadataHash = await polkadotAPI.metadataHash();
const unsigned: TransasctionPayloadInfo = {
address,
blockHash,
blockNumber,
era,
genesisHash,
method,
nonce,
signedExtensions: registry.signedExtensions,
specVersion,
tip,
nonce: nonceToUse,
transactionVersion,
specVersion,
version: EXTRINSIC_VERSION,
metadataHash: hexToU8a("01" + metadataHash),
mode: 1,
};
if (runtimeUpgraded) {
const metadataHash = await polkadotAPI.metadataHash();
unsigned = {
address,
blockHash,
era,
genesisHash,
method,
nonce: nonceToUse,
transactionVersion,
specVersion,
version: EXTRINSIC_VERSION,
metadataHash: hexToU8a("01" + metadataHash),
mode: 1,
} as TransasctionPayloadInfo;
}

return {
registry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypeRegistry } from "@polkadot/types";
import { u8aConcat } from "@polkadot/util";
import { CoreTransasctionInfo, TransasctionPayloadInfo } from "../types";
import { TransasctionPayloadInfo } from "../types";

/**
* Serialize a signed transaction in a format that can be submitted over the
Expand All @@ -12,7 +12,7 @@ import { CoreTransasctionInfo, TransasctionPayloadInfo } from "../types";
* @param registry - Registry used for constructing the payload.
*/
export const signExtrinsic = async (
unsigned: CoreTransasctionInfo | TransasctionPayloadInfo,
unsigned: TransasctionPayloadInfo,
signature: any,
registry: TypeRegistry,
): Promise<string> => {
Expand All @@ -35,7 +35,7 @@ export const signExtrinsic = async (
* @param registry - Registry used for constructing the payload.
*/
export const fakeSignExtrinsic = async (
unsigned: CoreTransasctionInfo | TransasctionPayloadInfo,
unsigned: TransasctionPayloadInfo,
registry: TypeRegistry,
): Promise<string> => {
const fakeSignature = u8aConcat(new Uint8Array([1]), new Uint8Array(64).fill(0x42));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe("sidecar integration test", () => {
metadataHash: {
url: "https://api.zondax.ch/polkadot/node/metadata/hash",
},
runtimeUpgraded: false,
}));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe("getAccount", () => {
metadataHash: {
url: "",
},
runtimeUpgraded: false,
}));

mockServer.listen();
Expand Down Expand Up @@ -112,7 +111,6 @@ describe("getRegistry", () => {
metadataHash: {
url: "",
},
runtimeUpgraded: false,
}));

mockServer.listen();
Expand Down
6 changes: 1 addition & 5 deletions libs/coin-modules/coin-polkadot/src/signer/getAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import { GetAddressFn } from "@ledgerhq/coin-framework/bridge/getAddressWrapper"
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import { GetAddressOptions } from "@ledgerhq/coin-framework/derivation";
import type { PolkadotSigner } from "../types";
import coinConfig from "../config";

const getAddress = (signerContext: SignerContext<PolkadotSigner>): GetAddressFn => {
return async (deviceId: string, { path, verify }: GetAddressOptions) => {
const runtimeUpgraded = coinConfig.getCoinConfig().runtimeUpgraded;
const r = await signerContext(deviceId, signer =>
signer.getAddress(path, 0, verify, runtimeUpgraded),
);
const r = await signerContext(deviceId, signer => signer.getAddress(path, 0, verify));
return {
address: r.address,
publicKey: r.pubKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ const coinConfig: PolkadotCoinConfig = {
metadataHash: {
url: "https://polkadot-metadata-shortener.api.live.ledger.com/node/metadata/hash",
},
runtimeUpgraded: true,
};

const subscriptions: any[] = [];
Expand Down
19 changes: 1 addition & 18 deletions libs/coin-modules/coin-polkadot/src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@ export type ExplorerExtrinsic = {
};
};

/// cf. ExtrinsicPayloadValue
export type CoreTransasctionInfo = {
// before runtime upgrade
address: string;
blockHash: string;
blockNumber: `0x${string}`;
era: `0x${string}`;
genesisHash: string;
method: `0x${string}`;
nonce: `0x${string}`;
signedExtensions: string[];
specVersion: `0x${string}`;
tip: `0x${string}`;
transactionVersion: `0x${string}`;
version: number;
};

export type TransasctionPayloadInfo = {
// after runtime upgrade
address: string;
Expand All @@ -87,5 +70,5 @@ export type TransasctionPayloadInfo = {

export type CoreTransaction = {
registry: TypeRegistry;
unsigned: CoreTransasctionInfo | TransasctionPayloadInfo;
unsigned: TransasctionPayloadInfo;
};
1 change: 0 additions & 1 deletion libs/coin-modules/coin-polkadot/src/types/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface PolkadotSigner {
path: string,
ss58prefix: number,
showAddrInDevice?: boolean,
runtimeUpgraded?: boolean,
): Promise<PolkadotAddress>;
sign(path: string, message: Uint8Array, metadata: string): Promise<PolkadotSignature>;
}
1 change: 0 additions & 1 deletion libs/ledger-live-common/src/families/polkadot/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const polkadotConfig: Record<string, ConfigInfo> = {
metadataHash: {
url: "https://polkadot-metadata-shortener.api.live.ledger.com/node/metadata/hash",
},
runtimeUpgraded: true,
},
},
};
1 change: 0 additions & 1 deletion libs/ledgerjs/packages/hw-app-polkadot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const polkadot = new Polkadot(transport)
* `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
* `ss58prefix` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (optional, default `0`)
* `showAddrInDevice` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** if true, user must valid if the address is correct on the device (optional, default `false`)
* `runtimeUpgraded` (optional, default `false`)

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{pubKey: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), address: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), return\_code: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)}>**&#x20;

Expand Down
Loading
Loading