Skip to content

Commit

Permalink
Merge pull request #2 from blockfrost/fix/simple-tx-example
Browse files Browse the repository at this point in the history
Fix/simple tx example
  • Loading branch information
slowbackspace committed May 23, 2023
2 parents 5bfc73c + 2fb3f37 commit d46452e
Show file tree
Hide file tree
Showing 11 changed files with 710 additions and 107 deletions.
10 changes: 10 additions & 0 deletions examples/asset-explorer/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion examples/asset-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type-check": "tsc --project tsconfig.json"
},
"dependencies": {
"@blockfrost/blockfrost-js": "4.0.0",
"@blockfrost/blockfrost-js": "5.3.1",
"@emurgo/cip14-js": "^2.0.0",
"@types/react-select": "^4.0.4",
"axios": "^0.21.1",
Expand Down
39 changes: 18 additions & 21 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
{
"name": "@blockfrost/example-basic",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rm -rf dist && tsc -b",
"lint": "eslint ./src/**/*.ts",
"start-api": "yarn build && yarn node ./dist/api.js",
"run-example:api": "yarn start-api",
"start-ipfs": "yarn build && yarn node ./dist/ipfs.js",
"run-example:ipfs": "yarn start-ipfs"
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.1.0",
"@emurgo/cardano-serialization-lib-nodejs": "^10.1.0",
"bip39": "^3.0.4"
},
"devDependencies": {
"@types/bip39": "^3.0.0",
"@types/node": "^14.6.0",
"typescript": "^4.7.2"
}
"name": "@blockfrost/example-basic",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rm -rf dist && tsc -b",
"lint": "eslint ./src/**/*.ts",
"start-api": "yarn build && yarn node ./dist/api.js",
"run-example:api": "yarn start-api",
"start-ipfs": "yarn build && yarn node ./dist/ipfs.js",
"run-example:ipfs": "yarn start-ipfs"
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.3.1"
},
"devDependencies": {
"@types/node": "^14.6.0",
"typescript": "^5.0.4"
}
}
6 changes: 3 additions & 3 deletions examples/simple-transaction/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"start": "yarn build && yarn node ./dist/index.js"
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.1.0",
"@emurgo/cardano-serialization-lib-nodejs": "^10.1.0",
"@blockfrost/blockfrost-js": "5.3.1",
"@emurgo/cardano-serialization-lib-nodejs": "^11.4.0",
"bip39": "^3.0.4"
},
"devDependencies": {
"@types/bip39": "^3.0.0",
"@types/node": "^14.6.0",
"typescript": "^4.7.2"
"typescript": "^5.0.4"
}
}
20 changes: 10 additions & 10 deletions examples/simple-transaction/src/helpers/key.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs';
import { mnemonicToEntropy } from 'bip39';
import * as CardanoWasm from "@emurgo/cardano-serialization-lib-nodejs";
import { mnemonicToEntropy } from "bip39";

const harden = (num: number): number => {
return 0x80000000 + num;
};

export const deriveAddressPrvKey = (
bipPrvKey: CardanoWasm.Bip32PrivateKey,
testnet: boolean,
testnetPreview: boolean
): {
signKey: CardanoWasm.PrivateKey;
address: string;
} => {
const networkId = testnet
? CardanoWasm.NetworkInfo.testnet().network_id()
const networkId = testnetPreview
? CardanoWasm.NetworkInfo.testnet_preview().network_id()
: CardanoWasm.NetworkInfo.mainnet().network_id();
const accountIndex = 0;
const addressIndex = 0;
Expand All @@ -35,9 +35,9 @@ export const deriveAddressPrvKey = (
const baseAddress = CardanoWasm.BaseAddress.new(
networkId,
CardanoWasm.StakeCredential.from_keyhash(
utxoKey.to_public().to_raw_key().hash(),
utxoKey.to_public().to_raw_key().hash()
),
CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash()),
CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash())
);

const address = baseAddress.to_address().to_bech32();
Expand All @@ -46,13 +46,13 @@ export const deriveAddressPrvKey = (
};

export const mnemonicToPrivateKey = (
mnemonic: string,
mnemonic: string
): CardanoWasm.Bip32PrivateKey => {
const entropy = mnemonicToEntropy(mnemonic);

const rootKey = CardanoWasm.Bip32PrivateKey.from_bip39_entropy(
Buffer.from(entropy, 'hex'),
Buffer.from(''),
Buffer.from(entropy, "hex"),
Buffer.from("")
);

return rootKey;
Expand Down
29 changes: 14 additions & 15 deletions examples/simple-transaction/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@
import {
BlockFrostAPI,
BlockfrostServerError,
} from '@blockfrost/blockfrost-js';
} from "@blockfrost/blockfrost-js";

import { composeTransaction } from './helpers/composeTransaction';
import { signTransaction } from './helpers/signTransaction';
import { deriveAddressPrvKey, mnemonicToPrivateKey } from './helpers/key';
import { UTXO } from './types';
import { composeTransaction } from "./helpers/composeTransaction";
import { signTransaction } from "./helpers/signTransaction";
import { deriveAddressPrvKey, mnemonicToPrivateKey } from "./helpers/key";
import { UTXO } from "./types";

// BIP39 mnemonic (seed) from which we will generate address to retrieve utxo from and private key used for signing the transaction
const MNEMONIC =
'maze riot drift silver field sadness shrimp affair whip embody odor damp';
"maze riot drift silver field sadness shrimp affair whip embody odor damp";

// Recipient address (needs to be Bech32)
const OUTPUT_ADDRESS =
'addr_test1qrzpr05qz7u7572hkyxl9gqrk90lgueftufaqk3glqswurq32vrcvj0rgef6s487ruu47me8uzp7cjvuuk2xsg4mtvsq50gf90';
"addr_test1qrzpr05qz7u7572hkyxl9gqrk90lgueftufaqk3glqswurq32vrcvj0rgef6s487ruu47me8uzp7cjvuuk2xsg4mtvsq50gf90";

// Amount sent to the recipient
const OUTPUT_AMOUNT = '1000000'; // 1 000 000 lovelaces = 1 ADA
const OUTPUT_AMOUNT = "1000000"; // 1 000 000 lovelaces = 1 ADA

if (!process.env.BLOCKFROST_PROJECT_ID) {
throw Error('Set env variable BLOCKFROST_PROJECT_ID');
throw Error("Set env variable BLOCKFROST_PROJECT_ID");
}

const client = new BlockFrostAPI({
projectId: process.env.BLOCKFROST_PROJECT_ID,
network: 'testnet',
});

const run = async () => {
// Derive an address (this is the address where you need to send ADA in order to have UTXO to actually make the transaction)
const bip32PrvKey = mnemonicToPrivateKey(MNEMONIC);
const testnet = true;
const { signKey, address } = deriveAddressPrvKey(bip32PrvKey, testnet);
const testnetPreview = true;
const { signKey, address } = deriveAddressPrvKey(bip32PrvKey, testnetPreview);
console.log(`Using address ${address}`);

// Retrieve utxo for the address
Expand All @@ -55,7 +54,7 @@ const run = async () => {
if (utxo.length === 0) {
console.log();
console.log(
`You should send ADA to ${address} to have enough funds to sent a transaction`,
`You should send ADA to ${address} to have enough funds to sent a transaction`
);
console.log();
}
Expand All @@ -67,7 +66,7 @@ const run = async () => {
const latestBlock = await client.blocksLatest();
const currentSlot = latestBlock.slot;
if (!currentSlot) {
throw Error('Failed to fetch slot number');
throw Error("Failed to fetch slot number");
}

// Prepare transaction
Expand All @@ -76,7 +75,7 @@ const run = async () => {
OUTPUT_ADDRESS,
OUTPUT_AMOUNT,
utxo,
currentSlot,
currentSlot
);

// Sign transaction
Expand Down
6 changes: 3 additions & 3 deletions examples/webhook-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"start": "yarn run build && yarn node ./dist/index.js"
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.1.0",
"@blockfrost/blockfrost-js": "5.3.1",
"express": "^4.18.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.25",
"tslib": "^2.4.0",
"typescript": "^4.7.2"
"tslib": "^2.5.2",
"typescript": "^5.0.4"
}
}
3 changes: 2 additions & 1 deletion examples/webhook-basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"esModuleInterop": true,
"strict": true,
"target": "es2017",
"skipLibCheck": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}
6 changes: 3 additions & 3 deletions examples/webhook-slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"start": "yarn run build && yarn node ./dist/index.js"
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.1.0",
"@blockfrost/blockfrost-js": "5.3.1",
"@slack/web-api": "^6.7.2",
"express": "^4.18.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.25",
"tslib": "^2.4.0",
"typescript": "^4.7.2"
"tslib": "^2.5.2",
"typescript": "^5.0.4"
}
}
3 changes: 2 additions & 1 deletion examples/webhook-slack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"esModuleInterop": true,
"strict": true,
"target": "es2017",
"skipLibCheck": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}

0 comments on commit d46452e

Please sign in to comment.