@concordium/common-sdk
Advanced tools
Comparing version
/// <reference types="node" /> | ||
import { Buffer } from 'buffer/'; | ||
import { AccountTransaction, AccountTransactionSignature, BlockItemKind, TypedCredentialDeployment } from './types'; | ||
import { AccountTransaction, AccountTransactionSignature, BlockItemKind, SmartContractTypeValues, TypedCredentialDeployment } from './types'; | ||
import { Readable } from 'stream'; | ||
@@ -61,1 +61,8 @@ /** | ||
export declare function deserializeInitError(errorBytes: Buffer, moduleSchema: Buffer, contractName: string): any; | ||
/** | ||
* Given a binary value for a smart contract type, and the raw schema for that type, deserialize the value into the JSON representation. | ||
* @param value the value that should be deserialized. | ||
* @param rawSchema the schema for the type that the given value should be deserialized as | ||
* @returns the deserialized value | ||
*/ | ||
export declare function deserializeTypeValue(value: Buffer, rawSchema: Buffer): SmartContractTypeValues; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deserializeInitError = exports.deserializeReceiveError = exports.deserializeReceiveReturnValue = exports.deserializeTransaction = exports.deserializeContractState = exports.deserializeUint8 = void 0; | ||
exports.deserializeTypeValue = exports.deserializeInitError = exports.deserializeReceiveError = exports.deserializeReceiveReturnValue = exports.deserializeTransaction = exports.deserializeContractState = exports.deserializeUint8 = void 0; | ||
const wasm = __importStar(require("@concordium/rust-bindings")); | ||
@@ -220,1 +220,12 @@ const buffer_1 = require("buffer/"); | ||
exports.deserializeInitError = deserializeInitError; | ||
/** | ||
* Given a binary value for a smart contract type, and the raw schema for that type, deserialize the value into the JSON representation. | ||
* @param value the value that should be deserialized. | ||
* @param rawSchema the schema for the type that the given value should be deserialized as | ||
* @returns the deserialized value | ||
*/ | ||
function deserializeTypeValue(value, rawSchema) { | ||
const deserializedValue = wasm.deserializeTypeValue(value.toString('hex'), rawSchema.toString('hex')); | ||
return JSON.parse(deserializedValue); | ||
} | ||
exports.deserializeTypeValue = deserializeTypeValue; |
@@ -13,3 +13,3 @@ import { sha256 } from './hash'; | ||
export { isAlias, getAlias } from './alias'; | ||
export { deserializeContractState, deserializeTransaction, deserializeReceiveReturnValue, deserializeReceiveError, deserializeInitError, } from './deserialization'; | ||
export { deserializeContractState, deserializeTransaction, deserializeReceiveReturnValue, deserializeReceiveError, deserializeInitError, deserializeTypeValue, } from './deserialization'; | ||
export * from './idProofs'; | ||
@@ -16,0 +16,0 @@ export * from './idProofTypes'; |
@@ -20,3 +20,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.calculateEnergyCost = exports.getAccountTransactionHandler = exports.ConcordiumGRPCClient = exports.JsonRpcClient = exports.HttpProvider = exports.isHex = exports.deserializeInitError = exports.deserializeReceiveError = exports.deserializeReceiveReturnValue = exports.deserializeTransaction = exports.deserializeContractState = exports.getAlias = exports.isAlias = exports.ModuleReference = exports.DataBlob = exports.TransactionExpiry = exports.CcdAmount = exports.AccountAddress = exports.CredentialRegistrationId = exports.sha256 = exports.serializeCredentialDeploymentPayload = exports.serializeAccountTransactionPayload = exports.serializeTypeValue = exports.getSignedCredentialDeploymentTransactionHash = exports.serializeCredentialDeploymentTransactionForSubmission = exports.serializeAccountTransactionForSubmission = exports.serializeUpdateContractParameters = exports.serializeInitContractParameters = exports.getCredentialForExistingAccountSignDigest = exports.getCredentialDeploymentTransactionHash = exports.getCredentialDeploymentSignDigest = exports.getAccountTransactionSignDigest = exports.getAccountTransactionHash = void 0; | ||
exports.calculateEnergyCost = exports.getAccountTransactionHandler = exports.ConcordiumGRPCClient = exports.JsonRpcClient = exports.HttpProvider = exports.isHex = exports.deserializeTypeValue = exports.deserializeInitError = exports.deserializeReceiveError = exports.deserializeReceiveReturnValue = exports.deserializeTransaction = exports.deserializeContractState = exports.getAlias = exports.isAlias = exports.ModuleReference = exports.DataBlob = exports.TransactionExpiry = exports.CcdAmount = exports.AccountAddress = exports.CredentialRegistrationId = exports.sha256 = exports.serializeCredentialDeploymentPayload = exports.serializeAccountTransactionPayload = exports.serializeTypeValue = exports.getSignedCredentialDeploymentTransactionHash = exports.serializeCredentialDeploymentTransactionForSubmission = exports.serializeAccountTransactionForSubmission = exports.serializeUpdateContractParameters = exports.serializeInitContractParameters = exports.getCredentialForExistingAccountSignDigest = exports.getCredentialDeploymentTransactionHash = exports.getCredentialDeploymentSignDigest = exports.getAccountTransactionSignDigest = exports.getAccountTransactionHash = void 0; | ||
const hash_1 = require("./hash"); | ||
@@ -61,2 +61,3 @@ Object.defineProperty(exports, "sha256", { enumerable: true, get: function () { return hash_1.sha256; } }); | ||
Object.defineProperty(exports, "deserializeInitError", { enumerable: true, get: function () { return deserialization_1.deserializeInitError; } }); | ||
Object.defineProperty(exports, "deserializeTypeValue", { enumerable: true, get: function () { return deserialization_1.deserializeTypeValue; } }); | ||
__exportStar(require("./idProofs"), exports); | ||
@@ -63,0 +64,0 @@ __exportStar(require("./idProofTypes"), exports); |
@@ -335,8 +335,3 @@ "use strict"; | ||
const serializedValue = wasm.serializeTypeValue(JSON.stringify(value), rawSchema.toString('hex')); | ||
try { | ||
return buffer_1.Buffer.from(serializedValue, 'hex'); | ||
} | ||
catch (e) { | ||
throw new Error('unable to deserialize value, due to: ' + serializedValue); // In this case serializedValue is the error message from the rust module | ||
} | ||
return buffer_1.Buffer.from(serializedValue, 'hex'); | ||
} | ||
@@ -343,0 +338,0 @@ exports.serializeTypeValue = serializeTypeValue; |
@@ -22,14 +22,14 @@ import { AccountInfo, AccountTransaction, AccountTransactionSignature } from './types'; | ||
* Helper function to sign a message. | ||
* Note that this function prepends the string "MyGoodPrepend" to ensure that the message is not a transaction. | ||
* Note that this function prepends the account address (32 bytes) and 8 zero-bytes to ensure that the message is not a transaction. | ||
* Note that the current prepend is temporary and will later be replaced. | ||
* @param message the message to sign, assumed to be utf8 encoded. | ||
* @param message the message to sign, assumed to be utf8 encoded string or a Uint8Array/buffer. | ||
* @param signer An object that handles the keys of the account, and performs the actual signing. | ||
*/ | ||
export declare function signMessage(account: AccountAddress, message: string, signer: AccountSigner): Promise<AccountTransactionSignature>; | ||
export declare function signMessage(account: AccountAddress, message: string | Uint8Array, signer: AccountSigner): Promise<AccountTransactionSignature>; | ||
/** | ||
* Helper function to verify a signed message. | ||
* @param message the message to sign, assumed to be utf8 encoded. | ||
* @param message the message to sign, assumed to be utf8 encoded string or a Uint8Array/buffer. | ||
* @param signature the signature of a message, from a specific account. | ||
* @param accountInfo the address and credentials of the account | ||
*/ | ||
export declare function verifyMessageSignature(message: string, signature: AccountTransactionSignature, accountInfo: Pick<AccountInfo, 'accountThreshold' | 'accountCredentials' | 'accountAddress'>): Promise<boolean>; | ||
export declare function verifyMessageSignature(message: string | Uint8Array, signature: AccountTransactionSignature, accountInfo: Pick<AccountInfo, 'accountThreshold' | 'accountCredentials' | 'accountAddress'>): Promise<boolean>; |
@@ -65,7 +65,7 @@ "use strict"; | ||
* @param account the address of the account that will sign this message. | ||
* @param message the message to sign, assumed to be utf8 encoded. | ||
* @param message the message to sign, assumed to be utf8 encoded string or a Uint8Array/buffer. | ||
*/ | ||
function getMessageDigest(account, message) { | ||
const prepend = buffer_1.Buffer.alloc(8, 0); | ||
const rawMessage = buffer_1.Buffer.from(message, 'utf8'); | ||
const rawMessage = typeof message === 'string' ? buffer_1.Buffer.from(message, 'utf8') : message; | ||
return (0, hash_1.sha256)([account.decodedAddress, prepend, rawMessage]); | ||
@@ -75,5 +75,5 @@ } | ||
* Helper function to sign a message. | ||
* Note that this function prepends the string "MyGoodPrepend" to ensure that the message is not a transaction. | ||
* Note that this function prepends the account address (32 bytes) and 8 zero-bytes to ensure that the message is not a transaction. | ||
* Note that the current prepend is temporary and will later be replaced. | ||
* @param message the message to sign, assumed to be utf8 encoded. | ||
* @param message the message to sign, assumed to be utf8 encoded string or a Uint8Array/buffer. | ||
* @param signer An object that handles the keys of the account, and performs the actual signing. | ||
@@ -87,3 +87,3 @@ */ | ||
* Helper function to verify a signed message. | ||
* @param message the message to sign, assumed to be utf8 encoded. | ||
* @param message the message to sign, assumed to be utf8 encoded string or a Uint8Array/buffer. | ||
* @param signature the signature of a message, from a specific account. | ||
@@ -90,0 +90,0 @@ * @param accountInfo the address and credentials of the account |
@@ -1246,1 +1246,4 @@ import { AccountAddress } from './types/accountAddress'; | ||
export declare type Network = 'Testnet' | 'Mainnet'; | ||
export declare type SmartContractTypeValues = { | ||
[key: string]: SmartContractTypeValues; | ||
} | SmartContractTypeValues[] | number | string | boolean; |
132
package.json
{ | ||
"name": "@concordium/common-sdk", | ||
"version": "6.3.0", | ||
"license": "Apache-2.0", | ||
"engines": { | ||
"node": ">=14.16.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Concordium/concordium-node-sdk-js", | ||
"directory": "packages/common" | ||
}, | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"/grpc/**/*", | ||
"/lib/**/*" | ||
], | ||
"devDependencies": { | ||
"@protobuf-ts/plugin": "2.8.1", | ||
"@types/bs58check": "^2.1.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/json-bigint": "^1.0.1", | ||
"@types/uuid": "^8.3.4", | ||
"@typescript-eslint/eslint-plugin": "^4.28.1", | ||
"@typescript-eslint/parser": "^4.28.1", | ||
"babel-jest": "^27.0.6", | ||
"eslint": "^7.29.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"grpc-tools": "^1.11.2", | ||
"grpc_tools_node_protoc_ts": "5.3.0", | ||
"husky": "^4.2.5", | ||
"jest": "^27.0.6", | ||
"lint-staged": "^12.0.2", | ||
"prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "^4.3.5" | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"tabWidth": 4 | ||
}, | ||
"scripts": { | ||
"generate-ts-v2": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts --ts_opt 'optimize_code_size,output_legacy_commonjs,output_javascript' --ts_out=grpc -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/v2/concordium/*.proto", | ||
"generate": "([ -e \"../../deps/concordium-base/concordium-grpc-api\" ] && yarn generate-ts-v2) || echo 'Please checkout submodules before building'", | ||
"lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", | ||
"lint-fix": "yarn --silent lint --fix; exit 0", | ||
"test": "jest", | ||
"build": "rm -rf grpc; mkdir -p grpc; yarn generate && tsc", | ||
"build-dev": "tsc" | ||
}, | ||
"dependencies": { | ||
"@concordium/rust-bindings": "0.10.0", | ||
"@grpc/grpc-js": "^1.3.4", | ||
"@noble/ed25519": "^1.7.1", | ||
"@protobuf-ts/runtime-rpc": "^2.8.2", | ||
"@scure/bip39": "^1.1.0", | ||
"bs58check": "^2.1.2", | ||
"buffer": "^6.0.3", | ||
"cross-fetch": "3.1.5", | ||
"hash.js": "^1.1.7", | ||
"iso-3166-1": "^2.1.1", | ||
"json-bigint": "^1.0.0", | ||
"uuid": "^8.3.2" | ||
} | ||
} | ||
"name": "@concordium/common-sdk", | ||
"version": "6.4.0", | ||
"license": "Apache-2.0", | ||
"engines": { | ||
"node": ">=14.16.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Concordium/concordium-node-sdk-js", | ||
"directory": "packages/common" | ||
}, | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"/grpc/**/*", | ||
"/lib/**/*" | ||
], | ||
"devDependencies": { | ||
"@protobuf-ts/plugin": "2.8.1", | ||
"@types/bs58check": "^2.1.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/json-bigint": "^1.0.1", | ||
"@types/uuid": "^8.3.4", | ||
"@typescript-eslint/eslint-plugin": "^4.28.1", | ||
"@typescript-eslint/parser": "^4.28.1", | ||
"babel-jest": "^27.0.6", | ||
"eslint": "^7.29.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"grpc-tools": "^1.11.2", | ||
"grpc_tools_node_protoc_ts": "5.3.0", | ||
"husky": "^4.2.5", | ||
"jest": "^27.0.6", | ||
"lint-staged": "^12.0.2", | ||
"prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "^4.3.5" | ||
}, | ||
"prettier": { | ||
"singleQuote": true, | ||
"tabWidth": 4 | ||
}, | ||
"scripts": { | ||
"generate-ts-v2": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts --ts_opt 'optimize_code_size,output_legacy_commonjs,output_javascript' --ts_out=grpc -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/v2/concordium/*.proto", | ||
"generate": "([ -e \"../../deps/concordium-base/concordium-grpc-api\" ] && yarn generate-ts-v2) || echo 'Please checkout submodules before building'", | ||
"lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", | ||
"lint-fix": "yarn --silent lint --fix; exit 0", | ||
"test": "jest", | ||
"build": "rm -rf grpc; mkdir -p grpc; yarn generate && tsc", | ||
"build-dev": "tsc" | ||
}, | ||
"dependencies": { | ||
"@concordium/rust-bindings": "0.11.0", | ||
"@grpc/grpc-js": "^1.3.4", | ||
"@noble/ed25519": "^1.7.1", | ||
"@protobuf-ts/runtime-rpc": "^2.8.2", | ||
"@scure/bip39": "^1.1.0", | ||
"bs58check": "^2.1.2", | ||
"buffer": "^6.0.3", | ||
"cross-fetch": "3.1.5", | ||
"hash.js": "^1.1.7", | ||
"iso-3166-1": "^2.1.1", | ||
"json-bigint": "^1.0.0", | ||
"uuid": "^8.3.2" | ||
} | ||
} |
@@ -673,2 +673,13 @@ # Common | ||
The message can either be a utf8 encoded string or a Uint8Array directly containing the message bytes. | ||
## Deserialize smart contract types with only the specific type's schema | ||
The SDK exposes a general function to deserialize smart contract values from binary format to their JSON representation. In the previous sections the schema used was assumed to be the schema for an entire module, this function can be used with the schema containing only the specific type of the parameter, return value, event or error. | ||
``` | ||
const deserializedValue = deserializeTypeValue(serializedValue, rawTypeSchema); | ||
``` | ||
Note that the specific schema can be obtained using [cargo-concordium](https://developer.concordium.software/en/mainnet/smart-contracts/guides/setup-tools.html#cargo-concordium)'s `schema-json` command, and specifically for parameters, this SDK exposes functions for that, check [the serialize parameters with only the specific types schema section](serialize-parameters-with-only-the-specific-types-schema) for those. | ||
# Identity proofs | ||
@@ -675,0 +686,0 @@ ## Build Statement |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1169548
2.69%92
1.1%26169
1.97%834
1.34%0
-100%+ Added
- Removed