symbol-sdk
TypeScript icon, indicating that this package has built-in type declarations

3.2.0 • Public • Published

Symbol-SDK

lint test vectors

JavaScript SDK for interacting with the Symbol and NEM blockchains.

Most common functionality is grouped under facades so that the same programming paradigm can be used for interacting with both Symbol and NEM.

Sending a Transaction

To send a transaction, first create a facade for the desired network:

Symbol

import { PrivateKey } from 'symbol-sdk';
import { SymbolFacade } from 'symbol-sdk/symbol';

const facade = new SymbolFacade('testnet');

NEM

import { PrivateKey } from 'symbol-sdk';
import { NemFacade } from 'symbol-sdk/nem';

const facade = new NemFacade('testnet');

Second, describe the transaction using JavaScript object syntax. For example, a transfer transaction can be described as follows:

Symbol

const transaction = facade.transactionFactory.create({
	type: 'transfer_transaction_v1',
	signerPublicKey: '87DA603E7BE5656C45692D5FC7F6D0EF8F24BB7A5C10ED5FDA8C5CFBC49FCBC8',
	fee: 1000000n,
	deadline: 41998024783n,
	recipientAddress: 'TCHBDENCLKEBILBPWP3JPB2XNY64OE7PYHHE32I',
	mosaics: [
		{ mosaicId: 0x7CDF3B117A3C40CCn, amount: 1000000n }
	]
});

NEM

const transaction = facade.transactionFactory.create({
	type: 'transfer_transaction_v1',
	signerPublicKey: 'A59277D56E9F4FA46854F5EFAAA253B09F8AE69A473565E01FD9E6A738E4AB74',
	fee: 0x186A0n,
	timestamp: 191205516,
	deadline: 191291916,
	recipientAddress: 'TALICE5VF6J5FYMTCB7A3QG6OIRDRUXDWJGFVXNW',
	amount: 5100000n
});

Third, sign the transaction and attach the signature:

const privateKey = new PrivateKey('EDB671EB741BD676969D8A035271D1EE5E75DF33278083D877F23615EB839FEC');
const signature = facade.signTransaction(new facade.static.KeyPair(privateKey), transaction);

const jsonPayload = facade.transactionFactory.static.attachSignature(transaction, signature);;

Finally, send the payload to the desired network using the specified node endpoint:

Symbol: PUT /transactions
NEM: POST /transaction/announce

Usage Environments

Node

Symbol-sdk is written node-first and published via npm, so simply install the package and import 'symbol-sdk':

npm install symbol-sdk
import { PrivateKey } from 'symbol-sdk';
import { KeyPair } from 'symbol-sdk/symbol';

const privateKey = new PrivateKey('EDB671EB741BD676969D8A035271D1EE5E75DF33278083D877F23615EB839FEC');
console.log(`Private Key: ${privateKey.toString()}`);

const keyPair = new KeyPair(privateKey);
console.log(`Public Key: ${keyPair.publicKey.toString()}`);

Browser

Symbol-sdk is alternatively published as a bundled file, which can be imported directly for browser usage:

<script type="module">
	import { core, /* nem, */ symbol } from './node_modules/symbol-sdk/dist/bundle.web.js';

	const { PrivateKey } = core;
	const { KeyPair } = symbol;

	const privateKey = new PrivateKey('EDB671EB741BD676969D8A035271D1EE5E75DF33278083D877F23615EB839FEC');
	console.log(`Private Key: ${privateKey.toString()}`);

	const keyPair = new KeyPair(privateKey);
	console.log(`Public Key: ${keyPair.publicKey.toString()}`);
</script>

Web Application / External Bundler

If you want to use symbol-sdk within a browser application and/or are using a bundler, additional configuration of the bundler is required.

For Webpack, the following configuration needs to be added:

export default {
	// ...
	plugins: [
		// configure browser replacements for node process and Buffer libraries
		new webpack.ProvidePlugin({
			process: 'process/browser',
			Buffer: ['buffer', 'Buffer']
		}),
		// use a browser-optimized wasm for Ed25519 crypto operrations
		new webpack.NormalModuleReplacementPlugin(
			/symbol-crypto-wasm-node/,
			`../../../symbol-crypto-wasm-web/symbol_crypto_wasm.js`
		)
	],

	// configure browser polyfills for node crypto, path and stream libraries
	resolve: {
		extensions: ['.js'],
		fallback: {
			crypto: 'crypto-browserify',
			path: 'path-browserify',
			stream: 'stream-browserify'
		}
	},

	experiments: {
		// enable async loading of wasm files
		asyncWebAssembly: true,
		topLevelAwait: true
	}
	// ...
}

If everything is set up correctly, the same syntax as the Node example can be used.

NEM Cheat Sheet

In order to simplify the learning curve for NEM and Symbol usage, the SDK uses Symbol terminology for shared Symbol and NEM concepts. Where appropriate, NEM terminology is replaced with Symbol terminology, including the names of many of the NEM transactions. The mapping of NEM transactions to SDK descriptors can be found in the following table:

NEM name (used in docs) SDK descriptor name
ImportanceTransfer transaction account_key_link_transaction_v1
MosaicDefinitionCreation transaction mosaic_definition_transaction_v1
MosaicSupplyChange transaction mosaic_supply_change_transaction_v1
MultisigAggregateModification transaction multisig_account_modification_transaction_v1
multisig_account_modification_transaction_v2
MultisigSignature transaction or Cosignature transaction cosignature_v1
Multisig transaction multisig_transaction_v1
ProvisionNamespace transaction namespace_registration_transaction_v1
Transfer transaction transfer_transaction_v1
transfer_transaction_v2

Dependents (41)

Package Sidebar

Install

npm i symbol-sdk

Weekly Downloads

1,408

Version

3.2.0

License

MIT

Unpacked Size

8.59 MB

Total Files

93

Last publish

Collaborators

  • symbol-npm
  • anthony14w
  • wayonblair
  • rg911