# Quick start

### Install

sharkdag.js 라이브러리는 브라우저(html 코드)와 Node.js 에서 직접 사용할 수 있습니다.

**Node.js 를 통한 사용방법**

Node.js에 직접 사용하고자 할 경우에는 아래와 같이 명령을 실행하세요.

```
npm i https://github.com/dubu4-dev/shark-dag-js.git --save
```

**브라우저(html)를 통한 사용방법**

index.html 등을 만들고 sharkdag.js 스크립트 소스에 포함 시키면 됩니다.

```
<script src="public/js/sharkdag.min.js"></script>
```

#### 코드 테스트 (Unit Test with Jest)

Jest를 이용한 유닛 테스트를 할 수 있습니다.

```
yarn test --verbose
```

### 사용법

웹소켓 클라이언트 생성:

```
const sharkdag = require('shark-dag-js');

// 기본 공식 허브에 연결 'wss://mainnet.dubu4.com/hub'
const client = new sharkdag.Client();

// 특정 허브에 연결
const client = new sharkdag.Client('wss://testnet.dubu4.com/hub');

// Connect to testnet
const options = { testnet: true };
const client = new sharkdag.Client('wss://testnet.dubu4.com/hub', options);
```

클라이언트 인스턴스 종료:

```
client.close();
```

모든 API 메서드는 아래와 같은 패턴을 가집니다:

```
// 마지막 인자가 함수이면 콜백 함수로 처리함
client.api.getJoint('D6XJ4WJmtMJ+1nNIIaUhbxvhP+yWKU/a/nlhOKf0Hq0=', function(err, result) {
  console.log(err, result);
});

// 콜백이 제공되지 않을 경우 Promise를 반환함
client.api.getJoint('D6XJ4WJmtMJ+1nNIIaUhbxvhP+yWKU/a/nlhOKf0Hq0=').then(function(result) {
  console.log(result);
});
```

#### Transaction

유닛을 작성하고 포스팅하기 위해서는 먼저 DUBU4 월렛을 생성하고 일정량의 MO를 소유하고 있어야 합니다. 실제 sharkdag.js를 이용하여 송금 코드를 작성하기 위해서는 임시로 WIF(Wallet Import Format)를 발급받아 여기로 필요한 양의 MO + 수수료를 송금하고 sharkdag.js 라이브러리에서 WIF를 직접 호출하여 사용하도록 합니다.

[Generate a random address](https://dev-explorer.dubu4.com/utils/generate-wallet)

송금하기 샘플:

```
const wif = '5JBFvTeSY5...'; // 생성된 WIF 문자열 (private key)

const params = {
  outputs: [
    {
      address: 'NX2BTV43XN6BOTCYZUUFU6TK7DVOC4LU', // 받을 사람 주소
      amount: 1000 // 보낼 금액 = MO * 1e6
    }
  ]
};

client.post.payment(params, wif, function(err, result) {
  console.log(result); // The unit hash is returned
});
```

###
