```
--------------------------------
### Implementing API Signature Generation in Go
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/zh-CN/api/sign.md
This Go code provides a complete solution for generating API signatures. It includes the `APISign` function for orchestrating the signature process, `calculateHMACSHA256` for cryptographic hashing, `getPath` for normalizing URL components, and `getJSONBody` which canonicalizes JSON request bodies by removing empty fields and sorting keys to ensure consistent input for hashing. The `TestAPISign` function demonstrates how to use these utilities to generate a signature for a POST request.
```Go
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"testing"
"time"
)
func APISign(timestamp string, method string, requestURL string, body string, secretKey string) (string, error) {
content := timestamp + strings.ToUpper(method) + getPath(requestURL) + getJSONBody(body)
signature, err := calculateHMACSHA256([]byte(content), []byte(secretKey))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(signature), nil
}
func calculateHMACSHA256(message []byte, secret []byte) ([]byte, error) {
hash := hmac.New(sha256.New, secret)
_, err := hash.Write(message)
if err != nil {
return nil, err
}
return hash.Sum(nil), nil
}
func getPath(requestURL string) string {
u, err := url.Parse(requestURL)
if err != nil {
return ""
}
path := u.Path
query := u.Query()
query.Del("")
keys := make([]string, 0, len(query))
for key := range query {
keys = append(keys, key)
}
sort.Strings(keys)
var encodedParams []string
for _, key := range keys {
encodedParams = append(encodedParams, fmt.Sprintf("%s=%s", key, query.Get(key)))
}
paramsString := strings.Join(encodedParams, "&")
if paramsString != "" {
return path + "?" + paramsString
}
return path
}
func getJSONBody(body string) string {
if body == "" {
return ""
}
var data interface{}
err := json.Unmarshal([]byte(body), &data)
if err != nil {
return ""
}
if dataMap, ok := data.(map[string]interface{}); ok && len(dataMap) == 0 {
return ""
}
removeEmptyKeys(data)
sortedData := sortObject(data)
jsonBytes, err := json.Marshal(sortedData)
if err != nil {
return ""
}
return string(jsonBytes)
}
func removeEmptyKeys(data interface{}) {
switch value := data.(type) {
case map[string]interface{}:
for key, v := range value {
if v == nil || v == "" {
delete(value, key)
} else {
removeEmptyKeys(v)
}
}
case []interface{}:
for _, v := range value {
removeEmptyKeys(v)
}
}
}
func sortObject(data interface{}) interface{} {
switch value := data.(type) {
case map[string]interface{}:
keys := make([]string, 0, len(value))
for k := range value {
keys = append(keys, k)
}
sort.Strings(keys)
sortedMap := make(map[string]interface{})
for _, k := range keys {
v := sortObject(value[k])
sortedMap[k] = v
}
return sortedMap
case []interface{}:
for i, v := range value {
value[i] = sortObject(v)
}
return value
default:
return value
}
}
func TestAPISign(t *testing.T) {
timestamp := fmt.Sprintf("%d", time.Now().UnixMilli())
method := "POST"
requestURL := "/mid/api/v1/partner/user"
reqBody := map[string]string{
"platform": "Telegram",
"platformId": "6112374290"
}
dataType, _ := json.Marshal(reqBody)
dataString := string(dataType)
appId := "your appid"
secretKey := "your app secretKey"
sign, err := APISign(timestamp, method, requestURL, dataString, secretKey)
if err != nil {
fmt.Println("Error:", err.Error())
return
}
fmt.Println("sign", sign)
}
```
--------------------------------
### Importing Blockchain Chains for Wallet Integration - JavaScript
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/chains.md
This snippet demonstrates how to import various blockchain networks from `viem/chains` and MatchID-specific chains from `@matchain/matchid-sdk-react/chains`. These imported chain objects are intended for use with the `useWallet` hook to configure wallet connections.
```jsx
import {
mainnet,
arbitrum,
arbitrumGoerli,
base,
baseSepolia,
baseGoerli,
bsc,
bscTestnet,
...otherChains
} from 'viem/chains';
import {MatchMain, MatchTest} from "@matchain/matchid-sdk-react/chains";
```
--------------------------------
### Importing LoginPanel Component - TypeScript
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/components/LoginPanel.md
This snippet demonstrates how to import the `LoginPanel` component from the `@matchain/matchid-sdk-react` package. It destructures `LoginPanel` from the `Components` object. This is a prerequisite for using the component in a React application.
```typescript
import {Components} from '@matchain/matchid-sdk-react';
const {LoginPanel} = Components
```
--------------------------------
### Styling Card Components with CSS
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/index.md
This CSS defines a responsive layout for card components using Flexbox, ensuring consistent sizing, hover effects, and dark mode compatibility. It includes styles for card containers, individual cards, and their internal elements, adapting to mobile views by stacking cards vertically.
```css
/* Flexbox container for all cards */
.card-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 20px;
margin-top: 20px;
align-items: stretch; /* ✅ Ensure cards stretch to match height */
}
/* Consistent Card Styling for Uniform Size */
.card {
flex: 1; /* Flex property for equal growth */
flex-basis: 200px; /* ✅ Ensures all cards start with the same width */
max-width: 200px; /* Prevent excessive growth */
min-height: 250px; /* ✅ Consistent height for all cards */
text-decoration: none;
border: 2px solid lightgray;
border-radius: 10px;
padding: 20px;
display: flex;
flex-direction: column;
justify-content: space-between; /* ✅ Balanced spacing */
align-items: center;
text-align: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
.card-explore-by-sdk {
flex: 1; /* Flex property for equal growth */
flex-basis: 100px; /* ✅ Ensures all cards start with the same width */
max-width: 100px; /* Prevent excessive growth */
min-height: 120px; /* ✅ Consistent height for all cards */
text-decoration: none;
border: 2px solid lightgray;
border-radius: 10px;
padding: 20px;
display: flex;
flex-direction: column;
justify-content: space-between; /* ✅ Balanced spacing */
align-items: center;
text-align: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
/* Hover Effect */
.card:hover {
background-color: #F9F9F9;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
/* Card Elements Styling */
.card-icon {
font-size: 40px;
color: black !important;
margin-bottom: 10px;
}
.card-title {
color: black !important;
font-size: 1.5rem;
font-weight: bold;
margin: 10px 0;
}
.card-description {
color: rgba(60, 60, 67) !important;
font-size: 14px;
line-height: 1.5;
}
/* Prevent Underline on Hover */
.card:hover .card-title,
.card:hover .card-description,
.card:hover .card-icon {
text-decoration: none;
color: inherit;
}
/* Dark Mode Styling */
html.dark .card {
border: 2px solid #333;
color: rgba(255, 255, 255, 0.85);
background-color: #222;
}
html.dark .card:hover {
background-color: #2c2c2c;
}
html.dark .card-icon {
color: rgba(255, 255, 255, 0.85);
}
html.dark .card-title,
html.dark .card-description {
color: rgba(255, 255, 245, 0.86) !important;
}
/* ✅ Mobile View Fix: Cards Stack Vertically */
@media (max-width: 768px) {
.card-container {
flex-direction: column;
align-items: center;
}
.card {
width: 100%; /* Full width for mobile screens */
max-width: 350px;
min-height: 350px; /* Consistent height */
}
}
```
--------------------------------
### Configuring LoginModal with All Props - TypeScript
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/components/LoginModal.md
This snippet illustrates how to use the `LoginModal` component with a comprehensive set of props, including `width`, `header`, `onClose` callback, and arrays for `methods` and `recommendMethods`, allowing for extensive customization.
```typescript
import React from 'react';
import { LoginMethodType } from '../types/types';
import {Components} from '@matchain/matchid-sdk-react';
const {LoginModal} = Components
function App() {
const methods = [ 'telegram', 'twitter'];
const recommendMethods = [ 'wallet', 'email','google'];
return (
Custom Header
}
onClose={() => console.log('Close button clicked')}
methods={methods}
recommendMethods={recommendMethods}
/>
);
}
export default App;
```
--------------------------------
### Importing useWallet Hook in TypeScript
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/hooks/useWallet.md
This snippet demonstrates how to import the `useWallet` hook from the `@matchain/matchid-sdk-react/hooks` package. It shows two common ways to import: direct named import and destructuring from a `Hooks` object, making the wallet functionalities available for use in a component.
```TypeScript
import { useWallet } from '@matchain/matchid-sdk-react/hooks';
const { useWallet } = Hooks;
```
--------------------------------
### Customizing LoginButton with All Props in React
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/components/LoginButton.md
This snippet illustrates how to customize the `LoginButton` component using various props. It configures specific login methods, recommended methods, wallet types, popover position, type, and gap, and defines a callback for the `onLoginClick` event, demonstrating advanced usage.
```typescript
import {Components} from '@matchain/matchid-sdk-react';
const {LoginButton} = Components;
function App() {
const handleLoginClick = () => {
console.log('Login button clicked');
};
return (
);
}
export default App;
```
--------------------------------
### Importing Button Component in TypeScript
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/components/Button.md
This snippet demonstrates how to import the `Button` component from the `@matchain/matchid-sdk-react` package, making it available for use in a React application.
```TypeScript
import {Components} from '@matchain/matchid-sdk-react';
const {Button} = Components
```
--------------------------------
### Importing Hooks from @matchain/matchid-sdk-react (JSX)
Source: https://github.com/affkoul/matchid-docs/blob/main/docs/react/hooks/index.md
This snippet demonstrates how to import and destructure the `Hooks` object from the `@matchain/matchid-sdk-react` library. It shows how to access specific hooks like `useMatchEvents`, `useUserInfo`, and `useWallet` for use in React components, enabling interaction with Matchain functionalities.
```jsx
import {Hooks} from '@matchain/matchid-sdk-react';
const {useMatchEvents, useUserInfo, useWallet} = Hooks
```