Skip to content

Commit

Permalink
[TypeScript SDK] Implement transfer coin (MystenLabs#1865)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored May 9, 2022
1 parent d4d894e commit 0bd01d0
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 111 deletions.
8 changes: 7 additions & 1 deletion explorer/client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1453,12 +1453,13 @@
integrity sha512-nkalE/f1RvRGChwBnEIoBfSEYOXnCRdleKuv6+lePbMDrMZXeDQnqak5XDOeBgrPPyPfAdcCu/B5z+v3VhplGg==

"@mysten/sui.js@file:../../sdk/typescript":
version "0.1.1"
version "0.1.2"
dependencies:
bn.js "^5.2.0"
buffer "^6.0.3"
cross-fetch "^3.1.5"
jayson "^3.6.6"
js-sha3 "^0.8.0"
tweetnacl "^1.0.3"
util "^0.12.4"

Expand Down Expand Up @@ -6166,6 +6167,11 @@ joi@^17.6.0:
"@sideway/formula" "^3.0.0"
"@sideway/pinpoint" "^2.0.0"

js-sha3@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down
30 changes: 23 additions & 7 deletions sdk/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,46 @@ Follow the [JSON RPC doc](https://github.com/MystenLabs/sui/blob/main/doc/src/bu

The `JsonRpcProvider` class provides a connection to the JSON-RPC Server and should be used for all read-only operations. For example:

Fetch objects owned by the address `C5206DD02C86A510C4848516229B02ADDFACBE55`
Fetch objects owned by the address `0xbff6ccc8707aa517b4f1b95750a2a8c666012df3`

```typescript
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('http://127.0.0.1:5001/');
const provider = new JsonRpcProvider('https://gateway.devnet.sui.io:9000/');
const objects = await provider.getOwnedObjectRefs(
'C5206DD02C86A510C4848516229B02ADDFACBE55'
'0xbff6ccc8707aa517b4f1b95750a2a8c666012df3'
);
```

Fetch transaction details from a transaction digest:

```typescript
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('http://127.0.0.1:5001/');
const provider = new JsonRpcProvider('https://gateway.devnet.sui.io:9000/');
const txn = await provider.getTransaction(
'6mn5W1CczLwitHCO9OIUbqirNrQ0cuKdyxaNe16SAME='
);
```

For any operations that involves signing or submitting transactions, you should use the `Signer` API. For example:

To sign a raw message:
TODO

To transfer a Coin<SUI>:

```typescript
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
// Generate a new Keypair
const keypair = new Ed25519Keypair();
const signer = new RawSigner(
keypair,
new JsonRpcProvider('https://gateway.devnet.sui.io:9000/')
);
const txn = await signer.transferCoin({
signer: keypair.getPublicKey().toSuiAddress(),
objectId: '0x5015b016ab570df14c87649eda918e09e5cc61e0',
gasPayment: '0x0a8c2a0fd59bf41678b2e22c3dd2b84425fb3673',
gasBudget: 10000,
recipient: '0xBFF6CCC8707AA517B4F1B95750A2A8C666012DF3',
});
```

To sign a raw message:
TODO
3 changes: 2 additions & 1 deletion sdk/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.1",
"version": "0.1.2",
"license": "Apache-2.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -67,6 +67,7 @@
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"jayson": "^3.6.6",
"js-sha3": "^0.8.0",
"tweetnacl": "^1.0.3",
"util": "^0.12.4"
},
Expand Down
20 changes: 17 additions & 3 deletions sdk/typescript/src/cryptography/publickey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import BN from 'bn.js';
import { Buffer } from 'buffer';
import { sha3_256 } from 'js-sha3';

/**
* Value to be converted into public key
Expand Down Expand Up @@ -47,7 +48,9 @@ export class PublicKey {
if (typeof value === 'string') {
const buffer = Buffer.from(value, 'base64');
if (buffer.length !== 32) {
throw new Error(`Invalid public key input`);
throw new Error(
`Invalid public key input. Expected 32 bytes, got ${buffer.length}`
);
}
this._bn = new BN(buffer);
} else {
Expand Down Expand Up @@ -105,7 +108,18 @@ export class PublicKey {
* Return the Sui address associated with this public key
*/
toSuiAddress(): string {
// TODO: implement this by apply sha256 hashing and take the first 20 bytes
throw new Error('Not implemented');
const hexHash = sha3_256(this.toBytes());
const publicKeyBytes = new BN(hexHash, 16).toArray(undefined, 32);
// Only take the first 20 bytes
const addressBytes = publicKeyBytes.slice(0, 20);
return toHexString(addressBytes);
}
}

// https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript
function toHexString(byteArray: number[]) {
return byteArray.reduce(
(output, elem) => output + ('0' + elem.toString(16)).slice(-2),
''
);
}
Loading

0 comments on commit 0bd01d0

Please sign in to comment.