80 lines
1.7 KiB
Markdown
80 lines
1.7 KiB
Markdown
|
# BDContract SDK
|
||
|
|
||
|
BDContract SDK for Node.js and browsers.
|
||
|
|
||
|
## Install
|
||
|
|
||
|
`npm install @bdware/bdcontract-sdk`
|
||
|
|
||
|
```typescript
|
||
|
import { Client } from '@bdware/bdcontract-sdk'
|
||
|
import { sm2 } from 'sm-crypto'
|
||
|
const client = new Client(
|
||
|
url,
|
||
|
(ev) => {
|
||
|
console.log(JSON.stringify(ev))
|
||
|
},
|
||
|
(ev, ws) => {
|
||
|
console.log(ev.data)
|
||
|
},
|
||
|
sm2.generateKeyPairHex(),
|
||
|
)
|
||
|
```
|
||
|
|
||
|
To use with plain HTML and JavaScript, include [dist/bdcontract-sdk.umd.js](dist/bdcontract-sdk.umd.js) (example from [test/index.html](test/index.html)):
|
||
|
|
||
|
```HTML
|
||
|
<script type="text/javascript" src="./sm2.js"></script>
|
||
|
<script type="text/javascript" src="./cryptico.iife.js"></script>
|
||
|
<script type="text/javascript" src="../dist/bdcontract-sdk.iife.js"></script>
|
||
|
```
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
```javascript
|
||
|
// Approach 1:
|
||
|
const wssocket = new WsSocket(url, wsHandler)
|
||
|
|
||
|
// Approach 2:
|
||
|
function print(string) {
|
||
|
document.write(string + '\n\n')
|
||
|
}
|
||
|
|
||
|
const url = 'ws://021.node.internetapi.cn:21030/SCIDE/SCExecutor'
|
||
|
|
||
|
const client = new bdcontract.Client(
|
||
|
url,
|
||
|
(ev) => {
|
||
|
console.log(JSON.stringify(ev))
|
||
|
},
|
||
|
(ev, ws) => {
|
||
|
console.log(ev.data)
|
||
|
},
|
||
|
)
|
||
|
setTimeout(async () => {
|
||
|
const status = client.status()
|
||
|
console.log(status)
|
||
|
try {
|
||
|
let data = await client.executeContract(
|
||
|
'AnnotationExample0608',
|
||
|
'main',
|
||
|
'abacd',
|
||
|
)
|
||
|
print(JSON.stringify(data))
|
||
|
} catch (data) {
|
||
|
print(JSON.stringify(data))
|
||
|
}
|
||
|
}, 1000)
|
||
|
|
||
|
// Wait for session receival
|
||
|
const session = await client.sessionReceived()
|
||
|
|
||
|
// Login and wait (will wait if session not yet received)
|
||
|
const success = await client.login()
|
||
|
|
||
|
async function anotherPlace() {
|
||
|
// In another place, wait for login to complete
|
||
|
const success = await client.Loggedin()
|
||
|
}()
|
||
|
```
|