From 3d2fe5fd03e78ae93893c2041786a12a31787104 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 7 Feb 2020 00:32:21 +0100 Subject: [PATCH] Use Pen in RestClient tests --- packages/sdk/package.json | 2 - packages/sdk/src/restclient.spec.ts | 72 ++++++++++------------------- 2 files changed, 24 insertions(+), 50 deletions(-) diff --git a/packages/sdk/package.json b/packages/sdk/package.json index a05d2ce8..55a04c7f 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -43,7 +43,5 @@ "axios": "^0.19.0" }, "devDependencies": { - "@iov/bcp": "^2.0.0-alpha.7", - "@iov/keycontrol": "^2.0.0-alpha.7" } } diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 405b780e..e64b3110 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -1,12 +1,11 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { ChainId, Identity, PrehashType, SignableBytes } from "@iov/bcp"; import { Random } from "@iov/crypto"; import { Bech32, Encoding } from "@iov/encoding"; -import { HdPaths, Secp256k1HdWallet } from "@iov/keycontrol"; import { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; import { leb128Encode } from "./leb128.spec"; import { Attribute, Log, parseLogs } from "./logs"; +import { Pen, Secp256k1Pen } from "./pen"; import { PostTxsResponse, RestClient } from "./restclient"; import contract from "./testdata/contract.json"; import cosmoshub from "./testdata/cosmoshub.json"; @@ -26,9 +25,9 @@ const { fromBase64, fromHex, toAscii, toBase64, toHex } = Encoding; const httpUrl = "http://localhost:1317"; const defaultNetworkId = "testing"; -const faucetMnemonic = - "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; -const faucetPath = HdPaths.cosmos(0); +const faucetPen = new Secp256k1Pen( + "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone", +); const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6"; const emptyAddress = "cosmos1ltkhnmdcqemmd2tkhnx7qx66tq7e0wykw2j85k"; @@ -96,11 +95,7 @@ function findAttribute(logs: readonly Log[], eventType: "message" | "transfer", return out; } -async function uploadContract( - client: RestClient, - wallet: Secp256k1HdWallet, - signer: Identity, -): Promise { +async function uploadContract(client: RestClient, pen: Pen): Promise { const memo = "My first contract on chain"; const theMsg: MsgStoreCode = { type: "wasm/store-code", @@ -122,17 +117,15 @@ async function uploadContract( }; const account = (await client.authAccounts(faucetAddress)).result.value; - const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account) as SignableBytes; - const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); - const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); + const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account); + const signature = encodeSecp256k1Signature(await pen.getPubkey(), await pen.createSignature(signBytes)); const signedTx = makeSignedTx(theMsg, fee, memo, signature); return client.postTx(marshalTx(signedTx)); } async function instantiateContract( client: RestClient, - wallet: Secp256k1HdWallet, - signer: Identity, + pen: Pen, codeId: number, beneficiaryAddress: string, transferAmount: readonly Coin[], @@ -161,17 +154,15 @@ async function instantiateContract( }; const account = (await client.authAccounts(faucetAddress)).result.value; - const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account) as SignableBytes; - const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); - const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); + const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account); + const signature = encodeSecp256k1Signature(await pen.getPubkey(), await pen.createSignature(signBytes)); const signedTx = makeSignedTx(theMsg, fee, memo, signature); return client.postTx(marshalTx(signedTx)); } async function executeContract( client: RestClient, - wallet: Secp256k1HdWallet, - signer: Identity, + pen: Pen, contractAddress: string, ): Promise { const memo = "Time for action"; @@ -195,9 +186,8 @@ async function executeContract( }; const account = (await client.authAccounts(faucetAddress)).result.value; - const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account) as SignableBytes; - const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); - const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); + const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account); + const signature = encodeSecp256k1Signature(await pen.getPubkey(), await pen.createSignature(signBytes)); const signedTx = makeSignedTx(theMsg, fee, memo, signature); return client.postTx(marshalTx(signedTx)); } @@ -240,8 +230,6 @@ describe("RestClient", () => { describe("post", () => { it("can send tokens", async () => { pendingWithoutCosmos(); - const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic); - const signer = await wallet.createIdentity("abc" as ChainId, faucetPath); const memo = "My first contract on chain"; const theMsg: MsgSend = { @@ -271,9 +259,11 @@ describe("RestClient", () => { const client = new RestClient(httpUrl); const account = (await client.authAccounts(faucetAddress)).result.value; - const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account) as SignableBytes; - const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); - const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); + const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account); + const signature = encodeSecp256k1Signature( + await faucetPen.getPubkey(), + await faucetPen.createSignature(signBytes), + ); const signedTx = makeSignedTx(theMsg, fee, memo, signature); const result = await client.postTx(marshalTx(signedTx)); // console.log("Raw log:", result.raw_log); @@ -282,8 +272,6 @@ describe("RestClient", () => { it("can upload, instantiate and execute wasm", async () => { pendingWithoutCosmos(); - const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic); - const signer = await wallet.createIdentity("abc" as ChainId, faucetPath); const client = new RestClient(httpUrl); const transferAmount: readonly Coin[] = [ @@ -303,7 +291,7 @@ describe("RestClient", () => { // upload { // console.log("Raw log:", result.raw_log); - const result = await uploadContract(client, wallet, signer); + const result = await uploadContract(client, faucetPen); expect(result.code).toBeFalsy(); const logs = parseSuccess(result.raw_log); const codeIdAttr = findAttribute(logs, "message", "code_id"); @@ -318,8 +306,7 @@ describe("RestClient", () => { { const result = await instantiateContract( client, - wallet, - signer, + faucetPen, codeId, beneficiaryAddress, transferAmount, @@ -338,7 +325,7 @@ describe("RestClient", () => { // execute { - const result = await executeContract(client, wallet, signer, contractAddress); + const result = await executeContract(client, faucetPen, contractAddress); expect(result.code).toBeFalsy(); // console.log("Raw log:", result.raw_log); const [firstLog] = parseSuccess(result.raw_log); @@ -356,8 +343,6 @@ describe("RestClient", () => { describe("query", () => { it("can list upload code", async () => { pendingWithoutCosmos(); - const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic); - const signer = await wallet.createIdentity("abc" as ChainId, faucetPath); const client = new RestClient(httpUrl); // check with contracts were here first to compare @@ -366,7 +351,7 @@ describe("RestClient", () => { const numExisting = existingInfos.length; // upload data - const result = await uploadContract(client, wallet, signer); + const result = await uploadContract(client, faucetPen); expect(result.code).toBeFalsy(); const logs = parseSuccess(result.raw_log); const codeIdAttr = findAttribute(logs, "message", "code_id"); @@ -387,8 +372,6 @@ describe("RestClient", () => { it("can list contracts and get info", async () => { pendingWithoutCosmos(); - const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic); - const signer = await wallet.createIdentity("abc" as ChainId, faucetPath); const client = new RestClient(httpUrl); const beneficiaryAddress = makeRandomAddress(); const transferAmount: readonly Coin[] = [ @@ -404,7 +387,7 @@ describe("RestClient", () => { if (existingInfos.length > 0) { codeId = existingInfos[existingInfos.length - 1].id; } else { - const uploaded = await uploadContract(client, wallet, signer); + const uploaded = await uploadContract(client, faucetPen); expect(uploaded.code).toBeFalsy(); const uploadLogs = parseSuccess(uploaded.raw_log); const codeIdAttr = findAttribute(uploadLogs, "message", "code_id"); @@ -414,14 +397,7 @@ describe("RestClient", () => { // create new instance and compare before and after const existingContracts = await client.listContractAddresses(); - const result = await instantiateContract( - client, - wallet, - signer, - codeId, - beneficiaryAddress, - transferAmount, - ); + const result = await instantiateContract(client, faucetPen, codeId, beneficiaryAddress, transferAmount); expect(result.code).toBeFalsy(); const logs = parseSuccess(result.raw_log); const contractAddressAttr = findAttribute(logs, "message", "contract_address");