Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c2bbebe
commit for basic polkadotChainState
ryuheimat Feb 2, 2021
4513bfc
commit for basic model of polkadot plugin
ryuheimat Feb 8, 2021
b79ff03
verify signature
ryuheimat Feb 8, 2021
ed16676
add fetchbalance to the chainpolkadotv1
ryuheimat Feb 9, 2021
a248b02
add some functions to chainpolkadotv1
ryuheimat Feb 9, 2021
aefe80a
complete polkadotAccount, polkadotCreateAccount class and example
ryuheimat Feb 15, 2021
0a1b5ba
change manifest
ryuheimat Mar 2, 2021
260e158
algo - transaction options now supports expireSeconds
traylewin Mar 2, 2021
3ce0a64
WIP - polkadot
traylewin Mar 2, 2021
27b6f5f
add isHexString helper
traylewin Mar 8, 2021
aa06a33
polka - reorganized cryptoModelHelpers
traylewin Mar 8, 2021
6215c44
polka - added constants
traylewin Mar 8, 2021
f0d5554
polka - WIP adding crypto functions
traylewin Mar 8, 2021
ab4d7c6
polka - added crypto models and helpers
traylewin Mar 8, 2021
80e3dfa
polka - WIP updating generateKeys, etc
traylewin Mar 8, 2021
fdca4d6
polkda - WIP adding main fn into polkadotCrypto
ryuheimat Mar 9, 2021
e9ebcef
polka - drop PolkadotCurve for CryptoCurve
traylewin Mar 9, 2021
3fc0da1
polka - refactor generateKeys()
traylewin Mar 9, 2021
321bd26
polka - WIP account example
traylewin Mar 9, 2021
6b1f827
polka - update PolkadotNativeInfo members
traylewin Mar 9, 2021
570246f
polka - WIP adding crypto.ts example
ryuheimat Mar 10, 2021
c592b4f
polka - WIP clean up
ryuheimat Mar 10, 2021
27b896a
polka - remove toValidKeypairType
traylewin Mar 10, 2021
a958d97
add not about node version
traylewin Mar 10, 2021
53c8f31
polka - update polkdot libraries versions
traylewin Mar 10, 2021
9660c66
polka - type polkadot chain in example
traylewin Mar 10, 2021
f758cd9
add ChainFeature type
traylewin Mar 10, 2021
dcdeade
polka - use ChainFeature for supportsFee
traylewin Mar 10, 2021
8bf0844
polka - use PolkadotChainInfo type
traylewin Mar 10, 2021
82ebc7f
polka - add hasFeature to chainState
traylewin Mar 10, 2021
a41e1cf
polka - add todo
traylewin Mar 10, 2021
a65592b
Merge pull request #161 from API-market/feature/polkadot-plugin-tray
traylewin Mar 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Just install the chainjs library to get started
```

To run ts files (and examples), use ts-node (with the --files option to include local customTypes)
Important: Node version 13 or greater is required to run polkadot examples - Hint: Use nvm to run run node on your local machine
```bash
$ ts-node --files mycode.ts
```
711 changes: 704 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
},
"dependencies": {
"@aikon/sjcl": "^0.1.8",
"@polkadot/api": "^4.0.3",
"@polkadot/keyring": "^6.0.5",
"@polkadot/types": "^4.0.3",
"@polkadot/util": "^6.0.5",
"@polkadot/util-crypto": "^6.0.5",
"@types/bignumber.js": "^5.0.0",
"@types/bn.js": "^4.11.6",
"@types/bs58": "^4.0.1",
Expand Down
50 changes: 50 additions & 0 deletions parachains
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@



ParachainA Manifest
- DPOS
- curve type
- Account palate Type 1
- Currency ABC
- Fee palate type 1

ParachainB
- POW
- curve type
- Account palate Type 2
- Currency XYZ
- No Fees

ParachainC
- POW
- curve type
- No Accounts
- No Currency


Developer Code

- CreateAccount on chain
- Transfer Currency on chain
- ...
- compose transaction type X on chain
- sign transaction
- send transaction to chain
- wait for transaction confirmation

chainJS != Poladot chain standard (parachains are diff)


para

new ChainFactory(PolkaDotV1, options: {parachainManifest})



Developer App - Game

(TX GAME123) -> Parachain X - general purpose contract chain (game contracts go here)
(tx 123) -> Parachain A - Marketplace for NFTs
(tx 456) -> Parachain B - Bridge to ETH (send 721)
Parachain C - service XYC

3 changes: 3 additions & 0 deletions src/chainFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChainEosV2 } from './chains/eos_2/ChainEosV2'
import { ChainEthereumV1 } from './chains/ethereum_1/ChainEthereumV1'
import { ChainAlgorandV1 } from './chains/algorand_1/ChainAlgorandV1'
import { ChainPolkadotV1 } from './chains/polkadot_1/ChainPolkadotV1'
import { Chain } from './interfaces'
import { ChainType, ChainEndpoint } from './models'
import { throwNewError } from './errors'
Expand All @@ -16,6 +17,8 @@ export class ChainFactory {
return new ChainEthereumV1(endpoints, settings)
case ChainType.AlgorandV1:
return new ChainAlgorandV1(endpoints, settings)
case ChainType.PolkadotV1:
return new ChainPolkadotV1(endpoints, settings)
default:
throwNewError(`Chain type ${chainType} is not supported`)
}
Expand Down
19 changes: 16 additions & 3 deletions src/chains/algorand_1/algoAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import * as algosdk from 'algosdk'
import { isNullOrEmpty, toBuffer, bufferToString, isAUint8Array, isAString, isAUint8ArrayArray } from '../../helpers'
import { throwNewError } from '../../errors'
import {
AlgorandTransactionOptions,
AlgorandTxAction,
AlgorandTxActionRaw,
AlgorandTxActionSdkEncoded,
AlgorandTxActionSdkEncodedFields,
} from './models/transactionModels'
import { AlgorandTxHeaderParams, AlgorandChainTransactionParamsStruct } from './models'
import { ALGORAND_TRX_COMFIRMATION_ROUNDS, ALGORAND_EMPTY_CONTRACT_NAME } from './algoConstants'
import {
ALGORAND_CHAIN_BLOCK_FREQUENCY,
ALGORAND_DEFAULT_TRANSACTION_VALID_BLOCKS,
ALGORAND_EMPTY_CONTRACT_NAME,
} from './algoConstants'
import { toAlgorandAddressFromRaw, toRawAddressFromAlgoAddr } from './helpers'

/** Helper class to ensure transaction actions properties are set correctly
Expand Down Expand Up @@ -212,12 +217,20 @@ export class AlgorandActionHelper {

/** Adds the latest transaction header fields (firstRound, etc.) from chain
* Applies any that are not already provided in the action */
applyCurrentTxHeaderParamsWhereNeeded(chainTxParams: AlgorandChainTransactionParamsStruct) {
applyCurrentTxHeaderParamsWhereNeeded(
chainTxParams: AlgorandChainTransactionParamsStruct,
transactionOptions?: AlgorandTransactionOptions,
) {
const rawAction = this.raw
// calculate last block
const numberOfBlockValidFor = transactionOptions?.expireSeconds
? transactionOptions?.expireSeconds * ALGORAND_CHAIN_BLOCK_FREQUENCY
: ALGORAND_DEFAULT_TRANSACTION_VALID_BLOCKS
const lastValidBlock = rawAction.firstRound + numberOfBlockValidFor
rawAction.genesisID = rawAction.genesisID || chainTxParams.genesisID
rawAction.genesisHash = rawAction.genesisHash || toBuffer(chainTxParams.genesisHash, 'base64')
rawAction.firstRound = rawAction.firstRound || chainTxParams.firstRound
rawAction.lastRound = rawAction.lastRound || rawAction.firstRound + ALGORAND_TRX_COMFIRMATION_ROUNDS
rawAction.lastRound = rawAction.lastRound || lastValidBlock
rawAction.fee = rawAction.fee || chainTxParams.minFee
rawAction.flatFee = true // since we're setting a fee, this will always be true - flatFee is just a hint to the AlgoSDK.Tx object which will set its own fee if this is not true
}
Expand Down
4 changes: 3 additions & 1 deletion src/chains/algorand_1/algoConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ export const PUBLIC_KEY_LENGTH = nacl.sign.publicKeyLength
export const ALGORAND_EMPTY_CONTRACT_NAME = 'none'
export const ALGORAND_POST_CONTENT_TYPE = { 'content-type': 'application/x-binary' }
/** Number of rounds to wait after the first round for the transaction confirmation */
export const ALGORAND_TRX_COMFIRMATION_ROUNDS = 1000
export const ALGORAND_DEFAULT_TRANSACTION_VALID_BLOCKS = 1000
export const DEFAULT_TIMEOUT_FOR_TRX_CONFIRM = 500
export const DEFAULT_ALGO_UNIT = AlgorandUnit.Microalgo

/** number of chain blocks to poll after submitting a transaction before failing */
export const DEFAULT_BLOCKS_TO_CHECK = 20
/** time to wait (in ms) between checking chain for a new block (to see if transaction appears within it) */
export const DEFAULT_CHECK_INTERVAL = 500
/** time in seconds between blocks */
export const ALGORAND_CHAIN_BLOCK_FREQUENCY = 2
/** number of times to attempt to read a chain endpoint before failing the read */
export const DEFAULT_GET_BLOCK_ATTEMPTS = 10

Expand Down
18 changes: 16 additions & 2 deletions src/chains/algorand_1/algoTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { throwNewError } from '../../errors'
import {
byteArrayToHexString,
hexStringToByteArray,
isANumber,
isArrayLengthOne,
isNullOrEmpty,
notImplemented,
Expand Down Expand Up @@ -79,7 +80,7 @@ export class AlgorandTransaction implements Transaction {
constructor(chainState: AlgorandChainState, options?: AlgorandTransactionOptions) {
this._chainState = chainState
this.assertValidOptions(options)
this._options = options || {}
this.applyOptions(options)
}

/** Chain-specific values included in the transaction sent to the chain */
Expand Down Expand Up @@ -188,7 +189,7 @@ export class AlgorandTransaction implements Transaction {
this._algoSdkTransaction = null
} else {
const chainTxHeaderParams = this._chainState.chainInfo.nativeInfo.transactionHeaderParams
this._actionHelper.applyCurrentTxHeaderParamsWhereNeeded(chainTxHeaderParams)
this._actionHelper.applyCurrentTxHeaderParamsWhereNeeded(chainTxHeaderParams, this.options)
this._algoSdkTransaction = new AlgoTransactionClass(this._actionHelper.actionEncodedForSdk)
}
}
Expand Down Expand Up @@ -579,13 +580,26 @@ export class AlgorandTransaction implements Transaction {

/** Throws if from is not null or empty algorand argument */
private assertValidOptions(options: AlgorandTransactionOptions): void {
if (options?.expireSeconds && !isANumber(options.expireSeconds)) {
throwNewError('Invalid transaction options: ExpireSeconds is not a number')
}
if (options?.multiSigOptions && options?.signerPublicKey) {
throwNewError(
'Invalid transaction options: Provide multiSigOptions OR signerPublicKey - not both. The signerPublicKey is for non-multisig transasctions only',
)
}
}

/** apply options and/or use defaults */
private applyOptions(options: AlgorandTransactionOptions) {
const { multiSigOptions, signerPublicKey } = options || {}
let { expireSeconds, fee, flatFee } = options || {}
expireSeconds = expireSeconds ?? this._chainState?.chainSettings?.defaultTransactionSettings?.expireSeconds
fee = fee ?? this._chainState?.chainSettings?.defaultTransactionSettings?.fee
flatFee = flatFee ?? this._chainState?.chainSettings?.defaultTransactionSettings?.flatFee
this._options = { expireSeconds, fee, flatFee, multiSigOptions, signerPublicKey }
}

/** Whether the transaction signature is valid for this transaction body and publicKey provided */
private isValidTxSignatureForPublicKey(signature: AlgorandSignature, publicKey: AlgorandPublicKey): boolean {
if (!this.rawTransaction) return false
Expand Down
2 changes: 2 additions & 0 deletions src/chains/algorand_1/models/transactionModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export type AlgorandSuggestedParams = {

/** Transaction 'header' options set to chain along with the content type */
export type AlgorandTransactionOptions = {
/** Number of seconds after which transaction expires - must be submitted to the chain before then */
expireSeconds?: number
fee?: AlgorandValue
flatFee?: boolean
multiSigOptions?: AlgorandMultiSigOptions
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum_1/models/cryptoModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type EthereumPublicKey = string & PublicKeyBrand
/** a signature string - formatted correcly for ethereum */
export type EthereumSignature = ECDSASignature & SignatureBrand

/** key pair - in the format returned from algosdk */
/** key pair - in the format returned from ethereum */
export type EthereumKeyPair = {
publicKey: EthereumPublicKey
privateKey: EthereumPrivateKey
Expand Down
Loading